This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model = get_model() | |
model.compile("adam", "binary_crossentropy", metrics=["accuracy"]) | |
model.fit(ds1_train, epochs=3, validation_data=ds1_val) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shapes=([200],()) | |
ds1_train = ( | |
ds1_train | |
.map(encode_map_fn) | |
.map(truncate) | |
.cache() | |
.shuffle(1024) | |
.padded_batch(32, shapes) | |
.prefetch(tf.data.experimental.AUTOTUNE) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ds1_train, ds1_val), ds1_info = tfds.load('imdb_reviews', | |
split=['train', 'test'], | |
as_supervised=True, | |
with_info=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model.compile("adam", "binary_crossentropy", metrics=["accuracy"]) | |
model.fit(x_train, y_train, batch_size=32, epochs=3, validation_data=(x_val, y_val)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |