[Python] tensorflow matrix multiplication

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    So, i want to multiply a matrix with a matrix. When I try an array with a matrix, it works:

    import tensorflow as tf

    x = tf.placeholder(tf.float32, [None, 3])
    W = tf.Variable(tf.ones([3, 3]))
    y = tf.matmul(x, W)

    with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    curr_y = sess.run(y, feed_dict={x: [[1,2,3],[0,4,5]]})
    print curr_y


    So the array has the batch size 2 and shape 3x1. So I can multiply the matrix with shape 3x3 with the array 3x1. But when I have again a matrix with the shape 3x3, but this time a matrix and not an array with the shape 3x2, with batch size 2, its not working.

    But if I try to multiply a matrix with a matrix. It doesn't work.

    import tensorflow as tf
    x = tf.placeholder(tf.float32, [None, 3,3])
    W = tf.Variable(tf.ones([3, 3]))
    y = tf.matmul(x, W)

    with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    curr_y = sess.run(y, feed_dict={x: [[[1,2,3],[1,2,3]],[[1,1,4],[0,4,5]]]})
    print curr_y


    ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul') with input shapes: [?,3,3], [3,3].
    ​
    ########EDIT
    Sorry, what I want to do, is, to matmul a matrix to a batch of matrix or arrays. So I dont want to do

    y = tf.matmul(x, W)


    actually, I want to do

    y = tf.matmul(W, x)

    Continue reading...

Compartilhe esta Página