I found a method but I think that it is not the best way to solve my question.
for t in range(2000):
prediction = net(x)
for i in range(0,Number_total_images):
    prediction[i] = sum(net(x)[Species_number_image[0][i] : Species_number_image[0][i+1]])

loss = torch.sqrt(loss_func(prediction[0:Number_total_images], y))     # must be (1. nn output, 2. target)

optimizer.zero_grad()   # clear gradients for next train
loss.backward()         # backpropagation, compute gradients
optimizer.step()        # apply gradients