Multi-class classification variable set up (torch)

titoniubo: <p>I’d like to prepare variables to run a multi class classification.</p>
<p>Note I try replicating the model shown in this <a href="https://discuss.pytorch.org/t/multi-class-classification/47565/5">post</a>, where <a class="mention" href="/u/oli">@Oli</a> , <a class="mention" href="/u/tymokvo">@tymokvo</a> , <a class="mention" href="/u/god_sp33d">@god_sp33d</a>  and <a class="mention" href="/u/rojin">@Rojin</a> contributed.</p>
<p>I have 10 variables split as (X_train,X_test) and 1 target variable split as (Y_train, Y_test).</p>
<p>My target/label is a 3 class (1,2,3)</p>
<p>I thought I would prepare them as follows:</p>
<pre><code class="lang-auto">&gt; X_train = np.array(X_train, dtype=np.float32)
&gt; X_test = np.array(X_test, dtype=np.float32)
&gt; Y_train = np.array(Y_train, dtype=np.float32)
&gt; y_test = np.array(y_test, dtype=np.float32)
</code></pre>
<p>and then convert them to tensor as follows:</p>
<pre><code class="lang-auto">train_x = torch.from_numpy(np.asanyarray(X_train)).requires_grad_()
train_y = torch.LongTensor(Y_train).long().requires_grad()
test_x = torch.from_numpy(np.asanyarray(X_test)).requires_grad_()
test_y = torch.LongTensor(y_test).long().requires_grad()
</code></pre>
<p>unfortunately, when running code above I get as result:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/9/09c9f37cd284943f165e1472bf07538f50b59379.png" alt="Screen Shot 2020-01-14 at 14.00.00" data-base62-sha1="1oAYtTPkKhiSgEJAGEtulSrVRy1" width="676" height="257"></p>
<p>I would be very grateful if any one could shed some light on this.</p>
<p>Sincerely,</p>

kabilan: <p>Remove long function</p>

titoniubo: <p>Thanks <a class="mention" href="/u/kabilan">@kabilan</a> , now it shows the following error:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/2/4217ec2621bf50fe13254c2ea84fd303a00521d1.png" alt="Screen Shot 2020-01-14 at 15.40.01" data-base62-sha1="9qGLx6BGO8rEeck4eSG5W66sis1" width="651" height="259"></p>

KFrank: <p>Hello Kabilan!</p>
<aside class="quote no-group" data-username="titoniubo" data-post="1" data-topic="66637">
<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/titoniubo/48/19669_2.png" class="avatar"> titoniubo:</div>
<blockquote>
<p>train_y = torch.LongTensor(Y_train).long().requires_grad()</p>
</blockquote>
</aside>
<aside class="quote no-group" data-username="kabilan" data-post="2" data-topic="66637" data-full="true">
<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/kabilan/48/19572_2.png" class="avatar"> kabilan:</div>
<blockquote>
<p>Remove long function</p>
</blockquote>
</aside>
<p>No, this isn’t correct.  First, calling the <code>.long()</code> method on a<br>
<code>LongTensor</code> is a no-op (does nothing).  Second, the <code>train_y</code><br>
are to be used as categorical class labels, so they have to be<br>
a <code>LongTensor</code>.</p>
<p>Best.</p>
<p>K. Frank</p>

