从 v1.1 开始,Tensorflow 涵盖了这种类似 Numpy 的索引,请参阅 Tensor.getitem。
import tensorflow as tf
with tf.Session() as sess:
y_pred = tf.constant([[[1,2,3,4,5,6,7,8,9,10], [10,20,30,40,50,60,70,80,90,100]]])
y_true = tf.constant([[[1,2,3,4,5,6,7,8,9,10], [10,20,30,40,50,60,70,80,90,100]]])
print((y_pred[:,:,:5] * y_true[:,:,:5]).eval())
# [[[ 1 4 9 16 25]
# [ 100 400 900 1600 2500]]]
评论后编辑:
现在,问题是“*=”部分,即项目分配。这在 Tensorflow 中并不是一个简单的操作。但是,在您的情况下,这可以使用tf.concat 或tf.where 轻松解决(tf.dynamic_partition + tf.dynamic_stitch 可用于更复杂的情况)。
在下面找到前两个解决方案的快速实现。
使用 Tensor.getitem 和 tf.concat 的解决方案:
import tensorflow as tf
with tf.Session() as sess:
y_pred = tf.constant([[[1,2,3,4,5,6,7,8,9,10]]])
y_true = tf.constant([[[1,2,3,4,5,6,7,8,9,10]]])
# tf.where can't apply the condition to any axis (see doc).
# In your case (condition on 2nd axis), we need either to manually broadcast the
# condition tensor, or transpose the target tensors.
# Here is a quick demonstration with the 2nd solution:
y_pred_edit = y_pred[:,:,:5] * y_true[:,:,:5]
y_pred_rest = y_pred[:,:,4:]
y_pred = tf.concat((y_pred_edit, y_pred_rest), axis=2)
print(y_pred.eval())
# [[[ 1 4 9 16 25 6 7 8 9 10]]]
使用 tf.where 的解决方案:
import tensorflow as tf
def select_n_fist_indices(n, batch_size):
""" Return a list of length batch_size with the n first elements True
and the rest False, i.e. [*[[True] * n], *[[False] * (batch_size - n)]].
"""
n_ones = tf.ones((n))
rest_zeros = tf.zeros((batch_size - n))
indices = tf.cast(tf.concat((n_ones, rest_zeros), axis=0), dtype=tf.bool)
return indices
with tf.Session() as sess:
y_pred = tf.constant([[[1,2,3,4,5,6,7,8,9,10]]])
y_true = tf.constant([[[1,2,3,4,5,6,7,8,9,10]]])
# tf.where can't apply the condition to any axis (see doc).
# In your case (condition on 2nd axis), we need either to manually broadcast the
# condition tensor, or transpose the target tensors.
# Here is a quick demonstration with the 2nd solution:
y_pred_tranposed = tf.transpose(y_pred, [2, 0, 1])
y_true_tranposed = tf.transpose(y_true, [2, 0, 1])
edit_indices = select_n_fist_indices(5, tf.shape(y_pred_tranposed)[0])
y_pred_tranposed = tf.where(condition=edit_indices,
x=y_pred_tranposed * y_true_tranposed, y=y_pred_tranposed)
# Transpose back:
y_pred = tf.transpose(y_pred_tranposed, [1, 2, 0])
print(y_pred.eval())
# [[[ 1 4 9 16 25 6 7 8 9 10]]]