Answers.Body: <p>Although you label it as a toy system, I see three possible ways to simplify it and get the classifier to start working.</p>
  <ol>
  <li>Instead of starting with 50 inputs, start with only 2 inputs.</li>
  <li>Instead of considering a range from 0 to 99, start with a smaller range. Since we are to learn a threshold between 2 and 3, a symmetric range would suggest having the numbers in the range 0..5, so the data are not that biased towards the trimmed region.</li>
  <li>Instead of viewing this as a classifier, take it as a function approximator: you want the sum of the two inputs when they are different and the maximum when they are the same.</li>
  </ol>
  <p>Once we get this small cell to work, we can take at least the following two possible approaches. The basic cell probably needs to have some hidden layer(s) already.</p>
  <p><strong>Fully connected</strong>. Double the number of inputs while the system works. When it crashes, add an intermediate layer. How many nodes? I guess the ceiling of the square root of the number of input cells, not sure why.</p>
  <p><strong>Sparse</strong>. Place a basic cell at the output and double the number of cells left-wards, until you can take all your input. For 50 inputs, layers with 32, 16, 8, 4, 2, 1 cells should do the job. Note that as each cell takes two inputs, 32 input cells provide 64 inputs, larger than your required 50. Although this network will be deeper than the fully connected alternative, the number of training parameters is given by the basic cell, so it is actually smaller. I take the idea of doubling the number of cells as a tree from <a href="https://www.youtube.com/watch?v=5dWp1mw_XNk" rel="nofollow noreferrer">this video by Andrew Ng</a>.</p>
  <p>As for the learning rate for the SGD algorithm, you may want to use the <a href="https://keras.io/api/optimizers/sgd/" rel="nofollow noreferrer">default value 0.01</a>. For the batch size, you want to learn a function of two variables with nine possible values after trimming, (0,1,2)x(0,1,2). I think I would start with say 16 data points and double while needed, or start at 10 and multiply by 10. But it still surprises me <a href="https://ai.stackexchange.com/questions/39176/why-are-neural-networks-so-data-hungry">how much data is needed to learn a simple function</a>, so my starting points are likely too low.</p>
  <p>Do you have a Colab notebook with your attempt?</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-24T10:55:17.137Z
Answers.Text: Thanks for your answer. I am still trying to digest your answer and suggestions. But here is the link to colab that I have created for this. https://colab.research.google.com/drive/1vSq-281zUGsvA0tei-E2oDwYu5liiyln?usp=sharing
Answers.UserDisplayName: Yang
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-24T11:13:18.393Z
Answers.Text: Ok, thanks! I am more familiar with TensorFlow, but this is a nice opportunity to see some PyTorch. I'll have a look and try some changes asap.
Answers.UserDisplayName: Jaume Oliver Lafont
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-24T12:09:10.323Z
Answers.Text: Thanks. No hurry. I am also just starting to learn these libraries, and this tutorial made me decided to go with pytorch. https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html About the suggestion to start with simpler cases, I was thinking if the cases are too simple, say only 2 inputs, there are only a very limited number of input combinations. In this case does it mean that the NN can simply exhaust all the possibilities and "remember" the outputs?
Answers.UserDisplayName: Yang
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-24T12:29:07.310Z
Answers.Text: Well... In a sense, it has to remember the outputs, doesn't it? Should it forget the outputs and give wrong results? Maybe I didn't get the aim of your project, but it brings to mind modeling logical functions such as OR, AND and XOR with networks as small as possible.
Answers.UserDisplayName: Jaume Oliver Lafont
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-24T12:37:11.050Z
Answers.Text: By "remember" I mean it has seen all the input combinations in the training dataset. In this case the NN just behave like a lookup table?  What we want to do is to use it to predict the output for some inputs that it has not seen before, but hopefully it has extracted certain useful patterns for any inputs in general.
Answers.UserDisplayName: Yang
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-24T08:59:45.267Z
Answers.LastActivityDate: 2023-03-24T09:07:10.383Z
Answers.LastEditDate: 2023-03-24T09:07:10.383Z
Answers.UserDisplayName: Jaume Oliver Lafont
Body: <p>The toy problem: <br>
  50 unique numbers are randomly selected from number 0 to 99.</p>
  <ul>
  <li>If number 1 appears in the selection but number 2 doesn't, the selection is labelled as &quot;1&quot;. <br></li>
  <li>If number 2 appears in the selection but number 1 doesn't, the selection is labelled as &quot;2&quot;. <br></li>
  <li>If both number 1 and number 2 appear in the selection, the selection is labelled as &quot;3&quot;. <br></li>
  <li>Lastly, if neither 1 nor 2 appears in the selection, the selection is labelled as &quot;0&quot;.<br><br>
  Let's say we want to use a fully connected neural network to classify the selections. The number of input nodes would be 50, which is the number of items in each selection. The number of output nodes will be 4, which will be number of labels/classes. The loss function is cross entry loss which is commonly used in classification problems. The activation function for the hidden layers is ReLU(or leaky ReLU). <br><br>The question is how do we decide on the number of hidden layers and the number of nodes in each layer? Also let's say we use stochastic gradient descent(SGD) as optimizer, how do we choose the learning rate? Also the training batch size?<br><br>
  I have tried some arbitrary values for those hyper parameters. In some cases the loss doesn't decrease. In some cases the loss decrease to almost 0 for the training data set, but the prediction accuracy for the validation dataset is 25%, which is as good as random guess.<br><br>Is there any guidelines/heuristics on how to choose those hyperparameters?</li>
  </ul>
  
