This file contains 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 = LeNet(num_classes=10) | |
model.compile(loss=tf.losses.categorical_crossentropy, | |
optimizer=tf.optimizers.SGD(learning_rate=1e-1, decay=1e-6, momentum=9e-1), | |
metrics=['accuracy']) | |
model.fit(train_dataset, | |
epochs=60, | |
validation_data=validation_dataset, | |
verbose=2) |
This file contains 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
(train_features, train_labels), (test_features, test_labels) = tf.keras.datasets.mnist.load_data() | |
train_features = train_features.reshape(-1, 28, 28, 1) | |
train_features = train_features.astype('float32') | |
train_features = train_features / 255. | |
test_features = test_features.reshape(-1, 28, 28, 1) | |
test_features = test_features.astype('float32') | |
test_features = test_features / 255. |
This file contains 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 score(self, X: np.ndarray, Y: np.ndarray, k: int = 2, dist_type: str = 'point') \ | |
-> Tuple[np.ndarray, np.ndarray]: | |
d = np.tile(None, (X.shape[0], self.classes)) # init distance matrix: [nb instances, nb classes] | |
for c in range(self.classes): | |
d_tmp = self.kdtrees[c].query(X, k=k)[0] # get k nearest neighbors for each class | |
if dist_type == 'point': | |
d[:, c] = d_tmp[:, -1] | |
elif dist_type == 'mean': |
This file contains 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 filter_by_distance_knn(self, X: np.ndarray) -> np.ndarray: | |
kdtree = KDTree(X, leaf_size=self.leaf_size, metric=self.metric) | |
knn_r = kdtree.query( | |
X, k=self.k_filter + 1 | |
)[0] # distances from 0 to k-nearest points | |
if self.dist_filter_type == 'point': | |
knn_r = knn_r[:, -1] | |
elif self.dist_filter_type == 'mean': | |
knn_r = np.mean( | |
knn_r[:, 1:], axis=1 |
This file contains 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
class NeuralNet(tf.keras.Model): | |
def __init__(self, **kwargs): | |
super(NeuralNet, self).__init__() | |
self.hidden_layer_1 = tf.keras.layers.Dense( | |
units=kwargs['units'][0], | |
activation=tf.nn.relu, | |
input_shape=kwargs['input_shape'] | |
) | |
self.dropout_layer_1 = tf.keras.layers.Dropout( | |
rate=['dropout_rate'] |
This file contains 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
class MiniVGG(tf.keras.Model): | |
def __init__(self, **kwargs): | |
super(MiniVGG, self).__init__() | |
self.conv1_layer_1 = tf.keras.layers.Conv2D( | |
filters=32, | |
kernel_size=(3, 3), | |
activation=tf.nn.relu, | |
input_shape=kwargs['input_shape'] | |
) | |
self.conv1_layer_2 = tf.keras.layers.Conv2D( |
This file contains 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
class LeNet(tf.keras.Model): | |
def __init__(self, **kwargs): | |
super(LeNet, self).__init__() | |
self.conv_layer_1 = tf.keras.layers.Conv2D( | |
filters=6, | |
kernel_size=(5, 5), | |
input_shape=(28, 28, 1), | |
padding='valid', | |
activation=tf.nn.relu | |
) |
This file contains 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 train_step(model, loss, features, labels, epoch): | |
with tf.GradientTape() as tape: | |
logits = model(features) | |
train_loss = loss(logits, labels) | |
gradients = tape.gradient(train_loss, model.trainable_variables) | |
stddev = 1 / ((1 + epoch)**0.55) | |
gradients = [tf.add(gradient, tf.random.normal(stddev=stddev, mean=0., shape=gradient.shape)) for gradient in gradients] | |
model.optimizer.apply_gradients(zip(gradients, model.trainable_variables)) | |
return train_loss, gradients |
This file contains 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
epochs = 60 | |
writer = tf.summary.create_file_writer('tmp') | |
with writer.as_default(): | |
with tf.summary.record_if(True): | |
for epoch in range(epochs): | |
for step, batch_features in enumerate(train_dataset): | |
with tf.GradientTape() as tape: | |
z_mean, z_log_var, z = vae.encoder(tf.constant(batch_features)) | |
reconstructed = vae.decoder(z) |
This file contains 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
class VariationalAutoencoder(tf.keras.Model): | |
def __init__(self, latent_dim, original_dim): | |
super(VariationalAutoencoder, self).__init__() | |
self.encoder = Encoder(latent_dim=latent_dim) | |
self.decoder = Decoder(original_dim=original_dim) | |
def call(self, input_features): | |
z_mean, z_log_var, latent_code = self.encoder(input_features) | |
reconstructed = self.decoder(latent_code) | |
kl_divergence = -5e-2 * tf.reduce_sum(tf.exp(z_log_var) + tf.square(z_mean) - 1 - z_log_var) |