Input size dimensions (when not use images) on nn for classification

titoniubo: <p>Hello,</p>
<p>My question is regarding the <strong>input size</strong> of the classification/regression when we use n columns instead of images.</p>
<p>Let’s say I have a classification problem, for which I have 20 numerical variables (500 raws each) and 1 label with 5 possible classes. I understand the output size is 5.</p>
<p>However, I do not know what is the input class, is it 1*20= 20?</p>
<p>Most Pytorch tutorials I have found for multi-class classification use MNIST dataset, thus the input size is 28*28, but what if I have several varibles instead of pictures?</p>
<p>Thanks for your help.</p>

KFrank: <p>Hi Josep!</p>
<aside class="quote no-group" data-username="titoniubo" data-post="1" data-topic="66012" 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>Let’s say I have a classification problem, for which I have 20 numerical variables (500 raws each) and 1 label with 5 possible classes. I understand the output size is 5.</p>
<p>However, I do not know what is the input class, is it 1*20= 20?</p>
</blockquote>
</aside>
<p>Yes, this is correct – with the proviso that pytorch models expect<br>
<em>batches</em> of inputs.</p>
<p>So, if your batch size were 7, the input to your model would be a<br>
tensor of shape (7, 20).  (If you only want to feed one input sample<br>
into your model, you still have to package it as a batch with a batch<br>
size of 1; thus your input tensor would have shape (1, 20).)</p>
<p>For 20 input variables (without any spatial structure as an image<br>
would have) and 5 classes, your network could be a simple as<br>
something like:</p>
<pre data-code-wrap="python"><code class="lang-python">model = nn.Sequential(
    nn.Linear (20, 50),
    nn.ReLU(),
    nn.Linear (50, 5),
)
</code></pre>
<p>(where I’ve chosen, for no particular reason to have 50 “hidden”<br>
neurons).</p>
<p>Good luck!</p>
<p>K. Frank</p>

titoniubo: <p>Thanks <a class="mention" href="/u/kfrank">@KFrank</a>,</p>
<p>I appreciate your feedback.</p>
<p>Can you recommend me any tutorial where Pytorch is used for Logistic Regression where the input is different from an image?</p>
<p>All tutorials I find use MNIST dataset.</p>
<p>I try to use Pytorch for a normal mulit-class classification problem, where I have over 20 variables and 1 target variable (5 class)</p>
<p>Thanks again for your time.</p>

KFrank: <p>Hi Josep!</p>
<aside class="quote no-group" data-username="titoniubo" data-post="3" data-topic="66012" 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>Can you recommend me any tutorial where Pytorch is used for Logistic Regression where the input is different from an image?</p>
<p>All tutorials I find use MNIST dataset.</p>
</blockquote>
</aside>
<p>I don’t know offhand of a tutorial that is pytorch and logistic regression,<br>
but doesn’t use the MNIST dataset.</p>
<p>But it doesn’t matter.  If you look at one of your logistic-regression<br>
tutorials (if it’s done right) you will see that it ignores the spatial<br>
structure of the MNIST image.  You just need to use your 20 variables<br>
in lieu of the ~1000 variable (pixels) in the MNIST image (and adjust<br>
the number of input variable in your model accordingly).</p>
<aside class="quote no-group">
<blockquote>
<p>I try to use Pytorch for a normal mulit-class classification problem, where I have over 20 variables and 1 target variable (5 class)</p>
</blockquote>
</aside>
<p>Just to be clear, you will be doing a <em>multinomial</em> (multiclass)<br>
logistic regression (in contrast to binary).  And (in the interest of<br>
working through a practice problem) you’ll be using a sledge<br>
hammer (pytorch) to perform an old-school logistic regression,<br>
rather than using the power of neural networks (using pytorch)<br>
to build what would likely be a much more capable classifier.<br>
(There’s nothing the matter with this – I just want to make sure you<br>
know that doing logistic regression with pytorch is a toy problem.)</p>
<p>Best.</p>
<p>K. Frank</p>

titoniubo: <p><a class="mention" href="/u/kfrank">@KFrank</a> thanks for your time and answers.</p>
<p>You are right. It’s the first time I try to use regular variables instead of images on Pytorch.</p>
<p>If you could advise me on how to transform variables as input, I would be grateful.</p>
<p>So far I try (note I have 10 variables and 3 outputs):</p>
<pre><code class="lang-auto">X_train = np.array(X_train, dtype=np.float32)
X_train.shape

X_train = X_train.reshape(-1, 10)
X_train.shape

X_test = np.array(X_test, dtype=np.float32)
X_test.shape

X_test = X_test.reshape(-1, 10)
X_test.shape

Y_train = np.array(Y_train, dtype=np.float32)
y_train.shape

Y_train = Y_train.reshape(-1, 1)
Y_train.shape
</code></pre>
<p>I’d appreciate if you could let me know if my reasoning is correct.</p>
<p>Sincerely,</p>

KFrank: <p>Hi Josep!</p>
<aside class="quote no-group quote-modified" data-username="titoniubo" data-post="5" data-topic="66012" 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>So far I try (note I have 10 variables and 3 outputs):</p>
<pre><code class="lang-auto">X_train = np.array(X_train, dtype=np.float32)
X_train.shape
...
</code></pre>
<p>I’d appreciate if you could let me know if my reasoning is correct.</p>
</blockquote>
</aside>
<p>I’m not sure what you are trying to do here.</p>
<p>First, you need to package your input data as a pytorch <code>tensor</code>.<br>
(That’s what pytorch works with.)</p>
<p>You need a batch size.  It can be 1, if you want to work with a single<br>
sample, but let’s say you use a batch size of 7.  Then a single batch<br>
of your input data should be a pytorch tensor of shape <code>(7, 10)</code>.  The<br>
first row of this tensor will be one sample consisting of the values of<br>
your 10 variables.</p>
<p>For <em>regression</em> (in contrast to a more full-featured neural network)<br>
you would train the weights of s single <code>Linear</code> layer so that its<br>
outputs best match your target value.</p>
<p>So you should instantiate <code>nn.Linear (10, 3)</code> as your “model”.</p>
<p>See if you can write a simple script that packs some data into a<br>
<code>tensor</code> of the appropriate shape and successfully passes it through<br>
a single <code>Linear</code>.</p>
<p>If you have issues, feel free to post the complete script, together<br>
with any error messages or incorrect / unexpected output, and<br>
forum participants will likely be able to help you further.</p>
<p>Good luck.</p>
<p>K. Frank</p>

titoniubo: <p>Hello Frank,</p>
<p>Many thanks for your feedback.</p>

titoniubo: <p>Hello Frank,</p>
<p>I hope this message finds you well.</p>
<p>Concerning my last enquiry, I have created a new <a href="https://discuss.pytorch.org/t/multi-class-variable-set-up-torch/66637">post</a>.</p>
<p>Should you have some time to have a look at it and let me know what your thoughts are, I would be very grateful.</p>
<p>Josep Maria</p>