Python 如何调整与Pytork线性回归一起使用的一批图像的大小?
我试图创建一个简单的线性回归神经网络,用于批量图像。输入尺寸为Python 如何调整与Pytork线性回归一起使用的一批图像的大小?,python,pytorch,Python,Pytorch,我试图创建一个简单的线性回归神经网络,用于批量图像。输入尺寸为[BatchSize,3,Width,Height],第二个尺寸表示输入图像的RGB通道 以下是我对该回归模型的(失败的)尝试: class LinearNet(torch.nn.Module): def __init__(self, Chn, W,H, nHidden): """ Input: A [BatchSize x Channels x Width x H
[BatchSize,3,Width,Height]
,第二个尺寸表示输入图像的RGB通道
以下是我对该回归模型的(失败的)尝试:
class LinearNet(torch.nn.Module):
def __init__(self, Chn, W,H, nHidden):
"""
Input: A [BatchSize x Channels x Width x Height] set of images
Output: A fitted regression model with weights dimension : [Width x Height]
"""
super(LinearNet, self).__init__()
self.Chn = Chn
self.W = W
self.H = H
self.hidden = torch.nn.Linear(Chn*W*H,nHidden) # hidden layer
self.predict = torch.nn.Linear(nHidden, Chn*W*H) # output layer
def forward(self, x):
torch.reshape(x, (-1,self.Chn*self.W*self.H)) # FAILS here
# x = x.resize(-1,self.Chn*self.W*self.H)
x = F.relu(self.hidden(x)) # activation function for hidden layer
x = self.predict(x) # linear output
x = x.resize(-1,self.Chn, self.W,self.H)
return x
当发送一批尺寸为[128 x 3 x 96 x 128]
的图像时,在指示行上失败:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (36864x128 and 36864x256)
为了使用这些pytorch函数,应如何正确地操纵矩阵尺寸
更新根据一条(删除后)注释,我已更新代码以使用
火炬。重塑
解决方案1作为一种可能的解决方案,您可以使用x.shape[0]
从输入x
获取批量大小,并在以后的重塑中使用它
import torch
batch = torch.zeros([128, 3, 96, 128], dtype=torch.float32)
# -1 will compute last dimension automatically
batch_upd = torch.reshape(batch, (batch.shape[0], -1))
print(batch_upd.shape)
此代码的输出为
torch.Size([128, 36864])
解决方案2
作为另一种可能的解决方案,您可以使用
将导致相同的输出
关于你的下一个问题,考虑修改过的<代码>前/代码>代码:
def forward(self, x):
x = x.flatten(1) # shape: [B, C, W, H] -> [B, C*W*H]
x = F.relu(self.hidden(x)) # activation function for hidden layer
x = self.predict(x) # linear output
x = x.reshape((-1, self.Chn, self.W, self.H)) # shape: [B, C*W*H] -> [B, C, W, H]
return x
以下是成功使用的示例:
ln = LinearNet(3, 96, 128, 256)
batch = torch.zeros((128, 3, 96, 128))
res = ln(batch)
print(res.shape) # torch.Size([128, 3, 96, 128])
解决方案1作为一种可能的解决方案,您可以使用
x.shape[0]
从输入x
中获取批量大小,并在以后的重塑中使用它
import torch
batch = torch.zeros([128, 3, 96, 128], dtype=torch.float32)
# -1 will compute last dimension automatically
batch_upd = torch.reshape(batch, (batch.shape[0], -1))
print(batch_upd.shape)
此代码的输出为
torch.Size([128, 36864])
解决方案2
作为另一种可能的解决方案,您可以使用
将导致相同的输出
关于你的下一个问题,考虑修改过的<代码>前/代码>代码:
def forward(self, x):
x = x.flatten(1) # shape: [B, C, W, H] -> [B, C*W*H]
x = F.relu(self.hidden(x)) # activation function for hidden layer
x = self.predict(x) # linear output
x = x.reshape((-1, self.Chn, self.W, self.H)) # shape: [B, C*W*H] -> [B, C, W, H]
return x
以下是成功使用的示例:
ln = LinearNet(3, 96, 128, 256)
batch = torch.zeros((128, 3, 96, 128))
res = ln(batch)
print(res.shape) # torch.Size([128, 3, 96, 128])
错误是说您的x的大小为
128x396x128
,并且有4718592
元素,但是,您试图将其仅转换为36864
元素。您缺少*128
。错误是您的x的大小为128x396x128
,并且它有4718592
元素,但是,您试图将其仅转换为36864
元素。您缺少*128
。扁平化是否会导致上下文丢失?线性函数不应该将所有行压缩成一个大向量:它需要同时求解所有128行(批量大小)。reformate()
似乎是正确的:但它不同于flatte
no?@StephenBoesch它看起来像flatte
是在引擎盖下使用reformate
实现的,您可以检查它。但是,如果您不确定,请随意使用普通重塑ofc。啊,我错过了展平
的参数(1)
使批次大小保持不变展平
不会导致上下文丢失吗?线性函数不应该将所有行压缩成一个大向量:它需要同时求解所有128行(批量大小)。reformate()
似乎是正确的:但它不同于flatte
no?@StephenBoesch它看起来像flatte
是在引擎盖下使用reformate
实现的,您可以检查它。但是,如果您不确定,请随意使用普通重塑ofc。啊,我错过了展平
的参数(1)
,该参数保持批量大小不变