Skip to content

Instantly share code, notes, and snippets.

model = get_model()
model.compile("adam", "binary_crossentropy", metrics=["accuracy"])
model.fit(ds1_train, epochs=3, validation_data=ds1_val)
shapes=([200],())
ds1_train = (
ds1_train
.map(encode_map_fn)
.map(truncate)
.cache()
.shuffle(1024)
.padded_batch(32, shapes)
.prefetch(tf.data.experimental.AUTOTUNE)
)
from collections import Counter
# Building vocabulary set for tokenizer
tokenizer = tfds.features.text.Tokenizer()
vocabulary_counter = Counter()
for text_tensor, _ in ds1_train:
some_tokens = tokenizer.tokenize(text_tensor.numpy())
vocabulary_counter.update(some_tokens)
vocabulary = vocabulary_counter.most_common(max_features-2)
(ds1_train, ds1_val), ds1_info = tfds.load('imdb_reviews',
split=['train', 'test'],
as_supervised=True,
with_info=True)
model.compile("adam", "binary_crossentropy", metrics=["accuracy"])
model.fit(x_train, y_train, batch_size=32, epochs=3, validation_data=(x_val, y_val))
(x_train, y_train), (x_val, y_val) = keras.datasets.imdb.load_data(
num_words=max_features
)
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=maxlen)
x_val = keras.preprocessing.sequence.pad_sequences(x_val, maxlen=maxlen)
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
max_features = 20000 # Only consider the top 20k words
maxlen = 200 # Only consider the first 200 words of each movie review
def get_model():
# Input for variable-length sequences of integers
inputs = keras.Input(shape=(None,), dtype="int32")
@negedng
negedng / img_pred.py
Last active June 29, 2020 15:31
Image classification snippet with Keras
import tensorflow as tf
from tensorflow.keras.applications.imagenet_utils import decode_predictions
from skimage import io
from skimage.transform import resize
import numpy as np
image_url = 'https://upload.wikimedia.org/wikipedia/commons/b/bb/Kittyply_edit1.jpg'
img = io.imread(image_url)
img_resized = resize(img, (224,224,3))
@negedng
negedng / label_limit_layer.py
Last active March 12, 2020 08:41
Custom Keras Layer For postprocessing in a Model
from keras.layers import Layer
import tensorflow as tf
class LabelLimitLayer(Layer):
def __init__(self, labels, topn, **kwargs):
self.labels = labels
self.topn = topn
super(LabelLimitLayer, self).__init__(**kwargs)
def lin_map_matrix(FV, W):
"""For ß basis in V and γ basis in W,
returns with the [T]^γ_ß matrix of the transformation T,
which transforms v in V to fv in FV"""
return np.transpose([np.linalg.solve(np.transpose(W), fv) for fv in FV])
def linmap_predict(v, M, V, W):
return np.matmul(np.transpose(W),np.matmul(M,np.linalg.solve(np.transpose(V),v)))