admin管理员组文章数量:1306128
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.
本文标签: pythonWhy do I have to unsqueeze the data from torchSize(50) to torchSize(501)Stack Overflow
版权声明:本文标题:python - Why do I have to unsqueeze the data from torch.Size([50]) to torch.Size([50, 1]) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741814792a2399011.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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