Hello. I am new to building neural networks. I am trying to build a neural network using Pytorch that has 11 inputs, 1 hidden layer with 11 neurons, and 2 outputs. Right now I am working on creating a tensor dataset with my given data but I’m having a hard time getting through the “AssertionError: Size mismatch between tensors”. Anything will help trying to get around this. My entire code so far is attached below:
import torch
import numpy as np
from torch import nn
from torch import optim
from torch.utils.data import TensorDataset
import matplotlib.pyplot as plt
import pandas as pd
‘’’
Neural Network Structure:
11 inputs
11 neurons in the first hidden layer
2 ouputs
‘’’
Get data from tables
Split data up into training and testing (80% Train, 20% Test)
pnt_cont_data = pd.read_excel(‘C:\Users\bamar\Downloads\Chile_Research\Research_Resources\DoE_point_contact.xlsm’, sheet_name=‘Sample’, index_col = 0, names=[‘#’,‘E1’,‘E2’,‘v1’,‘v2’,‘Ap’,‘rho0’,‘mue0’,‘u1’,‘u2’,‘R’,‘Fn’,‘hmin’,‘hc’,‘p’])
pnt_cont_data.drop(index = pnt_cont_data.index[0], axis = 0, inplace =True)
del pnt_cont_data[‘p’]
Turning data into numpy arrays
X = pnt_cont_data.to_numpy()[:,:-3]
split = int(0.8*len(X))
X_train_np, X_test_np = X[:split], X[split:]
y = pnt_cont_data.to_numpy()[:,11:]
y_train_np, y_test_np = y[:split], y[split:]
‘’’
print(X_train_np.shape, y_train_np.shape)
print(X_test_np.shape, y_test_np.shape)
(800, 10) (800, 2)
(200, 10) (200, 2)
‘’’
X_train_float = X_train_np.astype(np.float32)
X_train = torch.Tensor(X_train_float)
X_test_float = X_test_np.astype(np.float32)
X_test = torch.Tensor(X_test_float)
y_train_float = y_train_np.astype(np.float32)
y_train = torch.Tensor(y_train_float)
y_test_float = y_test_np.astype(np.float32)
y_test = torch.Tensor(y_test_float)
train_dataset = TensorDataset(X_train, y_train)