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
# Copyright 2017 Abien Fred Agarap. All Rights Reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, |
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
#!/usr/bin/env python3 | |
# Paging, a memory management scheme in operating systems | |
# Author: A.F. Agarap | |
def main(): | |
page_mapping = [] | |
physical_frame_table = [] | |
virtual_page_table = [] | |
size = int(input("How many words will you enter? ")) |
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
"""TensorFlow 2.0 implementation of vanilla Autoencoder.""" | |
import numpy as np | |
import tensorflow as tf | |
__author__ = "Abien Fred Agarap" | |
np.random.seed(1) | |
tf.random.set_seed(1) | |
batch_size = 128 | |
epochs = 10 |
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
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
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) |
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
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 | |
) |
NewerOlder