Having trouble writing training loop in pytorch
I load data from a CSV FILE OF 20+6 Columns(Features and Labels). Im trying to run my data through Convolutional Neural Network in pytorch. I get error saying it expects a 3D input and Im giving it 1D input. I am using Conv1d . 

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import pandas as pd
from torch.utils.data import Dataset,DataLoader
from sklearn.model_selection import train_test_split

#Read Data
data=pd.read_csv('Data.csv')
Features=data[data.columns[0:20]]
Labels=data[data.columns[20:]]
#Split Data
X_train, X_test, y_train, y_test = train_test_split( Features, Labels, test_size=0.33, shuffle=True)
#Create Tensors
train_in=torch.tensor(X_train.values)
train_out=torch.tensor(y_train.values)
test_in=torch.tensor(X_test.values)
test_out=torch.tensor(y_test.values)
#Model CNN
class CNN(nn.Module):
        def __init__(self):
                super(CNN,self).__init__()
                self.layer1 = nn.Sequential(
                                nn.Conv1d(20,40,kernel_size=5,stride=1,padding=2),
                                nn.ReLU(),
                                nn.MaxPool1d(kernel_size=2,stride=2)
                                )
                self.layer2 = nn.Sequential(
                                nn.Conv1d(40,60,kernel_size=5,stride=1,padding=2),
                                nn.ReLU(),
                                nn.MaxPool1d(kernel_size=2,stride=2)
                                )
                self.drop_out = nn.Dropout()
                self.fc1 = nn.Linear(60,30)
                self.fc2 = nn.Linear(30,15)
                self.fc3 = nn.Linear(15,6)

        def forward(self,x):
                out=self.layer1(x)
                out=self.layer2(out)
                out=self.drop_out(out)
                out=self.fc1(out)
                out=self.fc2(out)
                out=self.fc3(out)
                return out


Epochs=10
N_labels=len(Labels.columns)
N_features=len(Features.columns)
batch_size=100
learning_rate=0.001
#TRAIN MODEL        
model = CNN()
#LOSS AND OPTIMIZER
criterion = torch.nn.SmoothL1Loss()
optimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)
#TRAIN MODEL
model.train()
idx=0
for i in train_in:
        y=model(i)
        loss=criterion(y,train_out[idx])
        idx+=1
        loss.backward()
        optimizer.step()


How do I write the Training and Eval loop? All the examples I see on the internet all use images and they also use DataLoader.

Conv1D takes as input a tensor with 3 dimensions (N, C, L) where N is the batchsize, C is the number of channels and L size of the 1D data. In your case it seems like one sample has 20 entries and you have one channel. You have a batch_size variable but it is not used in the code posted.

 nn.Conv1d(20,40,kernel_size=5,stride=1,padding=2)


This lines creates a convolution which takes a input with 20 channels (you have 1) and outputs 40 channels. So you have to change the 20 to a 1 and you might wanna change the 40 to something smaller. Since convolutions are applied to the whole input (controlled by stride, patting and kernel size), there is no need to specify the size of a sample.

Also you might wanna add some logic to build minibatches. Right now it seems like you just want to input every sample by itself. Maybe read a bit about dataset classes and data loaders in pytorch.
