C++ 尺寸不匹配CNN LIBROTCH/PyTorch
我在LibTorch中有一个CNN结构,但尺寸不合适。我的目标是为DGAN输入3通道64x64图像并输出逻辑回归浮点。最后一层我设置为输入通道36,因为如果我移除该层,输出神经元的尺寸为6x6,所以我猜这是完全连接的输入所需的尺寸。我想知道:C++ 尺寸不匹配CNN LIBROTCH/PyTorch,c++,pytorch,conv-neural-network,libtorch,C++,Pytorch,Conv Neural Network,Libtorch,我在LibTorch中有一个CNN结构,但尺寸不合适。我的目标是为DGAN输入3通道64x64图像并输出逻辑回归浮点。最后一层我设置为输入通道36,因为如果我移除该层,输出神经元的尺寸为6x6,所以我猜这是完全连接的输入所需的尺寸。我想知道: 您通常如何检查LibTorch或Pytorch中的尺寸(即检查最后一个模块所需的尺寸,检查每层有多少个可培训参数…) 这种情况下的错误是什么 我有几个问题,但最终这个体系结构起了作用 using namespace torch; class DCGA
- 您通常如何检查LibTorch或Pytorch中的尺寸(即检查最后一个模块所需的尺寸,检查每层有多少个可培训参数…)
- 这种情况下的错误是什么
我有几个问题,但最终这个体系结构起了作用
using namespace torch;
class DCGANDiscriminatorImpl: public nn::Module {
private:
nn::Conv2d conv1, conv2, conv3, conv4;
nn::BatchNorm2d batch_norm1, batch_norm2;
nn::Linear fc1;
public:
DCGANDiscriminatorImpl()
:conv1(nn::Conv2dOptions(3, 64, 4).stride(2).padding(1).bias(false)),
conv2(nn::Conv2dOptions(64, 128, 4).stride(2).padding(1).bias(false)),
batch_norm1(128),
conv3(nn::Conv2dOptions(128, 256, 4).stride(2).padding(1).bias(false)),
batch_norm2(256),
conv4(nn::Conv2dOptions(256, 64, 3).stride(1).padding(0).bias(false)),
fc1(6*6*64, 1)
{
register_module("conv1", conv1);
register_module("conv2", conv2);
register_module("conv3", conv3);
register_module("conv4", conv4);
register_module("batch_norm1", batch_norm1);
register_module("batch_norm2", batch_norm2);
register_module("fc1", fc1);
}
Tensor forward(torch::Tensor x)
{
x = leaky_relu(conv1(x), cte::NEGATIVE_SLOPE);
x = leaky_relu(batch_norm1(conv2(x)), cte::NEGATIVE_SLOPE);
x = leaky_relu(batch_norm2(conv3(x)), cte::NEGATIVE_SLOPE);
x = leaky_relu(conv4(x), cte::NEGATIVE_SLOPE);
x = x.view({x.size(0), -1});
x = sigmoid(fc1(x));
return x;
}
};
TORCH_MODULE(DCGANDiscriminator);
您的代码没有使用
conv4
,因此您有[批次x 256 x高度x宽度]
尺寸。在forward
中应用conv4
并检查错误是否仍然存在(您可能还需要在torch::Tensor
之后在conv4
上应用revorme
查看操作)。谢谢@SzymonMaszke我仍然有一个尺寸问题,但我已经解决了。太好了,请将您的答案标记为解决您的问题的答案,谢谢。
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: size mismatch, m1: [131072 x 8], m2: [36 x 1] at ../aten/src/TH/generic/THTensorMath.cpp:136
using namespace torch;
class DCGANDiscriminatorImpl: public nn::Module {
private:
nn::Conv2d conv1, conv2, conv3, conv4;
nn::BatchNorm2d batch_norm1, batch_norm2;
nn::Linear fc1;
public:
DCGANDiscriminatorImpl()
:conv1(nn::Conv2dOptions(3, 64, 4).stride(2).padding(1).bias(false)),
conv2(nn::Conv2dOptions(64, 128, 4).stride(2).padding(1).bias(false)),
batch_norm1(128),
conv3(nn::Conv2dOptions(128, 256, 4).stride(2).padding(1).bias(false)),
batch_norm2(256),
conv4(nn::Conv2dOptions(256, 64, 3).stride(1).padding(0).bias(false)),
fc1(6*6*64, 1)
{
register_module("conv1", conv1);
register_module("conv2", conv2);
register_module("conv3", conv3);
register_module("conv4", conv4);
register_module("batch_norm1", batch_norm1);
register_module("batch_norm2", batch_norm2);
register_module("fc1", fc1);
}
Tensor forward(torch::Tensor x)
{
x = leaky_relu(conv1(x), cte::NEGATIVE_SLOPE);
x = leaky_relu(batch_norm1(conv2(x)), cte::NEGATIVE_SLOPE);
x = leaky_relu(batch_norm2(conv3(x)), cte::NEGATIVE_SLOPE);
x = leaky_relu(conv4(x), cte::NEGATIVE_SLOPE);
x = x.view({x.size(0), -1});
x = sigmoid(fc1(x));
return x;
}
};
TORCH_MODULE(DCGANDiscriminator);