首页 文章

如何将softmax的每一行乘以张量流中的特定矩阵?

提问于
浏览
-2

我试图解决的问题如下:给定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,;,:]

1 回答

  • 0

    此代码示例应该为您提供所需的结果 .

    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)
    

相关问题