I am trying to input non-image data into CNN as matrices. The data is approximately 24 x 20 where 20 is the batch size. This is stock market data where the should be 0 - Sell or 1 - Buy, however for all the data I input into the model, I am getting the same output on each run, sometimes 0 or sometimes 1. I tried removing the clamp at the end and playing around with activation functions however it didn't work and got the same output for all data.
trainData,trainDataOTP,testData,testDataOTP,trainDataLength,testDataLength,parameters = readyData(trainingDataPercentage,numberOfParameters,numberOfCompanies,fileName)
fullDataForTraining = True # set true when testing new batch/parameter size
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels=parameters,out_channels=conv1Output,kernel_size=kernel_size)
self.pool1 = nn.MaxPool2d(poolSize1, stride1_size)
self.conv2 = nn.Conv2d(conv1Output, conv2Output, kernel_size)
self.pool2 = nn.MaxPool2d(poolSize2, stride2_size)
self.conv3 = nn.Conv2d(conv2Output, conv3Output, kernel_size)
self.pool3 = nn.MaxPool2d(poolSize3, stride3_size)
self.conv4 = nn.Conv2d(conv3Output, conv4Output, kernel_size)
self.pool4 = nn.MaxPool2d(poolSize4, stride4_size)
self.conv5 = nn.Conv2d(conv4Output, conv5Output, kernel_size)
self.pool5 = nn.MaxPool2d(poolSize5, stride5_size)
self.conv6 = nn.Conv2d(conv5Output, conv6Output, kernel_size)
self.pool6 = nn.MaxPool2d(poolSize6, stride6_size)
self.conv7 = nn.Conv2d(conv6Output, conv7Output, kernel_size)
self.pool7 = nn.MaxPool2d(poolSize7, stride7_size)
self.sig = nn.Sigmoid()
def forward(self, x):
x = self.pool1(F.relu(self.conv1(x)))
x = self.pool2(F.relu(self.conv2(x)))
x = self.pool3(F.relu(self.conv3(x)))
x = self.pool4(F.relu(self.conv4(x)))
x = self.pool5(F.relu(self.conv5(x)))
x = self.sig(self.pool7(F.relu(self.conv7(x))))
x = x.clamp(0,1)
return x
model = ConvNet().to(device)
print(f'Datatype of modes: {type(model)}')
criterion = nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate,momentum=momentum)
start = time.time()
targetValue = torch.zeros(1,1).to(device=device)
lossValue = 0
count = 1
totalCount = num_epochs * numberOfCompanies * trainDataLength
if fullDataForTraining == False:
buyAcc=0
sellAcc=0
overallAccuracy=0
inputs = torch.zeros(parameters,1,batch_size).to(device=device)
outputs = torch.tensor(trainDataOTP[0,:,:batch_size]).to(device=device)
for param in range(parameters):
inputs[param] = torch.tensor(trainData[param,0,:batch_size]).float().to(device=device)
else:
outputCount = torch.zeros(1,2).to(device=device)
inputArray = torch.zeros(parameters,1,batch_size).to(device=device) # 1 for no of banks
inputs = torch.zeros(parameters,1,batch_size).to(device=device)
for bank in range(numberOfCompanies):
epochTime = time.time()
for epoch in range(num_epochs):
for date in range(trainDataLength-batch_size):
outputs = torch.tensor(trainDataOTP[0,:,date:date+batch_size]).to(device=device)
indxCount=0
for param in range(parameters):
# Forward pass
inputs[param] = torch.tensor(trainData[param,bank,date:date+batch_size]).float().to(device=device)
indxCount +=1
predicted_output = model(inputs)
for INDEX in range(batch_size):
loss = criterion(predicted_output[0,0,INDEX].float(),outputs[bank,INDEX].float())
outputCount[0,predicted_output[0,0,INDEX].int()] +=1
optimizer.zero_grad()
loss.backward()
optimizer.step()
dataToBeProcessed = trainDataLength-date-1
print('Finished Training')
PATH = './ModelParameters/' + fileName + '_' + bank + '.pth'
torch.save(model.state_dict(), PATH)
with torch.no_grad():
n_correct = 0
n_samples = 0
n_class_correct = [0 for i in range(2)]
n_class_samples = [0 for i in range(2)]
outputs = torch.tensor(testDataOTP[date:date+batch_size,:]).float().to(device=device)
for bank in range(numberOfCompanies):
for date in range(trainDataLength-batch_size):
outputs = torch.tensor(testDataOTP[0,:,date:date+batch_size]).to(device=device)
for param in range(parameters):
# for rows in range(testDataLength-batch_size):
inputs = torch.tensor(testData[param,bank,:batch_size]).float().to(device=device)
predicted_output = model(inputs)
for indx in batch_size:
if predicted_output[0,bank,indx].int() == outputs[bank,indx]:
n_correct +=1
n_class_correct[predicted_output[0,bank,indx].int()]+=1
n_class_samples[predicted_output[0,bank,indx].int()] +=1
n_samples += 1
acc = 100.0 * n_correct / n_samples
print(f'Accuracy of the network for company - {bank} = {acc:.4f} %')
overallAccuracy = acc
for i in range(2):
acc = 100.0 * n_class_correct[i] / n_class_samples[i]
print(f'Accuracy of {expectedOutputs[i]} = {acc:.4f} %')
if i==0:
sellAcc = acc
else:
buyAcc = acc