KFrank: <p>Hi Josep!</p>
<p>First, could you clarify what version of pytorch you are using?</p>
<aside class="quote no-group quote-modified" data-username="titoniubo" data-post="1" data-topic="66637" data-full="true">
<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/titoniubo/48/19669_2.png" class="avatar"> titoniubo:</div>
<blockquote>
<p>I’d like to prepare variables to run a multi class classification.</p>
<p>…</p>
<p>I have 10 variables split as (X_train,X_test) and 1 target variable split as (Y_train, Y_test).</p>
<p>My target/label is a 3 class (1,2,3)</p>
<pre><code class="lang-auto">&gt; X_train = np.array(X_train, dtype=np.float32)
&gt; X_test = np.array(X_test, dtype=np.float32)
&gt; Y_train = np.array(Y_train, dtype=np.float32)
&gt; y_test = np.array(y_test, dtype=np.float32)
</code></pre>
</blockquote>
</aside>
<p>What type of data structure are your <code>X_train</code>, <code>Y_train</code>, etc.,<br>
before you wrap them in numpy arrays?</p>
<aside class="quote no-group">
<blockquote>
<p>and then convert them to tensor as follows:</p>
<pre><code class="lang-auto">train_x = torch.from_numpy(np.asanyarray(X_train)).requires_grad_()
train_y = torch.LongTensor(Y_train).long().requires_grad()
test_x = torch.from_numpy(np.asanyarray(X_test)).requires_grad_()
test_y = torch.LongTensor(y_test).long().requires_grad()
</code></pre>
</blockquote>
</aside>
<p>Just to clean things up a little, I would (try to) convert directly<br>
from your <em>original</em> <code>X_train</code>, etc., to pytorch tensors.</p>
<aside class="quote no-group">
<blockquote>
<p>unfortunately, when running code above I get as result:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/9/09c9f37cd284943f165e1472bf07538f50b59379.png" alt="Screen Shot 2020-01-14 at 14.00.00" data-base62-sha1="1oAYtTPkKhiSgEJAGEtulSrVRy1" width="676" height="257"></p>
</blockquote>
</aside>
<p>(Please, in general, post text rather than screen-shots.  It can then<br>
be searched and copy-pasted.)</p>
<aside class="quote no-group">
<blockquote>
<p>I would be very grateful if any one could shed some light on this.</p>
</blockquote>
</aside>
<p>The main issue is that you neither need nor want gradients for<br>
your data (neither training nor test).  You use gradients for the<br>
parameters of your model.</p>
<p>When you “train” your model you adjust the parameters of your<br>
model so that they do a better job classifying your inputs.  The<br>
whole idea behind pytorch’s autograd facility is that it calculates<br>
for you the gradients of your loss function with respect to your<br>
model parameters so that you can use an optimization algorithm<br>
such as gradient descent to “optimize” (change) those parameters<br>
so that your model produces a lower loss function.</p>
<p>You <em>don’t</em> change the data you are training (or testing) with.</p>
<p>If you construct your model in the “standard” way, your model<br>
parameters will automatically be flagged with<br>
<code>requires_grad = True</code>.</p>
<p>Note, a <code>FloatTensor</code> is (approximately) continuous, so it makes<br>
sense to “do calculus” on it, i.e., calculate gradients.</p>
<p>But a <code>LongTensor</code> is made up of discrete (long) integers, so it<br>
isn’t natural to “do calculus” on it, so pytorch doesn’t support<br>
<code>requires_grad = True</code> for <code>LongTensor</code>s.</p>
<p>You should make your <code>train_x</code> and <code>test_x</code> “ordinary”<br>
<code>FloatTensor</code>s (that will default to <code>requires_grad = False</code>),<br>
and your <code>train_y</code> and <code>test_y</code> <code>LongTensors</code> (without<br>
<code>.requires_grad_()</code>) (that pytorch will force to have<br>
<code>requires_grad = False</code>).</p>
<p>Perhaps it would make sense to write a complete, runnable<br>
script that generates some toy (random, if you choose) data,<br>
packages that data in the appropriate pytorch tensors,<br>
build a simple (one or two-layer) model, and pass one<br>
batch of your input data through the model.</p>
<p>If you have trouble getting that to run, print out the types and<br>
shapes of some of your tensors along the way, and post the<br>
complete, runnable script, along with the output, and tell us<br>
what problems you are having.</p>
<p>Good luck.</p>
<p>K. Frank</p>

titoniubo: <p><a class="mention" href="/u/kfrank">@KFrank</a>, it works!</p>
<p>Thank you very much for your time and explanation.</p>
<p>From now onwards, following your advice I shall post text rather than screen-shots.</p>