Data likelihood loss optimization

skyline: <p>I have a question about how to write optimization code.<br>
That is I want to write a conditional random field my own.I need to optimize parameter for datalikelihood by pytorch gradient.<br>
The sketch of loss is below:assume <code>theta</code> in the code is  the parameter I want to optimize:</p>
<pre><code class="lang-auto">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()
</code></pre>
<p>Now I want to backprop  the sum of likelihood loss to optimize paramer theta.</p>
<blockquote>
<p>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.<br>
For example which one is defined as <code>Variable</code>,which are <code>torch.Tensor</code>,which are <code>list</code></p>
</blockquote>

Kushaj: <p>A loss function is just a python function. You can define a loss function as simple as</p>
<pre><code class="lang-python">def my_loss(pred, actual):
    return pred-actual
</code></pre>
<h3>Forward step</h3>
<p>Start sequentially from your input, which would be pytorch tensors (if they are of some other form, convert them to pytorch tensors). Now do all your operations that you want. Compute the loss, which is just a function.</p>
<h3>Backward step</h3>
<p>To compute the gradients use loss.backward() and you got the gradients for your variables. If you only want to update a specific tensor, set requires_grad=True for that tensor and requires_grad=False for all other tensors.<br>
Now to update your parameter you can use your desired optimization algorithm.</p>
<pre><code class="lang-python">def my_optimization(param):
    # To get the gradient
    grad = param.grad
    
    # SGD
    param = param - alpha * grad
</code></pre>

skyline: <aside class="quote no-group" data-username="Kushaj" data-post="2" data-topic="64040">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kushaj/48/58838_2.png" class="avatar"> Kushaj:</div>
<blockquote>
<p>to</p>
</blockquote>
</aside>
<p>I know this block, my problem here is how to initialize variables theta,likelihood here.<br>
For example<code> likelihood = []</code> or something like <code>likelihood = torch.Tensor()</code> or <code>likelihood = Variable(..)</code>  or <code>likelihood = Parameter(...)</code></p>

Kushaj: <p>Just use torch.tensor(0) if it a single value?</p>

111137: <p>Hi skyline-san,</p>
<p>Do you want to build your custom loss function? PyTorch has standard loss functions, google is your friend.</p>
<p>Tensor is used for variable which is on a path of your neural network (means that non-linear function between linear algebra mappings), implies that should be <strong>derivative</strong> in case of parameter. Variable is generally used for variable which is not tensor such as input vector.</p>
<p>If you do so, and the “features” means a set of parameter, then you can use nn.Parameter or nn.ParameterList for your custom layer.</p>
<p>If you do not aim so, then you can use not only loss function but also standard layer architectures such as convolution, fully-connected layers, etc, these have its own parameters.</p>
<p>You do not need to concern the gradient matter (“baseline” optimizer) because PyTorch supports auto gradient ability (back-prop is automatically constructed for your network).</p>
<p>I hope this comment help you.</p>
<p>Best,<br>
S.Takano</p>

skyline: <p>What strange is the right hand side of<br>
<code>likelihood_loss += some_function(fearure)</code><br>
is something like <code>tensor(-3.9759, grad_fn=&lt;SubBackward0&gt;)</code></p>
<p>When I add it to <code>likelihood_loss</code>  I get <code>tensor([])</code> not  <code>tensor(-3.9759, grad_fn=&lt;SubBackward0&gt;)</code></p>

skyline: <aside class="quote no-group" data-username="111137" data-post="5" data-topic="64040">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/111137/48/15236_2.png" class="avatar"> 111137:</div>
<blockquote>
<p>constructed</p>
</blockquote>
</aside>
<p>1.You mean tensor can be auto graded?But why there is a option that require_grad = …</p>
<p>2 I need to optimize <code>theta</code> here hence I initialize it as tensor ? can I initialize it by Parameter?  since likelihood as loss need grad hence I also initialize it as tensor?Features is something extracted from input data,I need to change it too Variable?</p>

111137: <p>Hi,</p>
<ol>
<li>
<p>Rather than tensor, on PyTorch, variable in neural network should be auto grad just like a path based on linear algebra, tensor is just like a box, 2-rank is a matrix, 1-rank is vector. Of course parameters should be auto grad in order to update it. You can seek a <strong>chain-rule</strong> in internet.</p>
</li>
<li>
<p>Before answer, I would like to confirm reason why you need to optimize the “theta” ?, and what is the “theta” ? then what is “features” you mean ?</p>
</li>
</ol>

skyline: <aside class="quote no-group" data-username="111137" data-post="8" data-topic="64040">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/111137/48/15236_2.png" class="avatar"> 111137:</div>
<blockquote>
<p>features</p>
</blockquote>
</aside>
<p>That is I have model with parameter theta,it need to get minimize loss on likelihood loss by learning some theta,hence I take “gradient descend” which need to compute gradient instead of compute it analytically I use pytorch here.you can treat feature as input data.X</p>
<p>I have go to sleep ,see you tomorrow friend</p>

111137: <p>I think that you confuse parameter and function in python script with PyTorch. Parameter is one of which a weight or a bias, and should be derivative. But at your first comment, “theta” is looked like a function name or something else. because of</p>
<pre><code class="lang-auto">feature = theta.matmul(features) 
</code></pre>
<p>Yo can do such as</p>
<pre><code class="lang-auto">self.fc = nn.Linear(RowSize, ClmSize) #2-rank FC Architecture
</code></pre>
<p>In your model definition. You can pass through non-linear function like;</p>
<pre><code class="lang-auto">pre_activation = self.fc(x) #x is input
post_activation = relu(pre_activation)
</code></pre>
<p>After this, a loss function feeds the output and can make loss value by the function, for example;</p>
<pre><code class="lang-auto">loss = criterion(post_activation, label)
loss.backward()
optimizer.step()
</code></pre>
<p>Best,<br>
S.Takano</p>