已知一个数组:X=[x1, x2, x3] ( exp(x)代表幂指数 )
x1的模拟概率为:p(x1)=exp(x1) / ( exp(x1)+exp(x2)+exp(x3))
x2的模拟概率为:p(x2)=exp(x2) / ( exp(x1)+exp(x2)+exp(x3))
x3的模拟概率为:p(x3)=exp(x3) / ( exp(x1)+exp(x2)+exp(x3))
import tensorflow as tf
x = tf.constant([[0.1, 0.2, 0.5],
[0.3, 0.4, 0.2]])
y0 = tf.nn.softmax(x, axis=0) # 求每列的概率
y1 = tf.nn.softmax(x, axis=1) # 求每行的概率
sess = tf.Session()
print(sess.run(y0),'\n')
print(sess.run(y1))
运行结果:
[[0.450166 0.450166 0.5744425 ]
[0.549834 0.54983395 0.42555746]]
[[0.27800977 0.30724832 0.41474187]
[0.332225 0.3671654 0.3006096 ]]