Reputation: 23
I'm sorry if this is a basic question, but I am reasonably new to Pytorch and have been trying to perform linear regression. I've been stuck on the following problem for about 3 hours and have tried everything, so I hope you're able to help.
I want to predict grades using a set of four inputs (time studied, travel time, failures and absenses). For my outputs I have grades on 3 tests. There are 395 students in the dataset.
I've listed all my code at the bottom, but I'll paste the relevant part here too:
So far I've created input and output tensors and have created a model for matrix multiplication. Here's my code:
w = torch.randn(395,4, requires_grad=True)
b = torch.randn(4, requires_grad=True)
print(w)
print(b)
def model(x):
return x @ w.t() + b
predictions = model(inputs)
print(predictions)
I know this isn't the linear regression yet, but I'm really struggling with the matrix multiplication aspect of it. Whenever I run the print(predictions) code I get the following message:
RuntimeError Traceback (most recent call last)
<ipython-input-277-c6db141153ac> in <module>
----> 1 preds = model(inputs)
2 print(preds)
<ipython-input-276-3b94bfbc599e> in model(x)
1 def model(x):
----> 2 return x @ w.t() + b
RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #3 'mat2' in call to _th_addmm_out
I have a feeling the numbers for w and b are wrong (395,4) and (4), but I have no idea why or what to change them to. Is there any chance someone could point me in the right direction please?
Thanks in advance!
Here's my whole code:
'''
import torch
import numpy as np
from numpy import genfromtxt
data = np.genfromtxt('student-mat.csv', delimiter=',',dtype=float)
data
travel_time = data[1:, 0:1]
study_time = data[1:, 1:2]
failures = data[1:, 2:3]
absenses = data[1:, 3:4]
grade_one = data[1:, 4:5]
grade_two = data[1:, 5:6]
grade_three = data[1:, 6:7]
data_input = data[1:, 0:4]
output = data[1:, 4:7]
inputs = torch.from_numpy(data_input)
outputs = torch.from_numpy(grade_one)
print(inputs)
print(grade_one)
w = torch.randn(395,4, requires_grad=True)
b = torch.randn(4, requires_grad=True)
print(w)
print(b)
def model(x):
return x @ w.t() + b
preds = model(inputs)
print(preds)
Dataset - https://archive.ics.uci.edu/ml/datasets/Student+Performance
Upvotes: 2
Views: 582
Reputation: 11227
The error message says it all. The tensors involved contain elements of different data types. By default, w
and b
have elements of type torch.float32
, while data_input
is a NumPy array with the Python default floating point type, i.e. double. That datatype will be preserved when you convert with from_numpy
. Try using dtype=np.float32
in your np.genfromtxt
call.
Upvotes: 1