Camilla
Camilla

Reputation: 29

ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes: [6], [6]

the error:

ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes: [6], [6].

import tensorflow as tf

with tf.device('/gpu:1'):
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name='b')
    c = tf.matmul(a, b)

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(c))

I don't know what's wrong. Thank you so much for your help.

Upvotes: 1

Views: 1773

Answers (3)

efdummy
efdummy

Reputation: 704

You can use tf.expand_dims(a,0) and tf.expand_dims(b,1) to have rank 2 shapes. Try the following code:

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name='b')
c = tf.matmul(tf.expand_dims(a,0), tf.expand_dims(b, 1))
c2=tf.squeeze(c)
sess=tf.Session()
print(sess.run(c))
print(sess.run(c2))enter code here

It will display:

[[ 91.]]
91.0

Upvotes: 0

josepdecid
josepdecid

Reputation: 1847

Otherwise if you want to do a Matrix multiplication, and not elementwise, as suggested in other answers, you need the vectors to be 2D to multiple a row-vector by a column-vector:

import tensorflow as tf
a = tf.constant([[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]], name="a") # Shape [6, 1]
b = tf.constant([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]], name="b") # Shape [1, 6]
c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) print(sess.run(c))

Upvotes: 0

nessuno
nessuno

Reputation: 27052

tf.matmul multiplies matrix, tensors with 2 dimensions. You're trying to multiply, using matmul, two vectors that are tensors with 1 dimension.

Your expected outcome is [ 1. 4. 9. 16. 25. 36.] that's the elementwise multiplication of the vector elements. To obtain it, you have to use the tf.multiply op.

import tensorflow as tf

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name="a")
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name="b")
c = tf.multiply(a, b)

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(c))

Upvotes: 1

Related Questions