本文整理汇总了Python中torch.distributions.categorical.Categorical.rsample方法的典型用法代码示例。如果您正苦于以下问题:Python Categorical.rsample方法的具体用法?Python Categorical.rsample怎么用?Python Categorical.rsample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.distributions.categorical.Categorical
的用法示例。
在下文中一共展示了Categorical.rsample方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: range
# 需要导入模块: from torch.distributions.categorical import Categorical [as 别名]
# 或者: from torch.distributions.categorical.Categorical import rsample [as 别名]
for step in range(n_steps):
optim.zero_grad()
loss = 0
net_loss = 0
surr_loss = 0
for i in range(batch_size):
x = sample_true().cuda().view(1,1)
logits = encoder.net(x)
# print (logits.shape)
# print (torch.softmax(logits, dim=1))
# fasd
# cat = Categorical(probs= torch.softmax(logits, dim=0))
cat = RelaxedOneHotCategorical(probs= torch.softmax(logits, dim=1), temperature=torch.tensor([1.]).cuda())
cluster_S = cat.rsample()
cluster_H = H(cluster_S)
# cluster_onehot = torch.zeros(n_components)
# cluster_onehot[cluster_H] = 1.
# print (cluster_onehot)
# print (cluster_H)
# print (cluster_S)
# fdsa
logprob_cluster = cat.log_prob(cluster_S.detach())
if logprob_cluster != logprob_cluster:
print ('nan')
# print (logprob_cluster)
pxz = logprob_undercomponent(x, component=cluster_H, needsoftmax_mixtureweight=needsoftmax_mixtureweight, cuda=True)
# print (pxz, logprob_cluster)
# fafd
f = pxz - logprob_cluster