简体   繁体   English

Tensorflow-重量值在tf.nn.conv2D()中是否已更改?

[英]Tensorflow - Does Weight value changed in tf.nn.conv2D()?

While I'm studying about Neural Network with tensorflow, I got a some question about tf.nn.conv2D(x, W, strides=[1, 1, 1, 1], padding='SAME') 当我研究带有张量流的神经网络时,我遇到了一个关于tf.nn.conv2D(x,W,strides = [1,1,1,1],padding ='SAME')的问题

When I input image value x, and weight value W(initailzed by tf.truncated_normal(shape, stddev=0.1)), I understand it will return some value, which is the result of tf.nn.conv2D(). 当我输入图像值x和权重值W(由tf.truncated_normal(shape,stddev = 0.1)定义)时,我知道它会返回一些值,这是tf.nn.conv2D()的结果。

But my question is, when tf.nn.conv2D() is called, Does It change weight value? 但是我的问题是,当调用tf.nn.conv2D()时,它会改变重量值吗?

If it changes the value of Weight, How does it works? 如果它改变了权重的值,它是如何工作的? In fact, When I print Weight value, it changes. 实际上,当我打印“重量”值时,它会发生变化。 But I don't know why... My assumption is that value W is some kind of call-by-reference, so while computing tf.nn.conv2D(), the value W is changed. 但是我不知道为什么...我的假设是值W是某种按引用调用,因此在计算tf.nn.conv2D()时,值W会更改。 Is it right? 这样对吗?

The Tensorflow code flow is not like your conventional programming language. Tensorflow代码流与您的常规编程语言不同。 First, a graph is created from the code (which can be visualized using Tensorboard , and then update rules are computed using backpropagation which has been implemented internally. 首先,从代码创建一个图形(可以使用Tensorboard可视化),然后使用内部实现的反向传播计算更新规则。

When you write: 当你写:

h = tf.nn.conv2D(x, W, strides=[1, 1, 1, 1], padding='SAME')

it creates a convolutional layer in your neural network, which performs convolution ( http://cs231n.github.io/convolutional-networks/ ) on your input matrix and outputs the result in h . 它在您的神经网络中创建一个卷积层,对您的输入矩阵执行卷积( http://cs231n.github.io/convolutional-networks/ ),并将结果输出到h中 Now, the whole purpose of performing such convolutions is to identify some local patterns such as vertical or horizontal edges in the image. 现在,执行此类卷积的全部目的是识别图像中的某些局部图案,例如垂直或水平边缘。 For example, a weight matrix W such as 例如,权重矩阵W例如

W = [[0,1,0],[0,1,0],[0,1,0]]

would identify vertical edges in the image. 将识别图像中的垂直边缘。 However, since W has been initialized randomly here 但是,由于W已在此处随机初始化

W = tf.Variable(tf.truncated_normal(shape, stddev=0.1)))

would not be able to find any pattern at the outset. 一开始将无法找到任何模式。 This is solved through backpropagation. 这可以通过反向传播解决。

When you train your neural network on a labeled data, at each step the matrix W gets updated such that the derivative of the error E wrt W is reduced. 当您在标记的数据上训练神经网络时,每一步矩阵W都会更新,从而减少了误差E wrt W的导数。 You cannot see it happening in your code because the backpropagation is implemented internally in Tensorflow and you only need to write code for the forward pass. 您无法在代码中看到它的发生,因为反向传播是在Tensorflow内部实现的,并且只需要为正向编写代码。 If you defined W as 如果您将W定义为

W = tf.Variable(tf.truncated_normal(shape, stddev=0.1)),trainable=False)

it won't be updated, but then the entire purpose of training the parameters would be defeated. 它不会被更新,但是训练参数的整个目的将被破坏。

I suggest you to go through http://neuralnetworksanddeeplearning.com to understand how neural networks work, before you proceed with Tensorflow. 我建议您在继续使用Tensorflow之前先浏览http://neuralnetworksanddeeplearning.com来了解神经网络的工作原理。

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

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