n t, tf.unsorted_segment_sum :
count_all = tf.unsorted_segment_sum(tf.ones_like(t), t, n)
count_all .
. count_all[0] , 0 t:
t = tf.placeholder(tf.int32)
count_all = tf.unsorted_segment_sum(tf.ones_like(t), t, 3)
sess.run(count_all[0], {t: [1,2,0,0,0,0]})
sess.run(count_all, {t: [1,2,0,0,0,0]})
, , . , Eli Bixby, one_hot . - tf.map_fn :
def count_all_fnc(e):
return tf.unsorted_segment_sum(tf.ones_like(e), e, n)
count_all = tf.map_fn(count_all_fnc, t)
.
n = 3
t = tf.placeholder(tf.int32)
def count_all_fnc(e):
return tf.unsorted_segment_sum(tf.ones_like(e), e, n)
count_all = tf.map_fn(count_all_fnc, t)
sess.run(count_all, {t:[[1, 0, 0, 2], [1, 2, 0, 0], [0, 0, 0, 0], [1, 1, 1, 2]]})
array([[2, 1, 1],
[2, 1, 1],
[4, 0, 0],
[0, 3, 1]], dtype=int32)
, ( 10 ) , , , . n*|t|, .
one_hot_t = tf.one_hot(t, n)
count_all = tf.reduce_sum(one_hot_t, axis=1)