0

I am having a doubt understanding the the linear regression process in tensorflow. Below is my code snippet:

def linearmodel(X,w,b):
    output = tf.matmul(X,w)+b
    return output

x = tf.placeholder(tf.float32,[None,4])
y_ = tf.placeholder(tf.float32,[None,3])
w = tf.Variable(tf.random_normal([4,3],mean=0.0,stddev=0.05))
b= tf.Variable(tf.zeros([3]))
y_pred = linearmodel(x,w,b)

In the above code while training time i am passing 120 elements with 4 columns so in that case when i try to multiply tf.matmul(x,w) i will get a matrix of size nx3. Now i while adding it with b which is of shape 1x3 how the matrix addition is gonna happen as it is not following the matrix addition rule which states that only same size matrix can be added.

Sorry if this sounds very silly but i would be really grateful if anyone can help.

animal
  • 994
  • 3
  • 13
  • 35
  • 1
    The duplicate applied to this question: broadcasting rules are applied, so the bias tensor of shape `[3]` is expanded to a matrix of shape `[n, 3]` for the addition in the linear model. – E_net4 Sep 28 '18 at 10:59
  • @E_net4 thanks a lot for the answer. It really solved my doubt. Thanks – animal Sep 28 '18 at 11:01
  • @E_net4 i did thanks. – animal Sep 28 '18 at 11:07

0 Answers0