有6x7 numpy数组:
<class 'numpy.ndarray'>
[[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]]
模型正在正常训练,当它被传递到此网络时:
class Net(BaseFeaturesExtractor):
def __init__(self, observation_space: gym.spaces.Box, features_dim: int = 256):
super(Net, self).__init__(observation_space, features_dim)
# We assume CxHxW images (channels first)
# Re-ordering will be done by pre-preprocessing or wrapper
# n_input_channels = observation_space.shape[0]
n_input_channels = 1
print("Input channels:", n_input_channels)
self.cnn = nn.Sequential(
nn.Conv2d(n_input_channels, 32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=0),
nn.ReLU(),
nn.Flatten(),
)
# Compute shape by doing one forward pass
with th.no_grad():
n_flatten = self.cnn(
th.as_tensor(observation_space.sample()[None]).float()
).shape[1]
self.linear = nn.Sequential(nn.Linear(n_flatten, features_dim), nn.ReLU())
def forward(self, observations: th.Tensor) -> th.Tensor:
return self.linear(self.cnn(observations))
6x7 numpy数组修改为3x6x7 numpy:
<class 'numpy.ndarray'>
[[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[1 1 1 1 1 1 1]]]
修改数组后,它给出了以下错误:
RuntimeError:给定组=1,大小为[32,1,3,3]的权重,应为输入[1,3,6,7]有1个通道,但得到了3个通道而不是
为了解决这个问题,我尝试更改通道的数量:
n_input_channels = 3
然而,现在它显示了这个错误:
RuntimeError:给定组=1,大小为[32,3,3,3]的权重,应为输入[1,1,6,7]有3个通道,但得到了1个通道而不是
如何使网络接受3x6x7阵列?
更新:我提供了更多的代码来澄清我的情况:
6x7输入阵列情况:
...
board = np.array(self.obs['board']).reshape(1, self.rows, self.columns)
# board = board_3layers(self.obs.mark, board)
print(type(board))
print(board)
return board
输出:
<class 'numpy.ndarray'>
[[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]]
通道数为3:
n_input_channels = 1
它正在发挥作用。
我正在尝试将数组修改为3x6x7:
board = np.array(self.obs['board']).reshape(1, self.rows, self.columns)
board = board_3layers(self.obs.mark, board)
print(type(board))
print(board)
return board
输出:
<class 'numpy.ndarray'>
[[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[1 1 1 1 1 1 1]]]
通道数为3:
n_input_channels = 3
我不明白为什么它显示这个错误:
RuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[1, 1, 6, 7] to have 3 channels, but got 1 channels instead
您的模型可以使用1通道输入或3通道输入,但不能同时使用这两种输入。
如果设置了n_input_channels=1
,则可以使用1x6x7
输入数组
如果设置了n_input_channels=3
,则可以使用3x6x7
输入数组。
你必须选择其中一个选项——你不能同时拥有这两个选项。