Theano入门神经网络(四)

这一部分主要介绍用Theano 实现softmax函数。

在多分类任务中经常用到softmax函数,首先上几个投影片解释一下

假设目标输出是y_hat ,激活函数后的Relu y 一个是1.2 一个是2 ,到底哪一个更好一点 是1.2与1 更加接近?还是2 意味这更大的概率呢?

基于这个原因,我们提出了softmax

将得到的输出进行指数运算,转化为其概率。越大的数值,代表其类别的可能性越大。

上个代码: 

 1 x= T.vector();
 2 W= T.matrix();
 3 z = T.dot(W,x)
 4 #y = 1.0/(1.0+np.exp(-z))
 5 y = np.exp(z)/T.sum(np.exp(z), axis=0) # 0  是按列 1 是按行
 6 log_activation = theano.function(inputs=[W,x],outputs=y)
 7 
 8 x = np.array([1 ,1.4 ,1.5],dtype=theano.config.floatX)
 9 W = np.array([[1.1,1.2,1.3],[0.1,0.2,0.4],[0.2,0.5,2.1]],dtype=theano.config.floatX)
10 
11 for i in range(x.shape[0]):
12    print '第 %d 的 log_activation 的输出是 %.2f ' %( i+1, log_activation(W,x)[i])
13 
14 c=log_activation(W,x)
15 y_class = np.argmax(c,axis=0)
16 print y_class+1

 

一般来说 softmax 函数和 交叉熵损失一起使用

 

 

posted on 2016-08-15 15:36  mitutao  阅读(600)  评论(0编辑  收藏  举报

导航