Skip to content

Instantly share code, notes, and snippets.

View omarsar's full-sized avatar
🐙

Elvis Saravia omarsar

🐙
View GitHub Profile
'''
[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>)]
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 = []
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 = []
print(Y0_val)
print(Y1_val)
### output
'''
tensor([[-0.9963, -0.9877, 0.9999, -0.1916, 0.3522],
[-0.9979, -1.0000, 1.0000, 0.7448, -0.8681],
[-0.9988, -1.0000, 1.0000, 0.9714, -0.9952],
[ 0.9948, -1.0000, -1.0000, 0.9981, -1.0000]])
tensor([[-0.7492, -1.0000, 1.0000, 0.9997, -0.9985],
N_INPUT = 3 # number of features in input
N_NEURONS = 5 # number of units in layer
X0_batch = torch.tensor([[0,1,2], [3,4,5],
[6,7,8], [9,0,1]],
dtype = torch.float) #t=0 => 4 X 3
X1_batch = torch.tensor([[9,8,7], [0,0,0],
[6,5,4], [3,2,1]],
dtype = torch.float) #t=1 => 4 X 3
class BasicRNN(nn.Module):
def __init__(self, n_inputs, n_neurons):
super(BasicRNN, self).__init__()
self.Wx = torch.randn(n_inputs, n_neurons) # n_inputs X n_neurons
self.Wy = torch.randn(n_neurons, n_neurons) # n_neurons X n_neurons
self.b = torch.zeros(1, n_neurons) # 1 X n_neurons
def forward(self, X0, X1):
print(Y0_val)
print(Y1_val)
### output
'''
tensor([[-0.9963, -0.9877, 0.9999, -0.1916, 0.3522],
[-0.9979, -1.0000, 1.0000, 0.7448, -0.8681],
[-0.9988, -1.0000, 1.0000, 0.9714, -0.9952],
[ 0.9948, -1.0000, -1.0000, 0.9981, -1.0000]])
tensor([[-0.7492, -1.0000, 1.0000, 0.9997, -0.9985],
N_INPUT = 4
N_NEURONS = 1
X0_batch = torch.tensor([[0,1,2,0], [3,4,5,0],
[6,7,8,0], [9,0,1,0]],
dtype = torch.float) #t=0 => 4 X 4
X1_batch = torch.tensor([[9,8,7,0], [0,0,0,0],
[6,5,4,0], [3,2,1,0]],
dtype = torch.float) #t=1 => 4 X 4
class SingleRNN(nn.Module):
def __init__(self, n_inputs, n_neurons):
super(SingleRNN, self).__init__()
self.Wx = torch.randn(n_inputs, n_neurons) # 4 X 1
self.Wy = torch.randn(n_neurons, n_neurons) # 1 X 1
self.b = torch.zeros(1, n_neurons) # 1 X 4
def forward(self, X0, X1):
import torch
import torch.nn as nn
import torch.nn.functional as F
import os
import numpy as np