Issue
I know there are some questions that are like this question, but when I follow them it seems to lead me down a rabbit hole. As if The problem I just fixed causes another problem.
Here are 2 of the rabbit hole solutions I have kept because they have seemed to fix their problems. I doubt they would be of any help but here they are just in case.
one:
batch_X = batch_X.to(device=device, dtype=torch.int64)
batch_y = batch_y.to(device=device, dtype=torch.int64)
two:
x = x.view(x.size(0), -1)
This is the error I'm getting.
Traceback (most recent call last):
File "c:/Users/14055/Desktop/Research/new 1.0.py", line 93, in <module>
training()
File "c:/Users/14055/Desktop/Research/new 1.0.py", line 63, in training
output = model(batch_X) # passed input data here!!!!!!!!!!!!!!!!!!!!!!!!!!
File "C:\Users\14055\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
return forward_call(*input, **kwargs)
File "c:/Users/14055/Desktop/Research/new 1.0.py", line 31, in forward
x = F.relu(self.fc1(x))
File "C:\Users\14055\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\14055\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\linear.py", line 96, in forward
return F.linear(input, self.weight, self.bias)
File "C:\Users\14055\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\functional.py", line 1847, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: expected scalar type Float but found Long
My code is below
import torch.cuda
import torch
import numpy as np
import sys
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary
#-------------------------------------------------------------------------
torch.cuda.set_device(0)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
np.set_printoptions(threshold=sys.maxsize)
#-------------------------------------------------------------------------
input_data = torch.Tensor(np.load("inputData.npy", allow_pickle=True))
predict_data = torch.Tensor(np.load("predict.npy", allow_pickle=True))
input_data = input_data.type(torch.FloatTensor)
predict_data = predict_data.type(torch.FloatTensor)
print(type(input_data))
class NeuralNet(nn.Module):
def __init__(self, gpu = True):
super(NeuralNet, self ).__init__()
self.fc1 = nn.Linear(248, 750).to(device)
self.fc2 = nn.Linear(750, 10).to(device)
def forward(self, x):
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = self.fc2(x).to(device)
return x.to(device)
def training():
model.to(device)
training.criterion = nn.CrossEntropyLoss()
optimizer= torch.optim.SGD(model.parameters(), lr=0.003, weight_decay= 0.00005, momentum = .9, nesterov = True)
n_epochs = 20000
a = np.float64([9,9,9,9,9]) #antioverfit
testing_loss = 0.0
BATCH_SIZE = 10
EPOCHS = 3
for epoch in range(EPOCHS):
if(testing_loss <= a[4]): # part of anti overfit
train_loss = 0.0
testing_loss = 0.0
model.train()
for i in (range(0, len(input_data), BATCH_SIZE)):
batch_X = input_data[i:i+BATCH_SIZE]
batch_y = predict_data[i:i+BATCH_SIZE]
optimizer.zero_grad()
batch_X = batch_X.to(device=device, dtype=torch.int64) #gpu # input data here!!!!!!!!!!!!!!!!!!!!!!!!!!
batch_y = batch_y.to(device=device, dtype=torch.int64) #gpu # larget data here!!!!!!!!!!!!!!!!!!!!!!!!!!
output = model(batch_X) # passed input data here!!!!!!!!!!!!!!!!!!!!!!!!!!
loss = training.criterion(output, batch_y)
loss.backward()
optimizer.step()
train_loss += loss.item()*batch_X.size(0)
train_loss = train_loss/len(predict_data.train_loader.dataset)
print('Epoch: {} \tTraining Loss: {:.6f}'.format(epoch+1, train_loss))
model.eval() # Gets Validation loss
train_loss = 0.0
with torch.no_grad():
for i in (range(0, len(input_data), BATCH_SIZE)):
batch_X = input_data[i:i+BATCH_SIZE]
batch_y = predict_data[i:i+BATCH_SIZE]
batch_X = batch_X.to(device=device, dtype=torch.int64)
batch_y = batch_y.to(device=device, dtype=torch.int64)
output = model(batch_X)
loss = training.criterion(output, batch_y).to(device=device, dtype=torch.int64)
testing_loss += loss.item()*batch_X.size(0)
testing_loss = testing_loss / len(predict_data.test_loader.dataset)
print('Validation loss = ' , testing_loss)
a = np.insert(a,0,testing_loss) # part of anti overfit
a = np.delete(a,5)
print('Validation loss = ' , testing_loss)
model = NeuralNet().to(device=device)
#summary(model, input_size=(1, 248, 248))
training()
Solution
Why do you cast X and Y to int64
? Mainly, this is the problem.
batch_X = batch_X.to(device=device, dtype=torch.int64) #gpu # input data here!!!!!!!!!!!!!!!!!!!!!!!!!!
batch_y = batch_y.to(device=device, dtype=torch.int64) #gpu
You cast batch_X
and batch_Y
to int64
a.k.a long
, but float
was expected, hence the error. Replace this with
batch_X = batch_X.to(device=device)
batch_y = batch_y.to(device=device, dtype=torch.int64)
or
batch_X = batch_X.to(device=device, dtype=torch.float)
batch_y = batch_y.to(device=device, dtype=torch.int64)
And this should solve your problem.
EDIT: You only need to keep y as int. Since you are using CrossEntropyLoss which expects target labels (expected to be an int or long). Overall, you need to keep the data type of x to be float, and y should be long or int.
Answered By - Umang Gupta
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.