-
I am new to First of all, I installed I also installed Then, I run the following program. import torch
from torch import nn
from sklearn.metrics import r2_score
class MyMachine(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Sequential(
nn.Linear(2,5),
nn.ReLU(),
nn.Linear(5,1)
)
def forward(self, x):
x = self.fc(x)
return x
def get_dataset():
X = torch.rand((1000,2))
x1 = X[:,0]
x2 = X[:,1]
y = x1 * x2
return X, y
def train():
model = MyMachine()
model.train()
X, y = get_dataset()
NUM_EPOCHS = 1000
optimizer = torch.optim.Adam(model.parameters(), lr=1e-2, weight_decay=1e-5)
criterion = torch.nn.MSELoss(reduction='mean')
for epoch in range(NUM_EPOCHS):
optimizer.zero_grad()
y_pred = model(X)
y_pred = y_pred.reshape(1000)
loss = criterion(y_pred, y)
loss.backward()
optimizer.step()
print(f'Epoch:{epoch}, Loss:{loss.item()}')
torch.save(model.state_dict(), 'model.h5')
def test():
model = MyMachine()
model.load_state_dict(torch.load("model.h5"))
model.eval()
X, y = get_dataset()
with torch.no_grad():
y_pred = model(X)
print(r2_score(y, y_pred))
train()
test() After that, I get the following error.
According to the PyTorch website, my installation should cause How can I fix this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Sorry you got caught by a scikit-learn issue in the latest release when using a scikit-learn metric on PyTorch tensors on the CPU and the error is confusing, see #29107 (comment) for more details. For completeness if you want a tensor on the GPU you need to use X = torch.rand((1000,2), device='cuda') If you want to fix your error quickly you need to convert to numpy arrays before calling the scikit-learn metric, something like You may be interested to have a look at array API support which is an ongoing work. |
Beta Was this translation helpful? Give feedback.
Sorry you got caught by a scikit-learn issue in the latest release when using a scikit-learn metric on PyTorch tensors on the CPU and the error is confusing, see #29107 (comment) for more details.
For completeness if you want a tensor on the GPU you need to use
device='cuda'
I think:If you want to fix your error quickly you need to convert to numpy arrays before calling the scikit-learn metric, something like
arr.numpy()
if you array in on the CPU orarr.cpu().device()
.You may be interested to have a look at array API support which is an ongoing work.