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
#!/bin/bash | |
# Define variables | |
filename='articles.txt' | |
n=1 | |
# Read in file and do processing on each one | |
while read line; do | |
# reading each line | |
n=$((n+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 myLinear(nn.Module): | |
def __init__(self, in_features, out_features, bias=True): | |
super().__init__() | |
self.in_features = in_features | |
self.out_features = out_features | |
self.bias = bias | |
self.weight = torch.nn.Parameter(torch.randn(out_features, in_features)) | |
self.bias = torch.nn.Parameter(torch.randn(out_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
class myLinear(nn.Module): | |
def __init__(self, in_features, out_features, bias=True): | |
super().__init__() | |
self.in_features = in_features | |
self.out_features = out_features | |
self.bias = bias | |
self.weight = torch.nn.Parameter(torch.Tensor(out_features, in_features)) | |
if bias: | |
self.bias = torch.nn.Parameter(torch.Tensor(out_features)) | |
else: |
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
# We'll use fast.ai to showcase how to build your own 'nn.Linear' module | |
%matplotlib inline | |
from fastai.basics import * | |
import sys | |
# create and download/prepare our MNIST dataset | |
path = Config().data_path()/'mnist' | |
path.mkdir(parents=True) | |
!wget http://deeplearning.net/data/mnist/mnist.pkl.gz -P {path} | |
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
m = RunManager() | |
# get all runs from params using RunBuilder class | |
for run in RunBuilder.get_runs(params): | |
# if params changes, following line of code should reflect the changes too | |
network = Network() | |
loader = torch.utils.data.DataLoader(train_set, batch_size = run.batch_size) | |
optimizer = optim.Adam(network.parameters(), lr=run.lr) |
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
# put all hyper params into a OrderedDict, easily expandable | |
params = OrderedDict( | |
lr = [.01, .001], | |
batch_size = [100, 1000], | |
shuffle = [True, False] | |
) | |
epochs = 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
# Helper class, help track loss, accuracy, epoch time, run time, | |
# hyper-parameters etc. Also record to TensorBoard and write into csv, json | |
class RunManager(): | |
def __init__(self): | |
# tracking every epoch count, loss, accuracy, time | |
self.epoch_count = 0 | |
self.epoch_loss = 0 | |
self.epoch_num_correct = 0 | |
self.epoch_start_time = None |
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
# import modules to build RunBuilder and RunManager helper classes | |
from collections import OrderedDict | |
from collections import namedtuple | |
from itertools import product | |
# Read in the hyper-parameters and return a Run namedtuple containing all the | |
# combinations of hyper-parameters | |
class RunBuilder(): | |
@staticmethod | |
def get_runs(params): |
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
# Build the neural network, expand on top of nn.Module | |
class Network(nn.Module): | |
def __init__(self): | |
super().__init__() | |
# define layers | |
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5) | |
self.conv2 = nn.Conv2d(in_channels=6, out_channels=12, kernel_size=5) | |
self.fc1 = nn.Linear(in_features=12*4*4, out_features=120) |
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 standard FashionMNIST dataset | |
train_set = torchvision.datasets.FashionMNIST( | |
root = './data/FashionMNIST', | |
train = True, | |
download = True, | |
transform = transforms.Compose([ | |
transforms.ToTensor() | |
]) | |
) |
NewerOlder