【问题标题】:How to multiply each row of the softmax by a specific matrix in tensorflow?如何将softmax的每一行乘以张量流中的特定矩阵?
【发布时间】:2017-10-23 14:48:06
【问题描述】:

我要解决的问题如下: 给定我的softmax的输出y_pred,维度为[batch_size, n_classes],以及一个包含每个预测的矩阵的张量T,这意味着张量的维度为[batch_size, n_classes, n_classes]

我希望能够将y_pred 的每一行乘以来自T 的矩阵之一,并在每个迭代训练步骤中将结果保存在y_pred 中。

例子:

y_pred[0,:] = y_pred[0,:]*T[0,;,:]
y_pred[1,:] = y_pred[1,:]*T[1,;,:]

【问题讨论】:

    标签: python tensorflow deep-learning


    【解决方案1】:

    此代码示例应该会给您想要的结果。

    import tensorflow as tf
    batch_size=3
    num_classes=7
    y_pred = tf.random_normal((batch_size, num_classes))
    T = tf.random_normal((batch_size, num_classes, num_classes))
    
    output = []
    for i in range(batch_size):
      output.append(tf.matmul(tf.expand_dims(y_pred[i], axis=0), T[i]))
    
    y_pred = tf.concat(output, axis=0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 2023-03-20
      • 2018-05-24
      • 1970-01-01
      • 2018-11-01
      相关资源
      最近更新 更多