【问题标题】:ValueError: setting an array element with a sequence while using it with tensorflowValueError:在将数组元素与 tensorflow 一起使用时设置数组元素
【发布时间】:2016-11-03 06:53:12
【问题描述】:

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

实际上我的代码在我的本地系统上运行良好,但是当我在服务器上执行它时,它给出了这个错误。代码sn-p:

    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

我尝试用np 替换tf,但它给了

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

【问题讨论】:

    标签: python numpy tensorflow


    【解决方案1】:

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

    更新

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

    在后一种情况下,您将[self.batch_size, &lt;tuple&gt;] 作为形状传递给tf.zeros(或np.zeros)。

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

    【讨论】:

    • 谢谢,但我没听懂。你能详细说明一下吗
    【解决方案2】:

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

    我发现,这段代码在 tensorflow 0.8.0 上运行良好。

    如果你安装最新的 tensorflow 并尝试这个代码,它会给出这个错误。

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

    【讨论】:

      猜你喜欢
      • 2018-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多