Tags: <neural-networks><machine-learning><classification><hyper-parameters>
Title: How to approach a toy classification problem using a neural network?
UserDisplayName: Yang
ThreadText: # How to approach a toy classification problem using a neural network?
  
  (Asked by: username_0 on 2023-03-24)
  
  The toy problem:   
  50 unique numbers are randomly selected from number 0 to 99.
  
  * If number 1 appears in the selection but number 2 doesn't, the selection is labelled as "1".
  * If number 2 appears in the selection but number 1 doesn't, the selection is labelled as "2".
  * If both number 1 and number 2 appear in the selection, the selection is labelled as "3".
  * Lastly, if neither 1 nor 2 appears in the selection, the selection is labelled as "0".  
      
    Let's say we want to use a fully connected neural network to classify the selections. The number of input nodes would be 50, which is the number of items in each selection. The number of output nodes will be 4, which will be number of labels/classes. The loss function is cross entry loss which is commonly used in classification problems. The activation function for the hidden layers is ReLU(or leaky ReLU).   
      
    The question is how do we decide on the number of hidden layers and the number of nodes in each layer? Also let's say we use stochastic gradient descent(SGD) as optimizer, how do we choose the learning rate? Also the training batch size?  
      
    I have tried some arbitrary values for those hyper parameters. In some cases the loss doesn't decrease. In some cases the loss decrease to almost 0 for the training data set, but the prediction accuracy for the validation dataset is 25%, which is as good as random guess.  
      
    Is there any guidelines/heuristics on how to choose those hyperparameters?
  
  **Tags:** neural-networks, machine-learning, classification, hyper-parameters
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-03-24. Score: 2
  
  Although you label it as a toy system, I see three possible ways to simplify it and get the classifier to start working.
  
  1. Instead of starting with 50 inputs, start with only 2 inputs.
  2. Instead of considering a range from 0 to 99, start with a smaller range. Since we are to learn a threshold between 2 and 3, a symmetric range would suggest having the numbers in the range 0..5, so the data are not that biased towards the trimmed region.
  3. Instead of viewing this as a classifier, take it as a function approximator: you want the sum of the two inputs when they are different and the maximum when they are the same.
  
  Once we get this small cell to work, we can take at least the following two possible approaches. The basic cell probably needs to have some hidden layer(s) already.
  
  **Fully connected**. Double the number of inputs while the system works. When it crashes, add an intermediate layer. How many nodes? I guess the ceiling of the square root of the number of input cells, not sure why.
  
  **Sparse**. Place a basic cell at the output and double the number of cells left-wards, until you can take all your input. For 50 inputs, layers with 32, 16, 8, 4, 2, 1 cells should do the job. Note that as each cell takes two inputs, 32 input cells provide 64 inputs, larger than your required 50. Although this network will be deeper than the fully connected alternative, the number of training parameters is given by the basic cell, so it is actually smaller. I take the idea of doubling the number of cells as a tree from [this video by Andrew Ng](https://www.youtube.com/watch?v=5dWp1mw_XNk).
  
  As for the learning rate for the SGD algorithm, you may want to use the [default value 0.01](https://keras.io/api/optimizers/sgd/). For the batch size, you want to learn a function of two variables with nine possible values after trimming, (0,1,2)x(0,1,2). I think I would start with say 16 data points and double while needed, or start at 10 and multiply by 10. But it still surprises me [how much data is needed to learn a simple function](https://ai.stackexchange.com/questions/39176/why-are-neural-networks-so-data-hungry), so my starting points are likely too low.
  
  Do you have a Colab notebook with your attempt?
  
  > **Comments:**
  > **username_0**: Thanks for your answer. I am still trying to digest your answer and suggestions. But here is the link to colab that I have created for this. https://colab.research.google.com/drive/1vSq-281zUGsvA0tei-E2oDwYu5liiyln?usp=sharing  
  > **username_1**: Ok, thanks! I am more familiar with TensorFlow, but this is a nice opportunity to see some PyTorch. I'll have a look and try some changes asap.  
  > **username_0**: Thanks. No hurry. I am also just starting to learn these libraries, and this tutorial made me decided to go with pytorch. https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html About the suggestion to start with simpler cases, I was thinking if the cases are too simple, say only 2 inputs, there are only a very limited number of input combinations. In this case does it mean that the NN can simply exhaust all the possibilities and "remember" the outputs?  
  > **username_1**: Well... In a sense, it has to remember the outputs, doesn't it? Should it forget the outputs and give wrong results? Maybe I didn't get the aim of your project, but it brings to mind modeling logical functions such as OR, AND and XOR with networks as small as possible.  
  > **username_0**: By "remember" I mean it has seen all the input combinations in the training dataset. In this case the NN just behave like a lookup table?  What we want to do is to use it to predict the output for some inputs that it has not seen before, but hopefully it has extracted certain useful patterns for any inputs in general.  
  
  ---
  
  