Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
for epoch in range(epochs): | |
loss = 0 | |
for batch_features, _ in train_loader: | |
# reshape mini-batch data to [N, 784] matrix | |
# load it to the active device | |
batch_features = batch_features.view(-1, 784).to(device) | |
# reset the gradients back to zero | |
# PyTorch accumulates gradients on subsequent backward passes | |
optimizer.zero_grad() |
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
# use gpu if available | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
# create a model from `AE` autoencoder class | |
# load it to the specified device, either gpu or cpu | |
model = AE(input_shape=784).to(device) | |
# create an optimizer object | |
# Adam optimizer with learning rate 1e-3 | |
optimizer = optim.Adam(model.parameters(), lr=1e-3) |
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
transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor()]) | |
train_dataset = torchvision.datasets.MNIST( | |
root="~/torch_datasets", train=True, transform=transform, download=True | |
) | |
test_dataset = torchvision.datasets.MNIST( | |
root="~/torch_datasets", train=False, transform=transform, download=True | |
) |
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 AE(nn.Module): | |
def __init__(self, **kwargs): | |
super().__init__() | |
self.encoder_hidden_layer = nn.Linear( | |
in_features=kwargs["input_shape"], out_features=128 | |
) | |
self.encoder_output_layer = nn.Linear( | |
in_features=128, out_features=128 | |
) | |
self.decoder_hidden_layer = nn.Linear( |
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 Decoder(tf.keras.layers.Layer): | |
def __init__(self, **kwargs): | |
super(Decoder, self).__init__() | |
self.convt_1_layer_1 = tf.keras.layers.Conv2DTranspose( | |
filters=64, | |
kernel_size=(3, 3), | |
activation=tf.nn.relu | |
) | |
self.convt_1_layer_2 = tf.keras.layers.Conv2DTranspose( | |
filters=64, |
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 Encoder(tf.keras.layers.Layer): | |
def __init__(self, **kwargs): | |
super(Encoder, self).__init__() | |
self.input_layer = tf.keras.layers.InputLayer( | |
input_shape=kwargs['input_shape'] | |
) | |
self.conv_1_layer_1 = tf.keras.layers.Conv2D( | |
filters=32, | |
kernel_size=(3, 3), | |
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
class Autoencoder(tf.keras.Model): | |
def __init__(self, **kwargs): | |
super(Autoencoder, self).__init__() | |
self.encoder = Encoder( | |
input_shape=kwargs['input_shape'], | |
latent_dim=kwargs['latent_dim'] | |
) | |
self.decoder = Decoder(latent_dim=kwargs['latent_dim']) | |
def call(self, features): |
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
from trustscore import TrustScore | |
ts = TrustScore(alpha=5e-2) | |
ts.fit(encoded_train_features, train_labels) | |
trust_score, closest_class_not_predicted = ts.score( | |
encoded_test_features, predictions, k=5 | |
) |
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 | |
def loss(actual, predicted): | |
crossentropy_loss = tf.losses.categorical_crossentropy(actual, predicted) | |
average_loss = tf.reduce_mean(crossentropy_loss) | |
return average_loss | |
def train(train_dataset, validation_dataset, epochs, learning_rate=1e-1, momentum=9e-1, decay=1e-6): | |
NewerOlder