欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

【Python-ML】神经网络激励函数-Softmax

发布时间:2025/4/16 python 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【Python-ML】神经网络激励函数-Softmax 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
# -*- coding: utf-8 -*- ''' Created on 2018年1月27日 @author: Jason.F @summary: 前馈神经网络激励函数-softmax函数,评估多类别分类任务中的类别概率 ''' import numpy as np import timeif __name__ == "__main__": start = time.clock() def net_input(X,w):z=X.dot(w)return zdef softmax(z):return np.exp(z)/np.sum(np.exp(z))def softmax_activation(X,w):z=net_input(X,w)return softmax(z)#W:array,shape=[n_output_units,n_hidden_units+1],weight matrix for hidden layer --> output layer#note that first column (A[:][0]=1) are the bias units.W=np.array([[1.1,1.2,1.3,0.5],[0.1,0.2,0.4,0.1],[0.2,0.5,2.1,1.9]])#A:array,shape=[n_hiddern+1,n_samples],Activation of hidden layer.#note that first element (A[0][0]=1) is the bias unit.A=np.array([[1.0],[0.1],[0.3],[0.7]])#Z:array,shape=[n_output_units,n_samples],Net input of the output layer.Z=W.dot(A)y_probas = softmax(Z)print ('Probabilities:\n',y_probas)print (y_probas.sum())y_class = np.argmax(Z,axis=0)print ('predicted class label:%d'%y_class[0])end = time.clock() print('finish all in %s' % str(end - start))


结果:

('Probabilities:\n', array([[ 0.40386493],[ 0.07756222],[ 0.51857284]])) 1.0 predicted class label:2 finish all in 0.00170994801643

总结

以上是生活随笔为你收集整理的【Python-ML】神经网络激励函数-Softmax的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。