简体   繁体   English

TensorFlow多层感知器

[英]TensorFlow Multi-Layer Perceptron

I am learning TensorFlow, and my goal is to implement MultiPerceptron for my needs. 我正在学习TensorFlow,我的目标是根据自己的需要实施MultiPerceptron。 I checked the MNIST tutorial with MultiPerceptron implementation and everything was clear to me except this: 我检查了具有MultiPerceptron实现MNIST教程,除此以外 ,其他一切对我都很清楚:

    _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                                  y: batch_y})

I guess, x is an image itself(28*28 pixels, so the input is 784 neurons) and y is a label which is an 1x10 array: 我猜想, x本身就是图像(28 * 28像素,所以输入是784个神经元), y是一个标签,它是一个1x10的数组:

x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])

They feed whole batches (which are packs of data points and labels)! 他们喂整批(这是数据点和标签的包)! How does tensorflow interpret this "batch" input? tensorflow如何解释这个“批量”输入? And how does it update the weights: simultaneously after each element in a batch, or after running through the whole batch? 以及它如何更新权重:在批处理中的每个元素之后还是在整个批处理之后同时进行?

And, if I need to input one number ( input_shape = [1,1] ) and output four numbers ( output_shape = [1,4] ), how should I change the tf.placeholders and in which form should I feed them into session? 并且,如果我需要输入一个数字( input_shape = [1,1] )并输出四个数字( output_shape = [1,4] ),那么我该如何更改tf.placeholders以及将它们以哪种形式输入会话?

  1. When I ask, how does tensorflow interpret it, I want to know how tensorflow splits the batch into single elements. 当我问时,tensorflow如何解释它,我想知道tensorflow如何将批处理拆分为单个元素。 For example, batch is a 2-D array, right? 例如,批处理是二维数组,对吗? In which direction does it split an array? 它朝哪个方向分割数组? Or it uses matrix operations and doesn't split anything? 还是使用矩阵运算并且不拆分任何内容?

  2. When I ask, how should I feed my data, I want to know, should it be a 2-D array with samples at its rows and features at its columns, or, maybe, could it be a 2-D list. 当我问时,我想知道应该如何馈入数据,它应该是一个二维数组,在其行上具有样本,在其列上具有特征,还是应该是一个二维列表。

When I feed my float numpy array X_train to x , which is : 当我将我的float numpy数组X_train x ,它是:

x = tf.placeholder("float", [1, n_input])

I receive an error: 我收到一个错误:

ValueError: Cannot feed value of shape (1, 18) for Tensor 'Placeholder_10:0', which has shape '(1, 1)'

It appears that I have to create my data as a Tensor too? 看来我也必须以张量创建数据?

When I tried [18x1]: 当我尝试[18x1]时:

Cannot feed value of shape (18, 1) for Tensor 'Placeholder_12:0', which has shape '(1, 1)'

They feed whole bathces(which are packs of data points and labels)! 他们喂饱所有东西(一包数据点和标签)!

Yes, this is how neural networks are usually trained (due to some nice mathematical properties of having best of two worlds - better gradient approximation than in SGD on one hand and much faster convergence than full GD). 是的,这是通常训练神经网络的方式(由于具有两个世界中最好的一些数学特性-一方面比SGD中更好的梯度逼近,而且比完整GD中的收敛快得多)​​。

How does tensorflow interpret this "batch" input? tensorflow如何解释这个“批量”输入?

It "interprets" it according to operations in your graph. 它根据图形中的操作“解释”它。 You probably have reduce mean somewhere in your graph, which calculates average over your batch, thus causing this to be the "interpretation". 您可能在图形中的某处具有减少的均值 ,该均值计算了整个批次的平均值 ,因此使之成为“解释”。

And how does it update the weights: 1.simultaniusly after each element in a batch? 以及它如何更新权重:1.在批处理中的每个元素之后同时进行? 2. After running threw the whole batch?. 2.运行后扔了整批?

As in the previous answer - there is nothing "magical" about batch, it is just another dimension, and each internal operation of neural net is well defined for the batch of data, thus there is still a single update in the end. 就像前面的答案一样,批处理没有任何“魔术”,它只是另一个维度,神经网络的每个内部操作都为该批数据定义了很好的定义,因此最后仍然只有一个更新。 Since you use reduce mean operation (or maybe reduce sum?) you are updating according to mean of the "small" gradients (or sum if there is reduce sum instead). 由于您使用减少均值运算(或者可能是减少总和?),因此将根据“小”梯度的平均值(或总和(如果有减少总和的话)进行更新)。 Again - you could control it (up to the agglomerative behaviour, you cannot force it to do per-sample update unless you introduce while loop into the graph). 再说一次-您可以控制它(直到聚集行为为止,除非您在图表中引入while循环,否则您不能强迫它进行每个样本的更新)。

And, if i need to imput one number(input_shape = [1,1]) and ouput four nubmers (output_shape = [1,4]), how should i change the tf.placeholders and in which form should i feed them into session? 并且,如果我需要输入一个数字(input_shape = [1,1])并输出四个小数字(output_shape = [1,4]),我应该如何更改tf.placeholders以及应以哪种形式将它们输入会话? THANKS!! 谢谢!!

just set the variables, n_input=1 and n_classes=4 , and you push your data as before, as [batch, n_input] and [batch, n_classes] arrays (in your case batch=1, if by "1x1" you mean "one sample of dimension 1", since your edit start to suggest that you actually do have a batch, and by 1x1 you meant a 1d input). 只需设置变量n_input=1n_classes=4 ,就可以像以前[batch,n_input]和[batch,n_classes]数组一样推送数据(在您的情况下,batch = 1,如果用“ 1x1”表示“一个尺寸为1“的样本,因为您的编辑开始暗示您实际上确实有一个批次,对于1x1,您表示一个1d输入)。

EDIT: 1.when i ask, how does tensorflow interpret it, i want to know, how tensorflow split the batch into single elements. 编辑:1.当我问时,tensorflow如何解释它,我想知道,tensorflow如何将批处理拆分为单个元素。 For example, batch is a 2-D array, right? 例如,批处理是二维数组,对吗? In which direction it splits an array. 它沿哪个方向分割数组。 Or it uses matrix operations and doesnt split anything? 还是使用矩阵运算并且不拆分任何内容? 2. When i ask, how should i feed my data, i want to know, should it be a 2-D array with samples at its rows and features at its colums, or, maybe, could it be a 2-D list. 2.当我问时,我想知道应该如何输入我的数据,它应该是2D数组,在其行上具有样本,在其列中具有特征,还是应该是2D列表。

It does not split anything. 不会拆分任何内容。 It is just a matrix, and each operation is perfectly well defined for matrices as well. 它只是一个矩阵,对于矩阵,每个操作都定义得很好。 Usually you put examples in rows, thus in first dimension , and this is exactly what [batch, n_inputs] says - that you have batch rows each with n_inputs columns. 通常,您将示例放在行中,因此放在第一维中 ,而这正是[batch,n_inputs]所说的-您有batch行,每行都有n_inputs列。 But again - there is nothing special about it, and you could also create a graph which accepts column-wise batches if you would really need to. 但同样,它没有什么特别的,如果您确实需要,还可以创建一个接受按列批处理的图形。

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

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