[Python] How is the input_matrix * weight_matrix + bias_vector possible in tensorflow?

Discussão em 'Python' iniciado por Stack, Agosto 20, 2025.

  1. Stack

    Stack Membro Participativo

    Given an N x M input matrix X, an M x U weight matrix W, and a bias vector b of length U, how is the affine transformation XW + b possible? If the result R of the matrix multiplication is of shape N x U, how is the bias vector b added to to R? I have tried googling matrix-vector addition but have only found results for matrix-matrix or vector-vector addition. Based on the Matrix Addition Wiki Article, this transformation shouldn't be possible, but clearly I misunderstand something.

    Example code below:

    import tensorflow as tf

    # Define tensors
    N = 10
    M = 4
    input_matrix = tf.random.normal(shape=(N, M))

    U = 2
    weight_matrix = tf.random.normal(shape=(M, U))
    bias_vector = tf.random.normal(shape=(U,))

    # Affine transformation
    R = tf.matmul(input_matrix, weight_matrix) + bias_vector
    print(input_matrix.shape, '*', weight_matrix.shape, '+', bias_vector.shape, '=', R.shape)
    >>> (10, 4) * (4, 2) + (2,) = (10, 2)

    Continue reading...

Compartilhe esta Página