Skip to content

Instantly share code, notes, and snippets.

View omarsar's full-sized avatar
🐙

Elvis Saravia omarsar

🐙
View GitHub Profile
rnn = nn.RNNCell(3, 5) # n_input X n_neurons
X_batch = torch.tensor([[[0,1,2], [3,4,5],
[6,7,8], [9,0,1]],
[[9,8,7], [0,0,0],
[6,5,4], [3,2,1]]
], dtype = torch.float) # X0 and X1
hx = torch.randn(4, 5) # m X n_neurons
output = []
class CleanBasicRNN(nn.Module):
def __init__(self, batch_size, n_inputs, n_neurons):
super(CleanBasicRNN, self).__init__()
self.rnn = nn.RNNCell(n_inputs, n_neurons)
self.hx = torch.randn(batch_size, n_neurons) # initialize hidden state
def forward(self, X):
output = []
'''
[tensor([[ 0.7728, 0.5515, -0.6296, 0.7153, -0.4945],
[-0.9922, -0.8795, -0.9972, -0.3248, -0.9954],
[-0.9994, -0.9798, -0.9995, -0.2330, -0.9987],
[-0.9997, -0.9992, -0.9671, -0.9958, -0.4567]],
grad_fn=<TanhBackward>), tensor([[-1.0000, -0.9981, -0.9999, -0.7502, -0.9987],
[ 0.2000, 0.8616, -0.4312, -0.1248, 0.4995],
[-0.9904, -0.9462, -0.9981, -0.2781, -0.9275],
[-0.6522, -0.2911, -0.8473, -0.2088, -0.2840]],
grad_fn=<TanhBackward>)]
import torchvision
import torchvision.transforms as transforms
BATCH_SIZE = 64
# list all transformations
transform = transforms.Compose(
[transforms.ToTensor()])
# download and load training dataset
import matplotlib.pyplot as plt
import numpy as np
# functions to show an image
def imshow(img):
#img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
# get some random training images
# parameters
N_STEPS = 28
N_INPUTS = 28
N_NEURONS = 150
N_OUTPUTS = 10
N_EPHOCS = 10
class ImageRNN(nn.Module):
def __init__(self, batch_size, n_steps, n_inputs, n_neurons, n_outputs):
super(ImageRNN, self).__init__()
self.n_neurons = n_neurons
self.batch_size = batch_size
self.n_steps = n_steps
self.n_inputs = n_inputs
self.n_outputs = n_outputs
dataiter = iter(trainloader)
images, labels = dataiter.next()
model = ImageRNN(BATCH_SIZE, N_STEPS, N_INPUTS, N_NEURONS, N_OUTPUTS)
logits = model(images.view(-1, 28,28))
print(logits[0:10])
### output
'''
tensor([[-0.0197, 0.0082, 0.0248, 0.0758, -0.0110, 0.0521, -0.0261, 0.0217,
-0.0083, 0.0866],
import torch.optim as optim
# Device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Model instance
model = ImageRNN(BATCH_SIZE, N_STEPS, N_INPUTS, N_NEURONS, N_OUTPUTS)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
for epoch in range(N_EPHOCS): # loop over the dataset multiple times
train_running_loss = 0.0
train_acc = 0.0
model.train()
# TRAINING ROUND
for i, data in enumerate(trainloader):
# zero the parameter gradients
optimizer.zero_grad()