Merge remote-tracking branch 'origin/master'

isaac
Pavan Mandava 5 years ago
commit 3988033311

@ -1,32 +1,25 @@
""" """
Simple feed-forward neural network in PyTorch for baseline results on Scicite data. Simple feed-forward neural network in PyTorch for baseline results on Scicite data.
Date: July 5th, 2020 Created: July 5th, 2020
""" """
import torch import torch
#import os
#os.chdir('/Users/iriley/code/citation-analysis')
import sys
sys.path.append('/Users/iriley/code/citation-analysis')
from utils.nn_reader import read_csv_nn from utils.nn_reader import read_csv_nn
from utils.nn_reader2 import read_csv_nn_dev
from sklearn.metrics import confusion_matrix
import pandas as pd
import numpy as np import numpy as np
class Feedforward(torch.nn.Module):
class FeedForward(torch.nn.Module):
""" """
Creates and trains a basic feedforward neural network. Creates and trains a basic feedforward neural network.
""" """
#
def __init__(self, input_size, hidden_size, output_size): def __init__(self, input_size, hidden_size, output_size):
""" Sets up all basic elements of NN. """ """ Sets up all basic elements of NN. """
super(Feedforward, self).__init__() super(FeedForward, self).__init__()
self.input_size = input_size self.input_size = input_size
self.hidden_size = hidden_size self.hidden_size = hidden_size
self.output_size = output_size self.output_size = output_size
self.fc1 = torch.nn.Linear(self.input_size, self.hidden_size) self.fc1 = torch.nn.Linear(self.input_size, self.hidden_size)
#self.relu = torch.nn.ReLU()
self.tanh = torch.nn.Tanh() self.tanh = torch.nn.Tanh()
self.fc2 = torch.nn.Linear(self.hidden_size, self.output_size) self.fc2 = torch.nn.Linear(self.hidden_size, self.output_size)
self.sigmoid = torch.nn.Sigmoid() self.sigmoid = torch.nn.Sigmoid()
@ -35,131 +28,100 @@ class Feedforward(torch.nn.Module):
def forward(self, x): def forward(self, x):
""" Computes output from a given input x. """ """ Computes output from a given input x. """
hidden = self.fc1(x) hidden = self.fc1(x)
#relu = self.relu(hidden)
tanh = self.tanh(hidden) tanh = self.tanh(hidden)
#output = self.fc2(relu)
output = self.fc2(tanh) output = self.fc2(tanh)
output = self.softmax(output) output = self.softmax(output)
return output return output
def read_data(self):
"""" Reads in training and test data and converts it to proper format. """
if __name__=='__main__': self.X_train_, self.y_train_, self.X_test_ = read_csv_nn()
""" Reads in the data, then trains and evaluates the neural network. """ yclass = np.array([(x[1] == 1) + 2 * (x[2] == 1) for x in self.y_train_])
X_train, y_train, X_test = read_csv_nn()
# balance classes
yclass = np.array([(x[1]==1)+2*(x[2]==1) for x in y_train])
is0 = yclass == 0 is0 = yclass == 0
is1 = yclass == 1 is1 = yclass == 1
is2 = yclass == 2 is2 = yclass == 2
X0 = torch.FloatTensor(X_train[is0]) self.X0 = torch.FloatTensor(self.X_train_[is0])
X1 = torch.FloatTensor(X_train[is1]) self.X1 = torch.FloatTensor(self.X_train_[is1])
X2 = torch.FloatTensor(X_train[is2]) self.X2 = torch.FloatTensor(self.X_train_[is2])
y0 = torch.LongTensor(np.zeros((sum(is0),))) self.y0 = torch.LongTensor(np.zeros((sum(is0),)))
y1 = torch.LongTensor(np.ones((sum(is1),))) self.y1 = torch.LongTensor(np.ones((sum(is1),)))
y2 = torch.LongTensor(2*np.ones((sum(is2),))) self.y2 = torch.LongTensor(2 * np.ones((sum(is2),)))
l0 = sum(is0) self.l0 = sum(is0)
l1 = sum(is1) self.l1 = sum(is1)
l2 = sum(is2) self.l2 = sum(is2)
p0 = torch.randperm(l0)
p1 = torch.randperm(l1) def fit(self, epochs=100, batch_size=16, lr=0.01, samples0=1000, samples1=1000, samples2=1000):
p2 = torch.randperm(l2) """ Trains model, using cross entropy loss and SGD optimizer. """
p = torch.randperm(3000) self.criterion = torch.nn.CrossEntropyLoss()
X_train = torch.cat((X0[p0][:1000], X1[p1][:1000], X2[p2][:1000]))[p] self.optimizer = torch.optim.SGD(self.parameters(), lr)
y_train = torch.cat((y0[:1000], y1[:1000], y2[:1000]))[p] self.samples0 = samples0
self.samples1 = samples1
#X_train = torch.FloatTensor(X_train) self.samples2 = samples2
X_test = torch.FloatTensor(X_test)
#y_train_ = torch.FloatTensor(y_train) model.eval() # put into eval mode
#y_train = torch.max(torch.FloatTensor(y_train_),1)[1]
# initialize training data
model = Feedforward(28, 9, 3) self.shuffle()
criterion = torch.nn.CrossEntropyLoss() y_pred = self.forward(self.X_train)
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01) before_train = self.criterion(y_pred, self.y_train)
model.eval()
y_pred = model(X_train)
before_train = criterion(y_pred, y_train)
print('Test loss before training', before_train.item()) print('Test loss before training', before_train.item())
l = 3000 # X_train.shape[0] # setup for batches
batch_indices = list(zip(list(range(0,l,16))[:-1], list(range(16,l,16))))# + [(l-l%16,l)] l = self.samples0 + self.samples1 + self.samples2 # total length
batch_indices = list(zip(list(range(0, l, batch_size))[:-1], list(range(16, l, batch_size))))
batch_indices[-1] = (batch_indices[-1][0], l) batch_indices[-1] = (batch_indices[-1][0], l)
# train model # train model
model.train() self.train() # put into training mode
epochs = 100
for epoch in range(epochs): for epoch in range(epochs):
batch = 0 batch = 0
for a, b in batch_indices: for a, b in batch_indices:
optimizer.zero_grad() self.optimizer.zero_grad()
# forward pass # forward pass
y_pred = model(X_train[a:b]) y_pred = model(X_train[a:b])
loss = criterion(y_pred, y_train[a:b]) loss = self.criterion(y_pred, self.y_train[a:b])
print('Epoch {}, batch {}: train loss: {}'.format(epoch, batch, loss.item()))
# backward pass # backward pass
loss.backward() loss.backward()
optimizer.step() self.optimizer.step()
batch += 1 batch += 1
# get loss following epoch # get loss following epoch
y_pred = model.forward(X_train) y_pred = self.forward(self.X_train)
loss = criterion(y_pred, y_train) loss = self.criterion(y_pred, self.y_train)
print('Epoch {}: train loss: {}'.format(epoch, loss.item())) print('Epoch {}: train loss: {}'.format(epoch, loss.item()))
# shuffle dataset # shuffle dataset
#p = torch.randperm(l) self.shuffle()
#X_train = X_train[p]
#y_train = y_train[p]
p0 = torch.randperm(l0)
p1 = torch.randperm(l1)
p2 = torch.randperm(l2)
p = torch.randperm(3000)
X_train = torch.cat((X0[p0][:1000], X1[p1][:1000], X2[p2][:1000]))[p]
y_train = torch.cat((y0[:1000], y1[:1000], y2[:1000]))[p]
model.eval()
y_pred = model.forward(X_train)
after_train = criterion(y_pred, y_train)
print('Training loss after training' , after_train.item())
## reload the data to get original order # display final loss
#X_train, y_train, X_test = read_csv_nn() self.eval() # back to eval mode
#X_train = torch.FloatTensor(X_train) y_pred = self.forward(self.X_train)
#X_test = torch.FloatTensor(X_test) after_train = self.criterion(y_pred, self.y_train)
#y_train_ = torch.FloatTensor(y_train) print('Training loss after training', after_train.item())
#y_train = torch.max(torch.FloatTensor(y_train_),1)[1]
# get dev set to make predictions def predict(self):
#X_dev, y_dev = read_csv_nn_dev() """ Generates predictions from model, using test data. """
#X_dev = torch.FloatTensor(X_dev)
#y_dev_pre = torch.FloatTensor(y_dev)
#y_dev = torch.max(torch.FloatTensor(y_dev_pre),1)[1]
# post-process to get predictions & get back to np format # post-process to get predictions & get back to np format
y_pred = model.forward(X_test) y_pred = self.forward(self.X_test)
y_pred_np = y_pred.detach().numpy() y_pred_np = y_pred.detach().numpy()
predmax = np.amax(y_pred_np, axis=1) predmax = np.amax(y_pred_np, axis=1)
preds = 1*(y_pred_np[:,1]==predmax) + 2*(y_pred_np[:,2]==predmax) self.preds = 1 * (y_pred_np[:, 1] == predmax) + 2 * (y_pred_np[:, 2] == predmax)
#y_dev_ = y_dev.detach().numpy() self.probs = y_pred.detach().numpy()
# create confusion matrix def shuffle(self):
#cm = confusion_matrix(y_dev_, preds) """ Samples and shuffles training data. """
#print(cm)
# create permutations for shuffling
# save predictions p0 = torch.randperm(self.l0)
df = pd.DataFrame() p1 = torch.randperm(self.l1)
df['preds'] = preds p2 = torch.randperm(self.l2)
#df['true'] = y_dev_ n = self.l0 + self.l1 + self.l2
probs = y_pred.detach().numpy() p = torch.randperm(n)
df['pr0'] = probs[:,0]
df['pr1'] = probs[:,1] # sample and shuffle data
df['pr2'] = probs[:,2] self.X_train = \
#df['correct'] = df.preds==df.true torch.cat((self.X0[p0][:self.samples0], self.X1[p1][:self.samples1], self.X2[p2][:self.samples2]))[p]
df.to_csv('/Users/iriley/code/machine_learning/lab2020/y_pred_model2.csv', index=False) self.y_train = torch.cat((self.y0[:self.samples0], self.y1[:self.samples1], self.y2[:self.samples2]))[p]

@ -0,0 +1,9 @@
from classifier.nn_ff import FeedForward
model = FeedForward(28, 9, 3)
model.fit()
model.predict()
Loading…
Cancel
Save