【问题标题】:How to store the hidden layer as a variable to visualize it如何将隐藏层存储为变量以将其可视化
【发布时间】:2021-12-21 20:22:37
【问题描述】:
class MLP(nn.Module):
  def __init__(self):
    super().__init__()

    self.in_dim = 28 * 28
    self.out_dim = 10

    self.fc1 =nn.Linear(self.in_dim,512)

    self.fc2=nn.Linear(512, 256)
    self.fc3 =nn.Linear(256, 128)
    self.fc4 =nn.Linear(128, 64)
    self.fc5 =nn.Linear(64, self.out_dim)

    self.relu = nn.ReLU()

  def forward(self, x):


      a1 = self.relu(self.fc1(x.view(-1,self.in_dim)))    

      a2 = self.relu(self.fc2(a1))
      a3 = self.relu(self.fc3(a2))
      a4 = self.relu(self.fc4(a3))
      logit = self.fc5(a4)

      return logit

例如,在上面的代码中,我想将 a1 和 (self.fc1(x.view(-1, self.in_dim))) 部分存储为单独的变量以进行可视化并使其成为 NumPy。你好吗?

我得到了使用 np.save(filepath, a1.numpy()) 保存的提示,但这是错误的,所以我再次询问。

【问题讨论】:

  • np.save 有什么问题?

标签: python deep-learning jupyter-notebook pytorch artificial-intelligence


【解决方案1】:
  • 您可以在 return 语句中添加 a1a2 ...,然后在您的代码中使用它们

  • 要将它们投射到numpy.ndarray,您可以使用a1.detach().cpu().numpy() 之类的东西

  • 至于可视化,它确实取决于您获得的张量 // numpy 数组的形状。

【讨论】:

    猜你喜欢
    • 2017-08-06
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    相关资源
    最近更新 更多