I am learning the basics of pytorch and thought to create a simple 4 layer nerual network with dropout to train IRIS dataset for classification. After refering to many tutorials I wrote this code.
import pandas as pd
from sklearn.datasets import load_iris
import torch
from torch.autograd import Variable
epochs=300
batch_size=20
lr=0.01
#loading data as numpy array
data = load_iris()
X=data.data
y=pd.get_dummies(data.target).values
#convert to tensor
X= Variable(torch.from_numpy(X), requires_grad=False)
y=Variable(torch.from_numpy(y), requires_grad=False)
print(X.size(),y.size())
#neural net model
model = torch.nn.Sequential(
torch.nn.Linear(4, 10),
torch.nn.ReLU(),
torch.nn.Dropout(),
torch.nn.Linear(10, 5),
torch.nn.ReLU(),
torch.nn.Dropout(),
torch.nn.Linear(5, 3),
torch.nn.Softmax()
)
print(model)
# Loss and Optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
loss_func = torch.nn.CrossEntropyLoss()
for i in range(epochs):
# Forward pass
y_pred = model(X)
# Compute and print loss.
loss = loss_func(y_pred, y)
print(i, loss.data[0])
# Before the backward pass, use the optimizer object to zero all of the
# gradients for the variables it will update (which are the learnable weights
# of the model)
optimizer.zero_grad()
# Backward pass
loss.backward()
# Calling the step function on an Optimizer makes an update to its parameters
optimizer.step()
There are currently two problems I am facing.
- I want to set a batch size of
20
. How should I do this? - At this step
y_pred = model(X)
its showing this error
Error
TypeError: addmm_ received an invalid combination of arguments - got (int, int, torch.DoubleTensor, torch.FloatTensor), but expected one of:
* (torch.DoubleTensor mat1, torch.DoubleTensor mat2)
* (torch.SparseDoubleTensor mat1, torch.DoubleTensor mat2)
* (float beta, torch.DoubleTensor mat1, torch.DoubleTensor mat2)
* (float alpha, torch.DoubleTensor mat1, torch.DoubleTensor mat2)
* (float beta, torch.SparseDoubleTensor mat1, torch.DoubleTensor mat2)
* (float alpha, torch.SparseDoubleTensor mat1, torch.DoubleTensor mat2)
* (float beta, float alpha, torch.DoubleTensor mat1, torch.DoubleTensor mat2)
didn't match because some of the arguments have invalid types: (int, int, torch.DoubleTensor, !torch.FloatTensor!)
* (float beta, float alpha, torch.SparseDoubleTensor mat1, torch.DoubleTensor mat2)
didn't match because some of the arguments have invalid types: (int, int, !torch.DoubleTensor!, !torch.FloatTensor!)