CNN for Numerical Predictions

Hi, I’m very new to PyTorch.

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.

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.

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.
This can be done by e.g. nn.Linear layers and here is a very simple example with some annotations to get you started:

# 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()))

I assume you’ve already taken a look at our tutorials, which would be the next steps to dig into the framework. :wink:

Hello,
Please correct me if I am wrong.
Sorry in advance if I have mistaken the information.

Your code indicates a simple neural network. What if one wants to implement CNN for numerical data?

Can you please provide insights related to the input to convolutional and max-pooling layer for numerical data?

CNNs would require the input data in another shape (e.g. nn.Conv2d would expect an input in the shape [batch_size, channels, height, width]). 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.
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. :slight_smile:

Thank you for the information.
I have given details of my dataset in another question. Please go through the attached link.
https://discuss.pytorch.org/t/convolutional-neural-network-cnn-on-numerical-data/122225/2

Please let me know if further information is required.