简体   繁体   中英

How can I separate the last layer of deep network in pytorch?

My deep network is:

self.actor = nn.Sequential(
   nn.Linear(state_dim, 256),
   nn.Softplus(),
   nn.Linear(256, 256),
   nn.Softplus(),
   nn.Linear(256, action_dim),
   nn.Softplus())

Now, I would like the network to give two separate outputs, like this:
演示网络

That's to say, only the last layer of the network is different and maybe the last layer has different activation functions. How should I change my code above?

The model you want to build is not sequential anymore, since there are two parallel branches at the end. You can keep the common trunk and separate with two additional separate layers. Something like:

class Model(nn.Module):
    def __init__(self):
        super.__init__()

        self.actor = nn.Sequential(
            nn.Linear(state_dim, 256),
            nn.Softplus(),
            nn.Linear(256, 256),
            nn.Softplus())
      
        self.outA = nn.Sequential(
            nn.Linear(256, action_dim),
            nn.Softplus())

        self.outB = nn.Sequential(
            nn.Linear(256, action_dim),
            nn.Softplus())
  
    def forward(self, x):
      features = self.actor(x)
      return self.outA(features), self.outB(features)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM