Tensorflow占位符具有负维度

2024-06-25 06:21:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究一个预测二进制选择的神经网络。当我试图提取一个预测时,它不起作用,并给出了以下线索:

2017-09-03 13:52:59.302796: W tensorflow/core/framework/op_kernel.cc:1148] Invalid argument: Shape [-1,2] has negative dimensions
2017-09-03 13:52:59.302843: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1,2] has negative dimensions
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
2017-09-03 13:52:59.302922: W tensorflow/core/framework/op_kernel.cc:1148] Invalid argument: Shape [-1,2] has negative dimensions
2017-09-03 13:52:59.302939: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1,2] has negative dimensions
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Traceback (most recent call last):
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1139, in _do_call
    return fn(*args)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1121, in _run_fn
    status, run_metadata)
  File "/home/tucker/anaconda3/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape [-1,2] has negative dimensions
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "train.py", line 104, in <module>
    print(sess.run(y, feed_dict={x: future_x}))
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 997, in _run
    feed_dict_string, options, run_metadata)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
    target_list, options, run_metadata)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape [-1,2] has negative dimensions
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op 'Placeholder_1', defined at:
  File "train.py", line 37, in <module>
    y = tf.placeholder("float32", [None, num_classes])
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1530, in placeholder
    return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1954, in _placeholder
    name=name)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Shape [-1,2] has negative dimensions
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

它看起来像是我定义“y”占位符时产生的,但我无法解决问题。只有当我试图得到一个预言的时候才会发生

这是我的代码(看起来很奇怪,因为我是从jupyter笔记本上取的):

^{pr2}$

这是当“train”时执行的代码_utils.create_网络(x=x,weights=weights,biases=bias,neurons=neurons,layers=num_layers,num峎chunks=num_chunks,look峎back=look_back)“行运行:

def create_network(x, weights, biases, neurons, layers, num_chunks, look_back):
    # x: tf var
    # weights: weights defined in file
    # biases: biases defined in file
    # num_chunks: num_chunks defined in file
    # look_back: look_back defined in file

    #return: idk just take it
    x = tf.transpose(x, [1,0,2])
    x = tf.reshape(x, [-1, look_back])
    x = tf.split(x, num_chunks, 0)

    cell = rnn.LSTMCell(neurons, state_is_tuple=True)

    def lstm_cell():
        return rnn.LSTMCell(neurons, state_is_tuple=True)
    stacked_lstm = rnn.MultiRNNCell([lstm_cell() for _ in range(layers)])

    outputs, states = rnn.static_rnn(cell, x, dtype=tf.float32)

    output = tf.matmul(outputs[-1], weights['layer']) + biases['layer']

    return output

提前谢谢


Tags: runinpyhomelibpackagestensorflowline
2条回答

预测是由张量prediction(或{},因为它是相同的),而不是占位符{}(这是您在培训期间放置标签的地方)给出的。预测代码应该是这样的:

print(sess.run(prediction, feed_dict={x: future_x}))

错误是由于这一行print(sess.run(y, feed_dict={x: future_x})),您试图通过输入另一个占位符x来获得{}。这里x和{}是独立的。在

应该按如下方式更正;您需要为y提供相应的数组

print(sess.run(y, feed_dict={y: y_test}))

相关问题 更多 >