I was doing the PyTorch Deep Learning course from FreeCodeCamp and the doubt is:
weight = 0.7
bias = 0.3
start = 0
end = 1
step = 0.02
X = torch.arange(start, end, step).unsqueeze(dim=1)
y=weight*X + bias
X[:10], y[:10]
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:]
Why the unsqueeze function is used to make the tensor of size [50, 1] and not [50]? The mentor was telling that it will cause error but I don't know why the error is happening?
Can you answer this question using maths and as well as basic fundamentals without maths as well ?
After trying to train the model I am getting this error:
class LinearRegressionModelv2(nn.Module):
def __init__(self):
super().__init__()
self.linear_layer = nn.Linear(in_features=1, out_features=1)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.linear_layer(x)
torch.manual_seed(42)
model_v2 = LinearRegressionModelv2()
y_prediction = model_v2(X_train)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2)
I was doing the PyTorch Deep Learning course from FreeCodeCamp and the doubt is:
weight = 0.7
bias = 0.3
start = 0
end = 1
step = 0.02
X = torch.arange(start, end, step).unsqueeze(dim=1)
y=weight*X + bias
X[:10], y[:10]
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:]
Why the unsqueeze function is used to make the tensor of size [50, 1] and not [50]? The mentor was telling that it will cause error but I don't know why the error is happening?
Can you answer this question using maths and as well as basic fundamentals without maths as well ?
After trying to train the model I am getting this error:
class LinearRegressionModelv2(nn.Module):
def __init__(self):
super().__init__()
self.linear_layer = nn.Linear(in_features=1, out_features=1)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.linear_layer(x)
torch.manual_seed(42)
model_v2 = LinearRegressionModelv2()
y_prediction = model_v2(X_train)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2)
Share Improve this question edited Feb 4 at 9:34 SouraOP asked Feb 3 at 14:44 SouraOPSouraOP 33 bronze badges 3 |1 Answer
Reset to default 0Mathematically, the formula for Linear regression is given by
(y = X.WT + b )
here,
- Input tensor,
X
, has shape (batch_size, in_features) - Weight matrix,
W
has shape (out_features, in_features) - Bias,
B
is a vector of shape (out_features).
When you make X
in 1D (e.g., [50] in your case), matrix multiplication is undefined because the dimensions don't align. Therefore, to ensure correct computation input data (X
) should be 2 dimensional, where:
- each row represents a sample
- and each column represents a feature.
X_train
is or what yourmodel_v2
was actually trained on. Generally, though,unsqueeze
would be to get dimensions to match. In this case it's probably for making the first dimension an index into the dataset, where each datapoint is a single value – Chrispresso Commented Feb 3 at 15:35