简体   繁体   English

Tensorflow第一层神经元的权重不变

[英]Tensorflow first layer neuron's weights don't change

Is it ok if my first layer neuron's weights don't change ? 如果我的第一层神经元的重量不变,可以吗?

I'm on the MNIST network on Tensorflow and I've tried to get the neuron's weights like this in the "inference" function: 我在Tensorflow上的MNIST网络上,并且尝试通过“推理”功能获取神经元的权重:

def inference(images, hidden1_units, hidden2_units):

    weights = []

    # Hidden 1
    with tf.name_scope('hidden1'):
        weights.append(tf.Variable( tf.truncated_normal([IMAGE_PIXELS, hidden1_units], stddev=1.0 / math.sqrt(float(IMAGE_PIXELS)))))
        biases = tf.Variable(tf.zeros([hidden1_units]))
        hidden1 = tf.nn.relu(tf.matmul(images, weights[0]) + biases)

    # Hidden 2
    with tf.name_scope('hidden2'):
        weights.append(tf.Variable(tf.truncated_normal([hidden1_units, hidden2_units],stddev=1.0 / math.sqrt(float(hidden1_units)))))
        biases = tf.Variable(tf.zeros([hidden2_units]))
        hidden2 = tf.nn.relu(tf.matmul(hidden1, weights[1]) + biases)

    # Linear
    with tf.name_scope('softmax_linear'):
        weights.append(tf.Variable(tf.truncated_normal([hidden2_units, NUM_CLASSES],stddev=1.0 / math.sqrt(float(hidden2_units)))))
        biases = tf.Variable(tf.zeros([NUM_CLASSES]))
        logits = tf.matmul(hidden2, weights[2]) + biases
    return weights, logits

I create an array which where I put the weight's arrays. 我创建一个放置权重数组的数组。

I print my array like this : 我这样打印我的数组:

print_weights(sess.run(poids))

where print_weights is 在哪里print_weights

def print_weights(poids):
    for i in range(len(poids)):
        print('--  + str(i) + ' --')
        print(poids[i])

Until here, all is fine. 到这里为止,一切都很好。 But I display the weights at the beginning and at the end and the first layer neuron's weights havn't changed. 但是我在开始和结束时显示权重,并且第一层神经元的权重没有改变。

BEGINNING

-- 0 --

[[ 0.03137168  0.03483023]
 [ 0.01353009  0.00035462]
 [ 0.02957422 -0.01347954]
 ..., 
 [-0.04083598  0.02377481]
 [-0.05120984  0.00143244]
 [-0.01799158 -0.02219945]]

-- 1 --

[[ 0.68714064]
 [ 0.30847442]]

-- 2 --

[[ 0.87441564  0.09957008 -0.58042473  1.34084558 -0.46372819 -0.19947429
  -1.46314788 -0.59285629  0.72775543 -0.69785988]]


END

-- 0 --

[[ 0.03137168  0.03483023]
 [ 0.01353009  0.00035462]
 [ 0.02957422 -0.01347954]
 ..., 
 [-0.04083598  0.02377481]
 [-0.05120984  0.00143244]
 [-0.01799158 -0.02219945]]

-- 1 --

[[-1.16852498]
 [-0.27643263]]

-- 2 --

[[ 0.98213464  0.12448452 -0.36638314  0.47689819 -0.42525211 -0.13292283
  -1.29118276 -0.49366322  0.74673325 -0.57575113]]

As you can see, the seconds and the thirds weight's array change, but not the firsts and I don't know why ... Someone could help me please ? 如您所见,秒和三分之二的权重数组发生了变化,但第一和三分之二没有变化,我也不知道为什么...有人可以帮助我吗? Thanks ! 谢谢 !

I wrapped your code up in a training harness and ran it without issue. 我将您的代码包装在一个训练工具中,并且毫无问题地运行了它。

I think the problem here is not your code but the interpretation of the results. 我认为这里的问题不是您的代码,而是结果的解释。 Numpy summarizes large arrays in the way you've shown, by displaying the first couple and last couple elements. Numpy通过显示前几对和最后几对元素,以您显示的方式汇总了大数组。 (The elements of your poids list are np.array 's.) (您的poids列表的元素是np.array 。)

What you're seeing is that the first couple and last couple weight elements aren't changing, but your conclusion is that the entire matrix isn't changing— but it is! 您看到的是前几对和最后几对权重元素没有变化,但是您的结论是整个矩阵都没有变化- 而是!

Try using this as a summary method instead (print the mean and standard deviation instead of just a few elements): 尝试使用此方法作为汇总方法(打印平均值和标准偏差,而不是仅打印一些元素):

def print_weights(poids):
  for i in range(len(poids)):
    print('-- ' + str(i) + ' --')
    print(np.mean(poids[i]),np.std(poids[i]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM