Tensorflow通过二进制矩阵计算乘法

时间:2022-08-06 13:54:52

I have my data tensor which is of the shape [batch_size,512] and I have a constant matrix with values only of 0 and 1 which has the shape [256,512].

我有我的数据张量,其形状为[batch_size,512],我有一个常数矩阵,其值仅为0和1,其形状为[256,512]。

I would like to compute efficiently for each batch the sum of the products of my vector (second dimension of the data tensor) only for the entries which are 1 and not 0.

我想为每个批次有效地计算我的向量的乘积之和(数据张量的第二维)仅对于1而不是0的条目。

An explaining example: let us say I have 1-sized batch: the data tensor has the values [5,4,3,7,8,2] and my constant matrix has the values:

一个解释的例子:让我们说我有1个大小的批处理:数据张量的值为[5,4,3,7,8,2],我的常量矩阵具有以下值:

[0,1,1,0,0,0]
[1,0,0,0,0,0]
[1,1,1,0,0,1]

it means that I would like to compute for the first row 4*3, for the second 5 and for the third 5*4*3*2. and in total for this batch, I get 4*3+5+5*4*3*2 which equals to 137. Currently, I do it by iterating over all the rows, compute elementwise the product of my data and constant-matrix-row and then sum, which runs pretty slow.

这意味着我想计算第一行4 * 3,第二行5和第三行5 * 4 * 3 * 2。并且对于这个批次,我得到4 * 3 + 5 + 5 * 4 * 3 * 2,等于137.目前,我通过迭代所有行来做,按元素计算我的数据和常量矩阵的乘积-row然后求和,运行速度很慢。

1 个解决方案

#1


0  

How about something like this:

这样的事情怎么样:

import tensorflow as tf

# Two-element batch
data = [[5, 4, 3, 7, 8, 2],
        [4, 2, 6, 1, 6, 8]]
mask = [[0, 1, 1, 0, 0, 0],
        [1, 0, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 1]]
with tf.Graph().as_default(), tf.Session() as sess:
    # Data as tensors
    d = tf.constant(data, dtype=tf.int32)
    m = tf.constant(mask, dtype=tf.int32)
    # Tile data as needed
    dd = tf.tile(d[:, tf.newaxis], (1, tf.shape(m)[0], 1))
    mm = tf.tile(m[tf.newaxis, :], (tf.shape(d)[0], 1, 1))
    # Replace values with 1 wherever the mask is 0
    w = tf.where(tf.cast(mm, tf.bool), dd, tf.ones_like(dd))
    # Multiply row-wise and sum
    result = tf.reduce_sum(tf.reduce_prod(w, axis=-1), axis=-1)
    print(sess.run(result))

Output:

[137 400]

EDIT:

If you input data is a single vector then you would just have:

如果您输入的数据是单个向量,那么您只需:

import tensorflow as tf

# Two-element batch
data = [5, 4, 3, 7, 8, 2]
mask = [[0, 1, 1, 0, 0, 0],
        [1, 0, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 1]]
with tf.Graph().as_default(), tf.Session() as sess:
    # Data as tensors
    d = tf.constant(data, dtype=tf.int32)
    m = tf.constant(mask, dtype=tf.int32)
    # Tile data as needed
    dd = tf.tile(d[tf.newaxis], (tf.shape(m)[0], 1))
    # Replace values with 1 wherever the mask is 0
    w = tf.where(tf.cast(m, tf.bool), dd, tf.ones_like(dd))
    # Multiply row-wise and sum
    result = tf.reduce_sum(tf.reduce_prod(w, axis=-1), axis=-1)
    print(sess.run(result))

Output:

137

#1


0  

How about something like this:

这样的事情怎么样:

import tensorflow as tf

# Two-element batch
data = [[5, 4, 3, 7, 8, 2],
        [4, 2, 6, 1, 6, 8]]
mask = [[0, 1, 1, 0, 0, 0],
        [1, 0, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 1]]
with tf.Graph().as_default(), tf.Session() as sess:
    # Data as tensors
    d = tf.constant(data, dtype=tf.int32)
    m = tf.constant(mask, dtype=tf.int32)
    # Tile data as needed
    dd = tf.tile(d[:, tf.newaxis], (1, tf.shape(m)[0], 1))
    mm = tf.tile(m[tf.newaxis, :], (tf.shape(d)[0], 1, 1))
    # Replace values with 1 wherever the mask is 0
    w = tf.where(tf.cast(mm, tf.bool), dd, tf.ones_like(dd))
    # Multiply row-wise and sum
    result = tf.reduce_sum(tf.reduce_prod(w, axis=-1), axis=-1)
    print(sess.run(result))

Output:

[137 400]

EDIT:

If you input data is a single vector then you would just have:

如果您输入的数据是单个向量,那么您只需:

import tensorflow as tf

# Two-element batch
data = [5, 4, 3, 7, 8, 2]
mask = [[0, 1, 1, 0, 0, 0],
        [1, 0, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 1]]
with tf.Graph().as_default(), tf.Session() as sess:
    # Data as tensors
    d = tf.constant(data, dtype=tf.int32)
    m = tf.constant(mask, dtype=tf.int32)
    # Tile data as needed
    dd = tf.tile(d[tf.newaxis], (tf.shape(m)[0], 1))
    # Replace values with 1 wherever the mask is 0
    w = tf.where(tf.cast(m, tf.bool), dd, tf.ones_like(dd))
    # Multiply row-wise and sum
    result = tf.reduce_sum(tf.reduce_prod(w, axis=-1), axis=-1)
    print(sess.run(result))

Output:

137