【发布时间】:2018-09-25 00:36:53
【问题描述】:
我正在尝试使用 PyTorch 的 2D 卷积验证一些结果:
- 输入矩阵:X (10, 10, 3) [虚拟 numpy 图像]
- 权重矩阵:W (3, 3, 3) [My Conv Filter to test]
- 输出矩阵:Y (10, 10, 1)
我有以下代码,但我无法正确分配权重并运行模型而不会出错。 我在这里做错了什么??
import torch
import torch.nn as nn
import torchvision.transforms
import numpy as np
# Convert image to tensor
image2tensor = torchvision.transforms.ToTensor()
class ConvNet(nn.Module):
def __init__(self, num_classes=10):
super(ConvNet, self).__init__()
# Test layer
self.layer1 = nn.Conv2d(3, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
def forward(self, x):
out = self.layer1(x)
return out
# Test image
image = np.ones((10, 10, 3))
tensor = image2tensor(image).unsqueeze(0)
# Create new model
conv = ConvNet()
# Assign test weight - NOT WORKING!!
weight = torch.nn.Parameter(torch.ones(3, 3, 3))
conv.layer1.weight.data = weight
# Run the model
output = conv(tensor)
【问题讨论】:
标签: python conv-neural-network pytorch