Dear Community
I would like to build a simple MLP that assigns class A or B to a given input.
The input data are 128d feature representations extracted from FaceNet.
Input data (X): shape=(23445, 128), dtype=float64
The target data are the binary class labels 0 or 1 (denotes class A or B)
Target data (y): shape=(23445, 1), array([0, 0, 1, …, 1, 1, 1]), dtype=int64
# Network architecture
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(128, 100)
self.fc2 = nn.Linear(100, 2)
def forward(self, x):
x = x.view(-1, 128)
x = F.relu(self.fc1(x))
x = F.softmax(self.fc2(x))
return x
net = Net()
# Create 2d array from target data
targets = np.empty((len(y), 2))
for i in range(0, len(y)):
if(y[i] == 0):
targets[i, 0] = 1
targets[i, 1] = 0
else:
targets[i, 0] = 0
targets[i, 1] = 1
Result
array([[1., 0.],
[1., 0.],
[0., 1.],
...,
[0., 1.],
[0., 1.],
[0., 1.]])
# Convert inputs, targets to tensor
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
inputs, targets = torch.from_numpy(X), torch.from_numpy(targets)
inputs, targets = inputs.type(torch.FloatTensor),targets.type(torch.FloatTensor)
inputs = inputs.to(device)
targets = targets.to(device)
net.to(device)
net.train()
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001)
running_loss = 0.0
# zero the parameter gradients
optimizer.zero_grad()
# forward
# inputs [23445x128]
# outputs [23445x2]
outputs = net(inputs)
# Result Forward Pass
tensor([[ 0.4894, 0.5106],
[ 0.4900, 0.5100],
[ 0.4897, 0.5103],
...,
[ 0.4813, 0.5187],
[ 0.4825, 0.5175],
[ 0.4889, 0.5111]], device='cuda:0')
# outputs [23445x2]
# targets [23445x2]
# batch size 23445
loss = criterion(outputs, targets)
print(loss)
loss.backward()
optimizer.step()
Result Loss
tensor(0.7241, device='cuda:0')
I hope you have some advice how to approach this problem.
Thank you very much