f = np.array([123,456,789])# example with 3 classes and each having large scores
p = np.exp(f)/ np.sum(np.exp(f))# Bad: Numeric problem, potential blowup# instead: first shift the values of f so that the highest number is 0:
f -= np.max(f)# f becomes [-666, -333, 0]
p = np.exp(f)/ np.sum(np.exp(f))# safe to do, gives the correct answer
m = np.random.rand(10,10)*10+1000print(m)#out<<[[1008.643040121001.250792291006.818968681005.890152581008.89152971001.849238661005.535097341005.340753051008.934047091006.94897664][1003.242678251003.727107411000.283543981000.320121051004.36903611007.183906021002.497416061005.835103321009.196783961002.32098566][1002.328240021006.28139991009.276456621002.572591591006.307436271000.352013231003.944300991008.790568691007.404858411006.38239542][1007.062287141006.013253521007.969018641002.342695421000.755632211005.263573171006.148611741005.681190441000.690064531007.21834125][1004.157704281003.05548481005.556190321003.040000251005.543384681002.239526381008.863178571006.969837891005.842323181009.28833837][1008.471516671006.303549271006.692740161004.124185431007.175509721004.317582921007.277604991007.452504451000.029432391002.25886446][1000.637647811003.398942761008.262987591001.892950121007.853883691004.675652551004.588727081003.244888151000.395289141007.20964465][1005.218153081007.426513551006.324077171003.00963291005.035459021008.859254371009.576344181003.745460241003.405128671004.4437606][1001.787866251008.732823771003.989062671008.175339411002.799575841000.893326661007.643439991003.882482111005.755175661008.27556001][1002.059160591007.256633921009.486557751009.568315641008.284880621004.925938541008.04685651007.532786211001.949351211007.01473574]]#np.exp(m)#输出每一行中的最大值
m_row_max = m.max(axis =1).reshape(10,1)print(m_row_max, m_row_max.shape)#out<<[[1008.93404709][1009.19678396][1009.27645662][1007.96901864][1009.28833837][1008.47151667][1008.26298759][1009.57634418][1008.73282377][1009.56831564]](10,1)
m = m - m_row_max
print(m)#out<<[[-0.29100696-7.6832548-2.11507841-3.04389451-0.04251738-7.08480843-3.39894975-3.593294030.-1.98507045][-5.95410571-5.46967655-8.91323998-8.87666291-4.82774786-2.01287794-6.6993679-3.361680650.-6.8757983][-6.9482166-2.995056730.-6.70386503-2.96902035-8.9244434-5.33215563-0.48588793-1.87159821-2.8940612][-0.9067315-1.955765120.-5.62632322-7.21338643-2.70544547-1.82040689-2.2878282-7.27895411-0.75067739][-5.13063409-6.23285357-3.73214805-6.24833812-3.74495369-7.04881199-0.4251598-2.31850048-3.446015180.][0.-2.16796739-1.77877651-4.34733123-1.29600694-4.15393374-1.19391168-1.01901222-8.44208427-6.21265221][-7.62533977-4.864044830.-6.37003747-0.4091039-3.58733504-3.67426051-5.01809944-7.86769844-1.05334294][-4.3581911-2.14983063-3.25226701-6.56671127-4.54088515-0.717089810.-5.83088393-6.1712155-5.13258358][-6.944957530.-4.7437611-0.55748437-5.93324793-7.83949711-1.08938379-4.85034166-2.97764811-0.45726376][-7.50915505-2.31168172-0.081757890.-1.28343501-4.6423771-1.52145914-2.03552943-7.61896443-2.5535799]]