Может ли tf.cond () использоваться для динамического определения графа - PullRequest
0 голосов
/ 15 февраля 2019

Чтобы построить код тестового примера, я просто хочу передать тензор в граф и попросить модель решить, умножить ли на 2 один или два раза.Решение основано на оценке логического значения.Вот код:

class what_to_do():
def __init__(self):

    self.conditionally_determined_A = None
    self.conditionally_determined_B = None

    self.truth_values = tf.placeholder(shape=[None, 1],
                                       dtype=tf.bool)
    self.input = tf.placeholder(shape=[None, 1],
                                dtype=tf.float32)

    # The question is, can this statement evaluate the conditional and then direct the
    # implementation of the model's components.
    _ = tf.cond(tf.constant(True),
                lambda: self.real_conditional(),
                lambda: self.fake_condition())

    self.input_A = tf.Variable(initial_value=self.conditionally_determined_A,
                               trainable=False,
                               validate_shape=False)
    self.const_A = tf.constant([2.])
    self.operation_A1 = tf.multiply(self.input_A, self.const_A)

    self.input_B = tf.Variable(initial_value=self.conditionally_determined_B,
                               trainable=False,
                               validate_shape=False)
    self.const_B = tf.constant([2.])
    self.operation_B1 = tf.multiply(self.input_B, self.const_B)

    self.output = self.operation_B1

    # These functions will serve as the condition coordinators for the model
    def real_conditional(self):
        print('in loop')
        self.conditionally_determined_B = self.input
        self.conditionally_determined_A = None
        return 1
    def fake_condition(self):
        print('not in loop')
        self.conditionally_determined_B = None
        self.conditionally_determined_A = self.input
        return 0


tf.reset_default_graph()
model = what_to_do()
data_set = np.array([[2,True], [2,True], [2,True], [2,False], [2,False], [2,False], [2,False]])
i, t = np.split(ary=data_set,
            indices_or_sections=2,
            axis=-1)
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)

    output = sess.run(fetches=[model.output],
                  feed_dict={model.input_A:i, model.truth_values:t})

Я столкнулся с проблемой, пытаясь заставить tf.cond () обработать тензор.Жалуется на звания и лайки (возможно, другой вопрос).

Что я сделал с кодом, так это просто определил условие как True или False, таким образом выполняя соответствующую функцию.Если это правда, график должен начинаться с input_B и вообще не беспокоиться о input_A.

Любая помощь в настройке tf.cond () для динамического управления графиком?

...