Hi,
I have been trying to classify type of signals based on their values. My input is of Nx32 matrix, where N is the number of samples and 32 is the number of features of signal.
I am able to implement CNN code using pytorch but the output is always constant (0 for all inputs).Here is the code that i have been using. I am unable to figure what is going wrong?
Any help is appreciated
from future import division
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
class Net(nn.Module):
def init(self):
super(Net, self).init()
self.conv1 = nn.Conv1d(1, 6, 5)
self.conv2 = nn.Conv1d(6, 16, 5)
self.fc1 = nn.Linear(384, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 2)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
def training(train_data,train_labels,test_data,test_labels):
net = Net()
x = Variable(torch.Tensor(train_data), requires_grad=False)
y = Variable(torch.Tensor(train_labels), requires_grad=False)
x_2 = Variable(torch.Tensor(test_data), requires_grad=False)
y_2 = Variable(torch.Tensor(test_labels), requires_grad=False)
# criterion = nn.MSELoss()
# optimizer = optim.SGD(net.parameters(), lr=0.00001, momentum=0.01)
optimizer = optim.Adam(net.parameters())
criterion = nn.MultiLabelSoftMarginLoss()
new = x.view(-1,1,32)
new_x_2 = x_2.view(-1,1,32)
num_epochs = 10
for epoch in range(10):
losses=[]
optimizer.zero_grad()
outputs = net(new)
loss = criterion(outputs,y)
loss.backward()
optimizer.step()
losses.append(loss.data.mean())
print('[%d/%d] Loss: %.3f' % (epoch+1, num_epochs, loss.data.mean()))
net.eval()
outputs = net(new_x_2)
_, predicted = torch.max(outputs.data, 1)
print(predicted)
ar=0
for i in predicted:
if(i==1):
ar=ar+1
print(ar)