如何将分类数据类型转换为numpy数组?

时间:2019-01-16 04:07:47

标签: python numpy pytorch

我尝试使用此代码。输出为:Categorical(probs: torch.Size([12])))。我想从输出中提取值并将其转换为numpy数组。有人可以提出建议吗?

我知道我可以返回a的值(已注释)。但是,仍然有解决方案吗?

class Actor(nn.Module):
def __init__(self, state_size, action_size):
    super(Actor, self).__init__()
    self.state_size = state_size
    self.action_size = action_size
    self.linear1 = nn.Linear(self.state_size, 128)
    self.linear2 = nn.Linear(128, 256)
    self.linear3 = nn.Linear(256, self.action_size)

def forward(self, state):
    output = F.relu(self.linear1(state))
    output = F.relu(self.linear2(output))
    output = self.linear3(output)
    distribution = Categorical(F.softmax(output, dim=-1))
    # a=F.softmax(output, dim=-1)
    # print(a.detach().numpy())
    return distribution

    output=Actor(state)
    print(output)

1 个答案:

答案 0 :(得分:0)

怎么样

prob = output.probs.detach().cpu().numpy()