Categorical类
import mindspore
import mindspore.nn as nn
import numpy as np
from mindspore import Tensor
from mindspore.nn.probability.distribution import Categorical
class Actor(nn.Cell):
def __init__(self, state_dim, action_dim):
super().__init__()
self.fc = nn.SequentialCell([
nn.Dense(in_channels=state_dim, out_channels=32),
nn.ReLU(),
nn.Dense(in_channels=32, out_channels=action_dim),
nn.Tanh()
])
self.softmax = nn.Softmax(axis=-1)
def construct(self, state):
out = self.fc(state)
return self.softmax(out)
actor = Actor(4, 2)
for i in range(1000):
x = Tensor(np.random.random(size=(10, 4)),dtype=mindspore.float32)
y = actor(x)
print(y)
cat = Categorical(y)
index = cat.sample()
print(index)
【操作步骤&问题现象】
今天使用了nn.softmax函数对神经网络输出处理,经过对神经网络的输出处理后,变为一个相加为1的Tensor,然后我用这个Tensor去输入到Categorical中,在训练过程中,有时会出现数据放到Categorical里面会出现相加不为1的错误
对于同样的数据,我把它放到pytorch的Categorical类里面处理发现没有错误,由此我在想是否是因为精度的原因,比如X为此tensor,允许X.sum() = 1 - epsilon,这个epsilon是否可以调大一点?
mindspore1.5-cpu,python3.7.5,windows 10环境下,Categorical类中的精度调整之后发现就不报错了。