When giving an input to this code: class ActorConvNetwork(nn.Module): def init(self,…
- User
When giving an input to this code:
class ActorConvNetwork(nn.Module): def init(self, n_actions, input_dims, alpha, fc1_dims=256, fc2_dims=256, chkpt_dir='tmp/ppo'): super(ActorConvNetwork, self).init()
self.checkpoint_file = os.path.join(chkpt_dir, 'actor_torch_ppo') """ self.conv1 = nn.Conv2d(4, 32, kernel_size=8, stride=4) self.conv2 = nn.Conv2d(32, 64, kernel_size=4, stride=2) self.conv3 = nn.Conv2d(64, 64, kernel_size=3, stride=1) self.bn = nn.BatchNorm2d(32) self.bn2 = nn.BatchNorm2d(64) self.bn3 = nn.BatchNorm2d(64) self.fc1 = nn.Linear(7 * 7 * 64, 512) self.adv = nn.Linear(512, action_size) """ self.actor = nn.Sequential( nn.Conv2d(4, 32, kernel_size=8, stride=4), nn.ReLU(), nn.Conv2d(32, 64, kernel_size=4, stride=2), nn.ReLU(), nn.Conv2d(64, 64, kernel_size=3, stride=1), nn.ReLU(), nn.Linear(7 * 7 * 64, 512), nn.ReLU(), nn.Linear(512, n_actions), nn.Softmax(dim=-1) ) self.optimizer = optim.Adam(self.parameters(), lr=alpha) self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') self.to(self.device) def forward(self, state): dist = self.actor(state) dist = Categorical(dist) return dist def save_checkpoint(self): torch.save(self.state_dict(), self.checkpoint_file) def load_checkpoint(self): self.load_state_dict(torch.load(self.checkpoint_file))I get the error:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (448x7 and 3136x512)
My input is of size 1x4x84x84
- Assistant
The error you're encountering is due to a mismatch in tensor shapes when transitioning from the convolutional layers to the fully connected (
Linear) layers. Specifically, the output from your convolutional layers is not being properly reshaped before being passed into theLinearlayer, which expects a 2D input of shape[batch_size, features].In your
nn.Sequentialdefinition, after the last convolutional layer (), the output tensor has the shape . However, the layer expects an input of shape . Without flattening the tensor, PyTorch attempts to apply the linear transformation across the wrong dimensions, leading to the shape mismatch and the runtime error you've observed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.