Softmax by Python
import math
#import numpy as np
def softmax_py(logits_data):
logits_exp = [math.exp(i) for i in logits_data]
#logits_exp = [np.exp(i) for i in logits]
print("logits_exp:",logits_exp)
sum_logits_exp = sum(logits_exp)
print("sum_logits_exp:",sum_logits_exp)
softmax = [round(i/sum_logits_exp,3) for i in logits_exp]
print("softmax:",softmax)
print("sum_softmax:",sum(softmax))
softmax_py([2.0,1.0,0.1])
logits_exp: [7.38905609893065, 2.718281828459045, 0.1051709180756477]
sum_logits_exp: 11.212508845465344
softmax: [0.659, 0.242, 0.099]
sum_softmax: 1.0
#问题一:为什么用numpy,softmax结果不止三位小数,round没效果?
SoftMax by tensorflow
import tensorflow as tf
def softmax_tf(logits_data):
output = None