I have a question about how to write optimization code.
That is I want to write a conditional random field my own.I need to optimize parameter for datalikelihood by pytorch gradient.
The sketch of loss is below:assume theta in the code is  the parameter I want to optimize:
for data in dataset:
    #here theta need to compute grad
   feature = theta.matmul(features) 
    #here is some function of feature(a function of theta) 
    #we add this function for all data as the loss
   likelihood_loss += some_function(fearure)

likelihood_loss.backward()

Now I want to backprop  the sum of likelihood loss to optimize paramer theta.

My question is I don’t know how to define loss function,how to initialize class of each variable to let the gradient being computed correctly.
For example which one is defined as Variable,which are torch.Tensor,which are list