基于模型的多样性采样与异常值检测
1. 从PyTorch隐藏层获取信息
在模型中获取隐藏层的值(z值,即logits),需要修改代码以访问这些信息。在PyTorch中,代码相对简单。
原始的前向传播代码如下:
def forward(self, feature_vec):
# Define how data is passed through the model
hidden1 = self.linear1(feature_vec).clamp(min=0) # ReLU
output = self.linear2(hidden1)
return F.log_softmax(output, dim=1)
为了返回所有层的信息,我们可以添加一个参数 return_all_layers ,修改后的代码如下:
def forward(self, feature_vec, return_all_layers=False):
# Define how data is passed through the model and what is returned
hidden1 = self.linear1(feature_vec).clamp(min=0) # ReLU
output = self.linear2(hidden1)
log_softmax = F.log_softmax(output, dim=1)
超级会员免费看
订阅专栏 解锁全文
1452

被折叠的 条评论
为什么被折叠?



