0

In tensorflow, I tried to add two matrices. One is 3 x 3 matrix and another is 3 x 1 matrix, so I expected that the latter matrix was broadcasted. But the result wasn't what I expected. Could you explain what kind of calculation is executed in below code?

import tensorflow as tf
import numpy as np

A = tf.random_normal([3, 3], mean=1, stddev=4, seed = 1)
B = tf.Variable([[1, 2, 3]], dtype=tf.float32)
C = A + B

init = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init)
    print("A")
    print(A.eval())
    print("\nB")
    print(B.eval())
    print("\nC")
    print(C.eval())

The result is below.

A
[[-2.24527287  6.93839502  1.26131749]
[-8.77081585  1.39699364  3.36489725]
[ 3.37129188 -7.49171829 -1.89158893]]

B
[[ 1.  2.  3.]]

C
[[ 6.96361637  4.79503536  5.32562923]
[ 2.75787783  2.64941168  2.83947849]
[ 1.70853901  9.26744461  1.85234141]]
P-Gn
  • 23,115
  • 9
  • 87
  • 104
moriteru
  • 70
  • 6

1 Answers1

1

Your problem is not a matrix broadcast issue but stems from the fact that you are using a random number generator and not executing your nodes simultaneously. See this question.

To see broadcasting at work in your example, use

sess.run([A, B, C])
P-Gn
  • 23,115
  • 9
  • 87
  • 104