CNN for Numerical Predictions

andrewjumanca: <p>Hi, I’m <em>very</em> new to PyTorch.</p>
<p>I’m somewhat experienced with Python, but have been programming in other languages (namely Java) for a few years now. My goal is to implement a machine learning program that will take in–as input–and learn from hundreds of arrays of numbers and a corresponding value to each array. To put it simply, I have a long list of arrays, and each array of numbers has 1 correct corresponding value based on the values inside the array.</p>
<p>I’m still new to neural networks, so you can imagine that all these introductory code examples and basics mainly feature simple neural networks designed to learn images and predict them. How do I go about using arrays as an input? I don’t know where else to go, so if someone could point me in the right direction that would be super helpful.</p>

ptrblck: <p>You could use your input data in a similar way, but would probably need to adapt the model architecture to accept “arrays” instead of images.<br>
This can be done by e.g. <code>nn.Linear</code> layers and here is a very simple example with some annotations to get you started:</p>
<pre><code class="lang-python"># 10 data samples with 8 features each
data = torch.randn(10, 8)

# one floating point target for each sample
target = torch.randn(10, 1)

# create a simple model using a linear layer
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.fc1 = nn.Linear(in_features=8, out_features=1)        
        
    def forward(self, x):
        x = self.fc1(x)
        return x

model = MyModel()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
criterion = nn.MSELoss()

# train the model for 10 epochs
nb_epochs = 1000
for epoch in range(nb_epochs):
    # zero out gradients, since gradients are accumulated in PyTorch
    optimizer.zero_grad()
    # perform the forward pass
    output = model(data)
    # calculate the loss
    loss = criterion(output, target)
    # calculate the gradients
    loss.backward()
    # optimize the parameters using the gradients
    optimizer.step()
    # print stats
    if epoch % 100 == 0:
        print('epoch {}, loss {}'.format(epoch, loss.item()))
</code></pre>
<p>I assume you’ve already taken a look at our <a href="https://pytorch.org/tutorials/">tutorials</a>, which would be the next steps to dig into the framework. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

Ajinkya_Anjikar: <p>Hello,<br>
Please correct me if I am wrong.<br>
Sorry in advance if I have mistaken the information.</p>
<p>Your code indicates a simple neural network. What if one wants to implement CNN for numerical data?</p>
<p>Can you please provide insights related to the input to convolutional and max-pooling layer for numerical data?</p>

ptrblck: <p>CNNs would require the input data in another shape (e.g. <code>nn.Conv2d</code> would expect an input in the shape <code>[batch_size, channels, height, width]</code>). I’m unsure what kind of data set you are using, but you could provide the input in the expected shape and use a CNN as an alternative to the proposed model.<br>
Note that CNNs apply their filters to the input and convolve (or cross-correlate) them. This is often beneficial, if your input data has a spatial pattern (such as images), but you could of course try this approach on any kind of data and report your findings. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

Ajinkya_Anjikar: <p>Thank you for the information.<br>
I have given details of my dataset in another question. Please go through the attached link.<br>
<a href="https://discuss.pytorch.org/t/convolutional-neural-network-cnn-on-numerical-data/122225/2">https://discuss.pytorch.org/t/convolutional-neural-network-cnn-on-numerical-data/122225/2</a></p>
<p>Please let me know if further information is required.</p>