如何正确地转发辍学布局

2024-06-16 10:37:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了以下具有如下辍学层的深度网络:

class QNet_dropout(nn.Module):

    """
        A MLP with 2 hidden layer and dropout

        observation_dim (int): number of observation features
        action_dim (int): Dimension of each action
        seed (int): Random seed
    """

    def __init__(self, observation_dim, action_dim, seed):
        super(QNet_dropout, self).__init__()
        self.seed = torch.manual_seed(seed)
        self.fc1 = nn.Linear(observation_dim, 128)
        self.fc2 = nn.Dropout(0.5)
        self.fc3 = nn.Linear(128, 64)
        self.fc4 = nn.Dropout(0.5)
        self.fc5 = nn.Linear(64, action_dim)

    def forward(self, observations):
        """
           Forward propagation of neural network

        """

        x = F.relu(self.fc1(observations))
        x = F.linear(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = F.linear(self.fc4(x))
        x = self.fc5(x)
        return x

但是,当我试图运行代码时,我遇到了以下错误:

^{pr2}$

好像我没有正确地使用/转发dropout层。什么是正确的方法来做的辍学层前进?谢谢!


Tags: ofselfinitdefactionnndropoutint
1条回答
网友
1楼 · 发布于 2024-06-16 10:37:06

F.linear()函数使用错误。你应该用你所说的线性函数代替torch.nn.功能. 辍学层应该在Relu之后。您可以从调用Relu函数torch.nn.功能. 在

import torch
import torch.nn.functional as F

class QNet_dropout(nn.Module):

    """
        A MLP with 2 hidden layer and dropout

        observation_dim (int): number of observation features
        action_dim (int): Dimension of each action
        seed (int): Random seed
    """

    def __init__(self, observation_dim, action_dim, seed):
        super(QNet_dropout, self).__init__()
        self.seed = torch.manual_seed(seed)
        self.fc1 = nn.Linear(observation_dim, 128)
        self.fc2 = nn.Dropout(0.5)
        self.fc3 = nn.Linear(128, 64)
        self.fc4 = nn.Dropout(0.5)
        self.fc5 = nn.Linear(64, action_dim)

    def forward(self, observations):
        """
           Forward propagation of neural network

        """
        x = self.fc2(F.relu(self.fc1(observations)))
        x = self.fc4(F.relu(self.fc3(x)))
        x = self.fc5(x)
        return x

observation_dim = 512
model = QNet_dropout(observation_dim, 10, 512)
batch_size = 8
inpt  = torch.rand(batch_size, observation_dim)
output = model(inpt)
print ("output shape: ", output.shape)

相关问题 更多 >