Is it professional when dealing with the softmax layer in mobile

username: MarcSteven
cooked: <p>Is it possible to do the task of softmax layer in pytorch, I know Tensorflow can do it</p>

username: ptrblck
cooked: <p>Could you explain your use case and your concerns a bit?<br>
Which task would you like to use with a softmax layer?<br>
PyTorch has a softmax implementation, but I’m not sure I understand your issue completely.</p>

username: MarcSteven
cooked: <p>I mean How to apply softmax layer in PyTorch. <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

username: ptrblck
cooked: <p>You can apply it as any other layer:</p>
<pre><code class="lang-python">softmax = nn.Softmax(dim=1)
x = torch.randn(2, 10)
output = softmax(x)
</code></pre>

username: MarcSteven
cooked: <p>As the final layer? Right? <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

username: ptrblck
cooked: <p>It depends on your use case and e.g. the loss function you are using.<br>
<code>nn.CrossEntropyLoss</code> expects raw logits for example, while <code>nn.NLLLoss</code> expects log probabilities (<code>F.log_softmax</code> applied to the last layer).</p>
<p>Without some more information it is hard to answer your general questions.</p>

username: MarcSteven
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/d/fdf5094b430ebac03acf8f8c22d3376f5b081826.png" data-download-href="https://discuss.pytorch.org/uploads/default/fdf5094b430ebac03acf8f8c22d3376f5b081826" title="09"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/d/fdf5094b430ebac03acf8f8c22d3376f5b081826_2_388x500.png" alt="09" data-base62-sha1="AeBMQD5evY0LILHZcLCUnRvnqPc" width="388" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/d/fdf5094b430ebac03acf8f8c22d3376f5b081826_2_388x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/d/fdf5094b430ebac03acf8f8c22d3376f5b081826_2_582x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/d/fdf5094b430ebac03acf8f8c22d3376f5b081826_2_776x1000.png 2x" data-dominant-color="404041"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">09</span><span class="informations">954×1228 48 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> have softmaxlayer<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/7/37e81cb31ff870efb3047da2b2b0370efbdae595.png" data-download-href="https://discuss.pytorch.org/uploads/default/37e81cb31ff870efb3047da2b2b0370efbdae595" title="50"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/7/37e81cb31ff870efb3047da2b2b0370efbdae595_2_525x500.png" alt="50" data-base62-sha1="7YzyDVgynnZSWELibiffxAfm3Jz" width="525" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/7/37e81cb31ff870efb3047da2b2b0370efbdae595_2_525x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/7/37e81cb31ff870efb3047da2b2b0370efbdae595_2_787x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/7/37e81cb31ff870efb3047da2b2b0370efbdae595_2_1050x1000.png 2x" data-dominant-color="404040"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">50</span><span class="informations">1470×1398 142 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> Without softmax layer, in mobile I need to do the task about the softmax layer. Now I wanna accomplished the task about the softmax layer in model, I searched the doc and then cannot find the documents to tell me how to do it. By the way I have  a doubt about convert input type parameters, how to convert the multiArray to the image, I did a project about image classifier ,Thank for your replies! <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

username: ptrblck
cooked: <p>How did you define this model?<br>
Did you export a PyTorch model to ONNX or did you take another path?</p>

username: MarcSteven
cooked: <p>Yeah I convert the Model of PyTorch to onnx and then convert it to CoreML model. Based on the demo apple offered the model deal with the softmax layer and then only show the result. But the model only needs to deal with the multiArray to accomplish softmax layer work.<br>
I mean any links to deal with the case.</p>
<ul>
<li>1,Change the input and output type - change input to image and then convert output to dictionary({string:float}</li>
</ul>

username: ptrblck
cooked: <p>Could you post a link to the demo, please?</p>

username: MarcSteven
cooked: <p>I don’t have a demo,I received one model from PyTorch. and then it doesn’t do softmax layer task. Now I want to do it in Pytorch model.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="MarcSteven" data-post="9" data-topic="65424">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/a6a055/48.png" class="avatar"> MarcSteven:</div>
<blockquote>
<p>Based on the demo apple offered the model deal with the softmax layer and then only show the result.</p>
</blockquote>
</aside>
<p>I was referring to this demo you mentioned.<br>
Based on the graph you’ve posted I assume it’s a CoreML graph, as these operations do not seem to come from PyTorch directly.</p>
<p>Could you explain your use case completely, i.e. what kind of model or code are you using now and what are you trying to achieve?</p>

username: MarcSteven
cooked: <p>I used mlmodel from PyTorch- convert PyTorch to onnx and then convert it to mlmodel.<br>
I don’t want to do the task of softmax layer in the field of mobile, so I hope let Machine learning engineer to change the code to do the softmax layer .<br>
For instance how to change the input and output type.I did image classifier .</p>

username: ptrblck
cooked: <p>Could you post a (minimal) code snippet to reproduce your workflow or link to a repository?<br>
I’m currently unsure how to help without seeing what you actually did.</p>
<p>If you cannot publish the model, just create a dummy model with a similar architecture.</p>

username: MarcSteven
cooked: <p>Sorry，perhaps my description is fuzzy.<br>
For me I only want to solve this issue.<br>
The result of precondition is multiArray, when I received the results, I need to deal with it on the mobile. For me I don’t want to do the task, so I know it’s best practices to do in the model.</p>

username: MarcSteven
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> I mean don’t do the softmax layer in the mobile, is it best practices to in the pytorch?</p>

username: ptrblck
cooked: <p>It depends on your actual use case and the applied loss function.<br>
For a multi-class classification problem, you wouldn’t use a softmax layer as your activation output, but pass the raw logits into <code>nn.CrossEntropyLoss</code> (or use <code>F.log_softmax</code> and <code>nn.NLLLoss</code> alternatively).<br>
That being said, I’m still unsure, how the graphs were created and if any conversion library added these softmax layers or where these layers are generally coming from.</p>

username: MarcSteven
cooked: <p>The project is a image classifier -emotion.<br>
And then I want to convert the result to dict , but unfortunately the forward() function cannot return dict, how to do it</p>

username: ptrblck
cooked: <p>You can return <code>dicts</code> in your <code>forward</code> method:</p>
<pre><code class="lang-python">class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.fc1 = nn.Linear(1, 1)
        self.fc2 = nn.Linear(1, 1)
        
    def forward(self, x):
        x1 = self.fc1(x)
        x2 = self.fc2(x)
        return {'x1': x1, 'x2': x2}

model = MyModel()
out_dict = model(torch.randn(1, 1))
print(out_dict)
&gt; {'x1': tensor([[0.5168]], grad_fn=&lt;AddmmBackward&gt;), 'x2': tensor([[0.2288]], grad_fn=&lt;AddmmBackward&gt;)}
</code></pre>

username: MarcSteven
cooked: <p>Yeah absolutely it works.</p>