ValueError: x and y must be the same size #557
-
im new to asking for help on GitHub so here is ALL the code: import torch
from torch import nn
import matplotlib.pyplot as plt
wieght = 0.7
bias = 0.3
start = 0
end = 1
step = 0.02
X = torch.arange(start, end, step).unsqueeze(dim=1)
y = wieght * X + bias
train_split = int(0.8 * len(X))
X_train, y_train = X[:train_split], y[:train_split]
X_test, y_test = X[train_split:], y[train_split:]
def plot_prediction(train_data = X_train,
train_labels = y_train,
test_data = X_test,
test_labels = y_test,
predictions = None):
plt.figure(figsize=(10, 7))
plt.scatter(train_data, train_labels, c="b", s=4, label="Training Data!")
plt.scatter(test_data, test_labels, c="g", s=4, label="Testing Data")
if predictions is not None:
plt.scatter(test_data, predictions, c="r", s=4, label="Epic Gamer Predictions!!!")
plt.legend(prop={"size":14});
class LinearRegressionModel(nn.Module):
def __init__(self):
super().__init__()
self.wieght = nn.Parameter(torch.randn(1,
requires_grad=True,
dtype=torch.float))
self.bias = nn.Parameter(torch.randn(1,
requires_grad=True,
dtype=torch.float))
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.wieght * X + self.bias
torch.manual_seed(42)
model0 = LinearRegressionModel()
with torch.inference_mode():
y_preds = model0(X_test)
plot_prediction(predictions=y_preds) error code:
sorry if i gave to much code, ive spent awhile trying to figure out what this means and how to fix it and cannot for the life of me figure it out. thank you in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi, Hope this helps :) import torch
from torch import nn
import matplotlib.pyplot as plt
wieght = 0.7
bias = 0.3
start = 0
end = 1
step = 0.02
X = torch.arange(start, end, step).unsqueeze(dim=1)
y = wieght * X + bias
train_split = int(0.8 * len(X))
X_train, y_train = X[:train_split], y[:train_split]
X_test, y_test = X[train_split:], y[train_split:]
def plot_prediction(train_data = X_train,
train_labels = y_train,
test_data = X_test,
test_labels = y_test,
predictions = None):
plt.figure(figsize=(10, 7))
plt.scatter(train_data, train_labels, c="b", s=4, label="Training Data!")
plt.scatter(test_data, test_labels, c="g", s=4, label="Testing Data")
if predictions is not None:
plt.scatter(test_data, predictions, c="r", s=4, label="Epic Gamer Predictions!!!")
plt.legend(prop={"size":14})
class LinearRegressionModel(nn.Module):
def __init__(self):
super().__init__()
self.wieght = nn.Parameter(torch.randn(1,
requires_grad=True,
dtype=torch.float))
self.bias = nn.Parameter(torch.randn(1,
requires_grad=True,
dtype=torch.float))
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.wieght * x + self.bias
torch.manual_seed(42)
model0 = LinearRegressionModel()
with torch.inference_mode():
y_preds = model0(X_test)
plot_prediction(predictions=y_preds) |
Beta Was this translation helpful? Give feedback.
-
The error “ValueError: x and y must be the same size” occurs in Python when the input initialized array does not have the same sizes. To rectify this error, different solutions are used in Python, such as aligning the length of input arrays or using numpy arrays instead of lists. I found this guide on search it was very helpful, you can check this out to resolve this error. |
Beta Was this translation helpful? Give feedback.
Hi,
I run your code but get different errors. Note that there are double underscores on both sides of the init method. In the forward method, the argument is x, but you use X below. And, there's a typo: it's weight instead of wieght, though the result will not be affected.
Hope this helps :)