Как использовать собирание в тензорном потоке

1

У меня есть массив 'status' размера (B, N) с true или false, указывающим состояние каждой точки в каждом пакете, и у меня есть другой массив 'position' размера (B, N, 2), где position (b, n) ) указывает положение точки в сетке.

status = tf.constant(np.random.randint(2, size=(B, N)))
# Each point has a status where status[b, n] indicates 
# status of the point
status = tf.cast(status, tf.bool)
# position(b, n) = (x, y) where (x, y) is coordinate of the point in the grid. 
# Each point falls into a cell. 
position = tf.constant(np.random.randint(10, size=(B, N, 2)))

Как подсчитать количество ложных и истинных в каждой ячейке двумерной сетки в тензорном потоке? Я ожидаю выходных данных размера (b, X, Y), где X и Y - размеры сетки.

  • 0
    Я не могу понять ваш вопрос. Можете ли вы привести пример ввода и ожидаемого результата?
Теги:
tensorflow
numpy

1 ответ

0

Я буду использовать размер партии в качестве первого измерения тензора, так как это обычный способ в тензорном потоке. Мы берем логические значения и tf.float32 их к tf.float32. Затем мы умножаем на тензор position с широковещательной передачей по пакетному измерению и получаем только истинные значения (ложные значения умножались на нули). Мы используем reduce_sum для вычисления суммы истинных значений для элемента в пакете. Затем мы применяем logical_not к статусу и повторяем то же самое для ложных значений. Следующий пример является продолжением того, что вы предоставили в качестве примера:

import tensorflow as tf
import numpy as np

B = 2
N = 3
status = tf.constant(np.float32(np.random.randint(2, size=(B, N))))
# Each point has a status where status[b, n] indicates 
# status of the point
status = tf.cast(status, tf.bool)
# position(b, n) = (x, y) where (x, y) is coordinate of the point in the grid. 
# Each point falls into a cell. 
position = tf.constant(np.float32(np.random.randint(10, size=(2, B, N))))

status = tf.expand_dims(status, axis=0)
status = tf.cast(status, tf.float32)

true_result = status * position
n_true = tf.reduce_sum(true_result, axis=-1)
n_true = tf.reduce_sum(n_true, axis=-1)

notstatus = tf.logical_not(tf.cast(status, tf.bool))
notstatus = tf.cast(notstatus, tf.float32)

false_result = notstatus * position
n_false = tf.reduce_sum(false_result, axis=-1)
n_false = tf.reduce_sum(n_false, axis=-1)

with tf.Session() as sess:
    print('status')
    print(status.eval())
    print('not status')
    print(notstatus.eval())
    print('position')
    print(position.eval())
    print('true result')
    print(true_result.eval())
    print('Sum of false values', n_false.eval())
    print('Sum of true values', n_true.eval())

# status
# [[[0. 0. 1.]
#   [1. 0. 0.]]]
# not status
# [[[1. 1. 0.]
#   [0. 1. 1.]]]
# position
# [[[5. 4. 0.]
#   [6. 0. 4.]]
# 
#  [[0. 2. 6.]
#   [6. 3. 6.]]]
# true result
# [[[0. 0. 0.]
#   [6. 0. 0.]]
# 
#  [[0. 0. 6.]
#   [6. 0. 0.]]]
# Sum of false values [13. 11.]
# Sum of true values [ 6. 12.]

Ещё вопросы

Сообщество Overcoder
Наверх
Меню