【问题标题】:How can i input boolean tensors to tf.cond() not just one boolean?我如何将布尔张量输入到 tf.cond() 而不仅仅是一个布尔值?
【发布时间】:2017-08-29 09:48:10
【问题描述】:

这是我想用 tensorflow 实现 f(x)

输入 x = (x1,x2,x3,x4,x5,x6,x7,x8,x9)

定义 f(x) = f1(x1,x2,x3,x4,x5) + f2(x5,x6,x7,x8,x9)

在哪里

f1(x1,x2,x3,x4,x5) = {1 如果 (x1,x2,x3,x4,x5)=(0,0,0,0,0),

                   g1(x1,x2,x3,x4,x5) otherwise}

f2(x5,x6,x7,x8,x9) = {1 如果 (x5,x6,x7,x8,x9)=(0,0,0,0,0),

                   g2(x5,x6,x7,x8,x9) otherwise}

这是我的张量流代码

import tensorflow as tf
import numpy as np
ph = tf.placeholder(dtype=tf.float32, shape=[None, 9])
x1 = tf.slice(ph, [0, 0], [-1, 5])
x2 = tf.slice(ph, [0, 4], [-1, 5])
fixed1 = tf.placeholder(dtype=tf.float32, shape=[1, 5])
fixed2 = tf.placeholder(dtype=tf.float32, shape=[1, 5])

# MLP 1
w1 = tf.Variable(tf.ones([5, 1]))
g1 = tf.matmul(x1, w1)

# MLP 2
w2 = tf.Variable(-tf.ones([5, 1]))
g2 = tf.matmul(x2, w2)

check1 = tf.reduce_all(tf.equal(x1, fixed1), axis=1, keep_dims=True)

check2 = tf.reduce_all(tf.equal(x2, fixed2), axis=1, keep_dims=True)

#### with Problem
f1 = tf.cond(check1,
                   lambda: tf.constant([2], dtype=tf.float32), lambda: g1)
f2 = tf.cond(check2,
                   lambda: tf.constant([1], dtype=tf.float32), lambda: g2)
####
f = tf.add(f1, f2)

x = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0, 0, 1],
              [1, 0, 0, 0, 0, 0, 0, 0, 0],
              [2, 0, 0, 0, 0, 0, 0, 0, 0],
              [9, 0, 0, 0, 0, 0, 0, 0, 0]])

fixed = np.array([[0, 0, 0, 0, 0]])

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print('(1)\n', sess.run(check1, feed_dict={ph: x, fixed1: fixed, fixed2: fixed}))
    print('(2)\n', sess.run(check2, feed_dict={ph: x, fixed1: fixed, fixed2: fixed}))
    print('(3)\n', sess.run(f, feed_dict={ph: x, fixed1: fixed, fixed2: fixed}))
    print('(4)\n', sess.run(f1, feed_dict={ph: x, fixed1: fixed, fixed2: fixed}))
    print('(5)\n', sess.run(f2, feed_dict={ph: x, fixed1: fixed, fixed2: fixed}))

在这种情况下,

check1 是 [[ True], [ True], [False], [False], [False]] 形状为 (5, 1)

check2 是 [[ True], [False], [ True], [True], [True]] 形状为 (5, 1)

我希望 f 的结果是 [[3], [1], [2], [3], [10]]

但似乎 tf.cond() 无法将输入处理为形状为 (5, 1) 的布尔张量

请您建议如何使用 tensorflow 实现 f(x)。

这是我收到的错误消息

Traceback(最近一次调用最后一次):文件 "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", 第 670 行,在 _call_cpp_shape_fn_impl 状态)文件“C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\contextlib.py”, 第 66 行,在 退出 next(self.gen) 文件 "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", 第 469 行,在 raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(status)) tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape 必须是 0 级,但对于 'cond/Switch'(操作:'Switch')是 2 级 输入形状:[?,1], [?,1]。

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次):文件 "C:/Users/hong/Dropbox/MLILAB/Research/GM-MLP/code/tensorflow_cond.py", 第 23 行,在 lambda: tf.constant([2], dtype=tf.float32), lambda: g1) 文件 "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\ops\ control_flow_ops.py", 第 1765 行,在条件下 p_2, p_1 = switch(pred, pred) 文件 "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", 第 318 行,在开关中 return gen_control_flow_ops._switch(data, pred, name=name) File "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_control_flow_ops.py", 第 368 行,在 _switch result = _op_def_lib.apply_op("Switch", data=data, pred=pred, name=name) 文件 "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", 第 759 行,在 apply_op 中 op_def=op_def) 文件 "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", 第 2242 行,在 create_op 中 set_shapes_for_outputs(ret) 文件 "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", 第 1617 行,在 set_shapes_for_outputs 形状 = shape_func(op) 文件“C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py”, 第 1568 行,在 call_with_requiring 中 返回 call_cpp_shape_fn(op, require_shape_fn=True) 文件 "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", 第 610 行,在 call_cpp_shape_fn 中 debug_python_shape_fn, require_shape_fn) 文件 "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", 第 675 行,在 _call_cpp_shape_fn_impl raise ValueError(err.message) ValueError: Shape must be rank 0 but is rank 2 for 'cond/Switch' (op: 'Switch') with input shapes: [?,1], [?,1].

进程以退出代码 1 结束

【问题讨论】:

  • 希望直接解决这个问题。很多解决方法都作为答案提供,但 tf.cond() 应该接受张量,如果它是有用的。

标签: machine-learning tensorflow neural-network data-science


【解决方案1】:

我认为您需要 tf.where,而不是 tf.cond。

查看这个问题的答案:How to use tf.cond for batch processing

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2020-10-06
    相关资源
    最近更新 更多