from tkinter import Variable
import pandas as pd
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import torch.optim as optim
import matplotlib.pyplot as plt
import time
import sys
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
#Load Training.csv
trainingData = pd.read_csv('training.csv')
#The training data labels the data as either s or b for signal or background
#we use this mappings lambda to make s be 1 and b be 0
mappings = {
's': 1,
'b': 0
}
trainingData['Label'] = trainingData['Label'].apply(lambda x: mappings[x])
trainingData = trainingData.where(trainingData > -998.0, 0)
sig = nn.Sigmoid()
model = nn.Sequential( nn.Linear(31, 60),
nn.ReLU(),
nn.Linear(60, 60),
nn.Linear(60, 60),
nn.Linear(60, 60),
nn.ReLU(),
nn.Linear(60, 60),
nn.Linear(60, 60),
nn.ReLU(),
nn.Linear(60, 1))
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
#initializing some variables for plotting loss over epoch
yList = []
xList = []
yListlater = []
xListlater = []
laterInt = 1
inputData = torch.tensor(trainingData.drop(['Weight', 'Label'], axis=1).values, dtype=float)
outputData = torch.tensor(trainingData['Label'].values, dtype=float).reshape(250000, 1)
listIn = torch.chunk(inputData, 25000, dim=0)
listOut = torch.chunk(outputData, 25000, dim=0)
epochs = 100
for name, param in model.named_parameters():
print(name, param.grad)
print(model)
print(model[0].weight)
for i in tqdm(range(epochs)):
x = listIn[i]
y = listOut[i]
outData = model(x.float())
optimizer.zero_grad()
loss = criterion(outData, target=y)
loss.backward()
optimizer.step()
if (i % 1 == 0):
yList.append(loss.item())
xList.append(i)
print(model[0].weight)
plt.plot(xList, yList)
plt.xlabel('epoch')
plt.ylabel('running_loss')
plt.title('loss vs epoch')
plt.show()
Here is the code, I have been trying to figure out why nothing is changing for a couple days now and no luck.
Here is the output as well.
0.weight None
0.bias None
2.weight None
2.bias None
3.weight None
3.bias None
4.weight None
4.bias None
6.weight None
6.bias None
7.weight None
7.bias None
9.weight None
9.bias None
Sequential(
(0): Linear(in_features=31, out_features=60, bias=True)
(1): ReLU()
(2): Linear(in_features=60, out_features=60, bias=True)
(3): Linear(in_features=60, out_features=60, bias=True)
(4): Linear(in_features=60, out_features=60, bias=True)
(5): ReLU()
(6): Linear(in_features=60, out_features=60, bias=True)
(7): Linear(in_features=60, out_features=60, bias=True)
(8): ReLU()
(9): Linear(in_features=60, out_features=1, bias=True)
)
Parameter containing:
tensor([[ 0.1255, -0.0629, -0.0803, …, 0.0183, -0.1115, 0.0929],
[ 0.0016, -0.1008, 0.1053, …, 0.1485, -0.0020, -0.0183],
[ 0.0559, -0.0457, 0.0267, …, 0.0708, 0.0448, -0.0680],
…,
[ 0.0056, 0.1003, 0.0257, …, -0.1627, 0.0517, -0.0623],
[-0.0674, 0.1298, -0.0035, …, 0.1628, -0.0131, -0.1417],
[-0.0073, 0.1473, -0.0778, …, 0.1646, 0.0768, 0.1202]],
requires_grad=True)
[In here is the training loop]
Parameter containing:
tensor([[ 0.1255, -0.0629, -0.0803, …, 0.0183, -0.1115, 0.0929],
[ 0.0016, -0.1008, 0.1053, …, 0.1485, -0.0020, -0.0183],
[ 0.0559, -0.0457, 0.0267, …, 0.0708, 0.0448, -0.0680],
…,
[ 0.0056, 0.1003, 0.0257, …, -0.1627, 0.0517, -0.0623],
[-0.0674, 0.1298, -0.0035, …, 0.1628, -0.0131, -0.1417],
[-0.0073, 0.1473, -0.0778, …, 0.1646, 0.0768, 0.1202]],