ValueError:在使用tensorflow时使用序列设置数组元素

时间:2016-11-03 06:53:12

标签: python numpy tensorflow

我检查了所有类似的主题,但无法解决我的问题。

实际上我的代码在我的本地系统上工作正常,但是当我在服务器上执行它时,它会出现此错误。代码段:

    with tf.variable_scope("lstm") as scope:
        # The RNN cell
        single_cell = rnn_cell.DropoutWrapper(
            rnn_cell.LSTMCell(hidden_size, hidden_size, initializer=tf.random_uniform_initializer(-1.0, 1.0)),
            input_keep_prob=self.dropout_keep_prob_lstm_input,
            output_keep_prob=self.dropout_keep_prob_lstm_output)
        self.cell = rnn_cell.MultiRNNCell([single_cell] * num_layers)
        # Build the recurrence. We do this manually to use truncated backprop
        self.initial_state = tf.zeros([self.batch_size, self.cell.state_size])  # ERROR IS IN THIS LINE
        self.encoder_states = [self.initial_state]
        self.encoder_outputs = []

回溯:

WARNING:tensorflow:<tensorflow.python.ops.rnn_cell.LSTMCell object at 0x7f56e6c2cb10>: The input_size parameter is deprecated.
Traceback (most recent call last):
  File "train.py", line 194, in <module>
    main()
  File "train.py", line 63, in main
    model = create_model(sess, hyper_params, vocab_size)
  File "train.py", line 124, in create_model
    hyper_params["batch_size"])
  File "/home/datametica/karim/deeplearning/neural-sentiment/models/sentiment.py", line 73, in __init__
    self.initial_state = tf.zeros([self.batch_size, self.cell.state_size])
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1184, in zeros
    shape = ops.convert_to_tensor(shape, dtype=dtypes.int32, name="shape")
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 657, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 180, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 163, in constant
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 354, in make_tensor_proto
    nparray = np.array(values, dtype=np_dt)
ValueError: setting an array element with a sequence.

以下是实际代码的链接 - https://github.com/inikdom/neural-sentiment/blob/master/train.py

这个错误是由于numpy版本造成的吗?早些时候我的服务器numpy版本是1.11.2,所以我卸载并安装numpy 1.11.1

我的本​​地系统有1.11.1,工作正常,没有任何错误。

参考解决方案:tensorflow: ValueError: setting an array element with a sequence

我尝试将tf替换为np,但它提供了

WARNING:tensorflow:<tensorflow.python.ops.rnn_cell.LSTMCell object at 0x7f84f6f8e890>: The input_size parameter is deprecated.
Traceback (most recent call last):
  File "train.py", line 194, in <module>
    main()
  File "train.py", line 63, in main
    model = create_model(sess, hyper_params, vocab_size)
  File "train.py", line 124, in create_model
    hyper_params["batch_size"])
  File "/home/datametica/karim/deeplearning/neural-sentiment/models/sentiment.py", line 73, in __init__
    self.initial_state = np.zeros([self.batch_size, self.cell.state_size])
TypeError: an integer is required

2 个答案:

答案 0 :(得分:1)

我认为原因是state_is_tuple构造函数的MultiRNNCell参数。默认情况下是这样,在这种情况下self.cell.state_size是一个元组。

<强>更新

MultiRNNCell是由其他几个细胞组成的细胞。因此MultiRNNCell的状态由内部细胞的状态组成。构造函数的state_is_tuple参数控制内部单元格的状态是否连接到单个张量中。如果为真,则state_size的{​​{1}}是MultiRNNCell个内部单元格的总和(see source)。否则state_size是内部单元格大小的元组。

在后一种情况下,您将state_size作为形状传递给[self.batch_size, <tuple>](或tf.zeros)。

我不知道为什么它在您的本地系统中有效。我只能猜测在您的系统中,您使用具有其他默认行为的其他版本的tensorflow。

答案 1 :(得分:0)

我认为这是由于numpy版本。所以我尝试改变它,但没有帮助。还尝试通过更改代码,没有运气。

我发现,此代码适用于tensorflow 0.8.0。

如果您安装了最新的tensorflow并尝试此代码,则会出现此错误。

所以我卸载了最新版本并安装了0.8.0,现在再次正常工作。