我有一个这样的二维张量:
$s = '"foo bar"ANDbar"foo"AND"foofoo" lorem "impsum"';
if (preg_match_all('~(?|"([^\\\\"]*(?:\\\\.[^"\\\\]*)*)"|([^\s"]+))~s', $s, $matches))
{
print_r($matches[1]);
}
// => Array ( [0] => foo bar [1] => ANDbar [2] => foo [3] => AND [4] => foofoo [5] => lorem [6] => impsum )
我想计算每一行中每个非零元素的平均值,所以结果将是:
[[1. 0. 0. 2. 1. 0. 1.]
[0. 0. 0. 1. 0. 0. 0.]
[2. 0. 2. 1. 1. 3. 0.]]
如何使用TensorFlow做到这一点?
答案 0 :(得分:2)
计算每行掩蔽均值的一种方法是使用tf.math.unsorted_segment_mean
。本质上,每行可以有一个段ID,然后用额外的一个替换掩码元素的段ID。
[1.25 1. 1.8 ]
但是,由于在这种情况下掩码恰好适用于非零元素,因此您也可以只使用tf.math.count_nonzero
:
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
x = tf.constant([[1., 0., 0., 2., 1., 0., 1.],
[0., 0., 0., 1., 0., 0., 0.],
[2., 0., 2., 1., 1., 3., 0.]], tf.float32)
s = tf.shape(x)
num_rows = s[0]
num_cols = s[1]
# Mask for selected elements
mask = tf.not_equal(x, 0)
# Make per-row ids
row_id = tf.tile(tf.expand_dims(tf.range(num_rows), 1), [1, num_cols])
# Id is replaced for masked elements
seg_id = tf.where(mask, row_id, num_rows * tf.ones_like(row_id))
# Take segmented mean discarding last value (mean of masked elements)
out = tf.math.unsorted_segment_mean(tf.reshape(x, [-1]), tf.reshape(seg_id, [-1]),
num_rows + 1)[:-1]
print(sess.run(out))
# [1.25 1. 1.8 ]
答案 1 :(得分:1)
我们可以使用tf.map_fn
来实现:
x = tf.constant([[1., 0., 0., 2., 1., 0., 1.],
[0., 0., 0., 1., 0., 0., 0.],
[2., 0., 2., 1., 1., 3., 0.]], tf.float32)
def mean(row):
mask = tf.not_equal(row, 0.0)
filtered = tf.boolean_mask(row, mask)
return tf.reduce_mean(filtered)
out = tf.map_fn(mean, x)