首页 文章

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

提问于
浏览
1

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

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

一个解释的例子:让我们说我有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]

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

1 回答

  • 0

    这样的事情怎么样:

    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))
    

    输出:

    [137 400]
    

    编辑:

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

    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))
    

    输出:

    137
    

相关问题