import torch import torch.nn as nn import torch.nn.functional as F class CNNPolicyNetwork(nn.Module): def __init__(self, input_channels, output_dim): super(CNNPolicyNetwork, self).__init__() # Define convolutional layers self.conv1 = nn.Conv2d(input_channels, 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) # Define fully connected layers self.fc1 = nn.Linear(64 * 7 * 7, 512) self.fc2 = nn.Linear(512, output_dim) def forward(self, x): # Pass input through convolutional layers with ReLU activation x = F.relu(self.conv1(x)) x = F.relu(self.conv2(x)) x = F.relu(self.conv3(x)) # Flatten the tensor before passing to fully connected layers x = x.view(x.size(0), -1) x = F.relu(self.fc1(x)) # Output layer with softmax to get probabilities x = F.softmax(self.fc2(x), dim=-1) return x