Skip to content

Instantly share code, notes, and snippets.

View omarsar's full-sized avatar
🐙

Elvis Saravia omarsar

🐙
View GitHub Profile
# scale units
X_max, _ = torch.max(X, 0)
xPredicted_max, _ = torch.max(xPredicted, 0)
X = torch.div(X, X_max)
xPredicted = torch.div(xPredicted, xPredicted_max)
y = y / 100 # max test score is 100
@omarsar
omarsar / py_nn.py
Last active February 27, 2020 13:09
class Neural_Network(nn.Module):
def __init__(self, ):
super(Neural_Network, self).__init__()
# parameters
# TODO: parameters can be parameterized instead of declaring them here
self.inputSize = 2
self.outputSize = 1
self.hiddenSize = 3
# weights
NN = Neural_Network()
for i in range(1000): # trains the NN 1,000 times
print ("#" + str(i) + " Loss: " + str(torch.mean((y - NN(X))**2).detach().item())) # mean sum squared loss
NN.train(X, y)
NN.saveWeights(NN)
NN.predict()
import torch
import torch.nn as nn
import torch.nn.functional as F
import os
import numpy as np
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):
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
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],
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):
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
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],