일반 Softmax 함수 


import tensorflow as tf
import numpy as np

z = [1,2,3,4,5,6]

def softmax(a) : 
    c = np.max(a) 
    exp_a = np.exp(a-c) 
    sum_exp_a = np.sum(exp_a)
    y = exp_a / sum_exp_a
    return y

q = softmax(z)


Softmax with Temperature Parameter


def softmax_with_temperature(z, T) : 
    z = np.array(z)
    z = z / T 
    max_z = np.max(z) 
    exp_z = np.exp(z-max_z) 
    sum_exp_z = np.sum(exp_z)
    y = exp_z / sum_exp_z
    return y

calibrated_q2 = softmax_with_temperature(z, 2)
calibrated_q10 = softmax_with_temperature(z, 10)


Temperature 를 크게 줄 수록 각 확률 값들의 차이가 줄어듬을 확인할 수 있음

하지만, 순서는 변하지 않기 때문에 정확도에 영향을 주지 않음 


fig = sns.barplot(x=np.array([0,1,2,3,4,5]), y=q)
fig.set(ylim=(0,1))
plt.show()

fig2 = sns.barplot(x=np.array([0,1,2,3,4,5]), y=calibrated_q2)
fig2.set(ylim=(0,1))
plt.show()

fig3 = sns.barplot(x=np.array([0,1,2,3,4,5]), y=calibrated_q10)
fig3.set(ylim=(0,1))
plt.show()