The input is rgb-d image with the corresponding label and regression data.
How to make a model have the output of regression and classification?
This is my program concept:
#### program concept ####
# 4 class, 3 regression
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden): super(Net, self).__init__() self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer self.out = torch.nn.Linear(n_hidden, 4) # output layer self.out2 = torch.nn.Linear(n_hidden, 3) # output layer def forward(self, x): x = F.relu(self.hidden(x)) # activation function for hidden layer x_out = self.out(x) x_out2 = self.out2(x) return x_out, x_out2
net = Net(n_feature=4, n_hidden=1024) # define the network
optimizer = torch.optim.Adam(net.parameters(), lr=0.02)
loss_func = torch.nn.CrossEntropyLoss() # the target label is NOT an one-hotted
loss_func2 = torch.nn.MSELoss() # this is for regression mean squared loss
for rgbd, y, y2 in data_loader_image_rgbd:
optimizer.zero_grad() # clear gradients
pre_class, pre_regression = net(rgbd) # input x and predict based on x
loss = loss_func(pre_class, y) # must be (1. nn output, 2. target)
loss2 = loss_func2(pre_regression, y) # must be (1. nn output, 2. target)
loss_total = loss +loss2
loss_total.backward() # backpropagation, compute gradients
optimizer.step() # apply gradients
#### program concept ####
Can I update my network parameters correctly? If not, how can I change it?
Sorry, my English is very poor, I hope you understand.
thanks!