一开始按照里面的例子输入并验证了下,结果还比较符合预期,但是前后又看了下原理及代码,怎么没有看到这些权重及偏置参数被不断更新(训练)呢?
我自己的理解就是,神经网络主要是建立模型,这个模型有很多参数需要训练,但是在线性回归及逻辑回归的两篇文章中,并没有直接显示如何去更新这些参数。
如线性回归中,是这么写的:
update_weights =tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
逻辑回归中,是这么写的:
optimizer =tf.train.MomentumOptimizer(learning_rate, 0.9).minimize(loss,global_step=batch)
实际上,对于线性回归里的参数更新,他们隐含在函数实现里,
如:update_weights =tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
真正分解应该是下面这个样子,这样看起来就比较直观了!
#Change the weights by subtracting derivative with respect to that weight
loss = 0.5 *tf.reduce_sum(tf.multiply(yerror, yerror))
gradient = tf.reduce_sum(tf.transpose(tf.multiply(input,yerror)), 1, keep_dims=True)
update_weights = tf.assign_sub(weights,learning_rate * gradient)
而在tensorflow 平台下面,这些weights and bias又是如何传入的,
正如这篇stackoverflow 里的问题一样,
TensorflowGradientDescentOptimizer - how does it connect to tf.Variables?
How doestensorflow know I want it to update W
and b
? Does it just see that those are the only Variables in thesession?
It takes them from tf.trainable_variables() which includes all variables created with trainable=True flag (the default)
这样终于搞明白,这些参数是怎么得到更新及如何传人到这些黑盒子函数里的。