TensorFlow Multi-Layer Perceptron
<p>I am learning TensorFlow, and my goal is to implement MultiPerceptron for my needs. I checked the <a href="https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/multilayer_perceptron.py" rel="nofollow">MNIST tutorial with MultiPerceptron implementation</a>  and everything was clear to me except this:</p>

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

<p>I guess, <code>x</code> is an image itself(28*28 pixels, so the input is 784 neurons) and <code>y</code> is a label which is an 1x10 array:</p>

<pre><code>x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])
</code></pre>

<p>They feed whole batches (which are packs of data points and labels)! How does tensorflow interpret this "batch" input? And how does it update the weights: simultaneously after each element in a batch, or after running through the whole batch?</p>

<p>And, if I need to input one number (<code>input_shape = [1,1]</code>) and output four numbers (<code>output_shape = [1,4]</code>), how should I change the <code>tf.placeholders</code> and in which form should I feed them into session?</p>

<ol>
<li><p>When I ask, how does tensorflow interpret it, I want to know how tensorflow splits the batch into single elements. 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?</p></li>
<li><p>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. </p></li>
</ol>

<p>When I feed my float numpy array <code>X_train</code> to <code>x</code>, which is :</p>

<pre><code>x = tf.placeholder("float", [1, n_input])
</code></pre>

<p>I receive an error:</p>

<p><code>ValueError: Cannot feed value of shape (1, 18) for Tensor 'Placeholder_10:0', which has shape '(1, 1)'</code></p>

<p>It appears that I have to create my data as a Tensor too?</p>

<p>When I tried [18x1]:</p>

<p><code>Cannot feed value of shape (18, 1) for Tensor 'Placeholder_12:0', which has shape '(1, 1)'</code></p>

<blockquote>
  <p>They feed whole bathces(which are packs of data points and labels)!</p>
</blockquote>

<p>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). </p>

<blockquote>
  <p>How does tensorflow interpret this "batch" input?</p>
</blockquote>

<p>It "interprets" it according to operations in your graph. You probably have <strong>reduce mean</strong> somewhere in your graph, which calculates <strong>average</strong> over your batch, thus causing this to be the "interpretation". </p>

<blockquote>
  <p>And how does it update the weights: 1.simultaniusly after each element in a batch? 2. After running threw the whole batch?.</p>
</blockquote>

<p>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 <strong>a single</strong> 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).</p>

<blockquote>
  <p>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? THANKS!!</p>
</blockquote>

<p>just set the variables, <code>n_input=1</code> and <code>n_classes=4</code>, 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).</p>

<blockquote>
  <p>EDIT: 1.when i ask, how does tensorflow interpret it, i want to know, how tensorflow split the batch into single elements. 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.</p>
</blockquote>

<p>It <strong>does not</strong> split anything. It is just a matrix, and each operation is perfectly well defined for matrices as well. <strong>Usually</strong> you put examples in rows, thus in <strong>first dimension</strong>, and this is exactly what [batch, n_inputs] says - that you have <code>batch</code> rows each with <code>n_inputs</code> columns. 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. </p>

python machine-learning tensorflow