Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

RuntimeError: Given groups=1, weight of size [32, 1, 3, 3], expected input[1, 3, 6, 7] to have 1 channels, but got 3 channels instead

Ask Question

Model is training normally, when it is passed to this network:

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 array is modified to 3x6x7 numpy array:

<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]]]

After modifying the array, it is giving this error:

RuntimeError: Given groups=1, weight of size [32, 1, 3, 3], expected input[1, 3, 6, 7] to have 1 channels, but got 3 channels instead

In order to solve this problem, I have tried to change the number of channels:

n_input_channels = 3

However, now it is showing this error:

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

How can I make network accept 3x6x7 array?

Update: I provide more code to make my case clear:

6x7 input array case:

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

Output:

<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]]]

Number of channels is 3:

n_input_channels = 1

It is working.

I am trying to modify array to 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

Output:

<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]]]

Number of channels is 3:

    n_input_channels = 3

I do not understand why it is showing this error:

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

Your model can work with either 1 channel input, or 3 channels input, but not both.

If you set n_input_channels=1, you can work with 1x6x7 input arrays.
If you set n_input_channels=3, you can work with 3x6x7 input arrays.

You must pick one of the options - you cannot have them both simultanously.

thank you for your answer. I want to work with 3x6x7 input array. As I wrote in question, I am getting error: 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. I want to the network accept 3x6x7 input array – Joe Rakhimov Nov 29, 2020 at 15:00 @JoeRakhimov you set n_input_channels=3 but the input array you used was 6x7 and not 3x6x7 - this is what the error message you got said. – Shai Nov 29, 2020 at 15:25 I think I am using 3x6x7 input array. I have added code how I am doing it. Please have a look in Update part – Joe Rakhimov Nov 29, 2020 at 16:03 thank you for your answer again, problem was in another place. I have this: shape=(1, self.rows, self.columns). Changed to this shape=(3, self.rows, self.columns). Now it is working – Joe Rakhimov Nov 29, 2020 at 23:34

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.