Tensorflow ReLU奇怪地正常化

时间:2017-07-28 12:36:47

标签: python tensorflow deep-learning

在我看来,整流线性单元应该执行以下功能:

relu(x) = max(x, 0)

然而,tf.nn.relu

似乎并非如此
import tensorflow as tf
import numpy as np
rand_large = np.random.randn(10, 3)*100
X = tf.placeholder(tf.float32, [10, 3])
sess = tf.Session()
sess.run(tf.nn.relu(X), feed_dict={X:rand_large})

随机矩阵如下所示:

>>> rand_large
array([[  21.94064161,  -82.16632876,   16.25152777],
   [  55.54897693,  -93.15235155,  118.99166126],
   [ -13.36452239,   39.36508285,   65.42844521],
   [-193.34041145,  -97.08632376,   99.22162259],
   [  87.02924619,    2.04134891,  -27.29975745],
   [-181.11406687,   43.55952393,   42.29312993],
   [ -29.81242188,   93.5764354 , -165.62711447],
   [  17.78380711, -171.30536766, -197.20709038],
   [ 105.94903623,   34.07995616,   -7.27568839],
   [-100.59533697, -189.88957685,   -7.52421816]])

relu函数的输出如下:

>>> sess.run(tf.nn.relu(X), feed_dict={X:rand_large})array([[ 1. ,  0.5,  0.5],
   [ 0.5,  0.5,  0.5],
   [ 0.5,  0.5,  0.5],
   [ 0.5,  0.5,  0.5],
   [ 0.5,  0.5,  0.5],
   [ 0.5,  0.5,  0.5],
   [ 0.5,  0.5,  0.5],
   [ 0.5,  0.5,  0.5],
   [ 0.5,  0.5,  0.5],
   [ 0.5,  0.5,  0.5]], dtype=float32)

所以,如果我看到它正确,tf.nn.relu会进行某种规范化,对吧?如果是,为什么在docs

中没有提到它

好的,我发现整个问题与我的tensorflow安装有关,这似乎是腐败的。在另一台机器上,我确实得到了预期的结果。 感谢您的帮助和有用的评论。

1 个答案:

答案 0 :(得分:4)

tf.nn.relu未对数据进行规范化。例如,如果我运行

import tensorflow as tf
import numpy as np
X = tf.placeholder(tf.float32, [2, 3])
relu_X=tf.nn.relu(X)

sess = tf.Session()
mat = np.array([[-1,2,3],[2,-5,1]])
sess.run(relu_X, feed_dict={X:mat})

结果是

array([[ 0.,  2.,  3.],
       [ 2.,  0.,  1.]], dtype=float32)