使用TensorFlow与dynamic_rnn和LSTMCell中的输入字符串时出错

时间:2017-07-18 10:55:51

标签: python string tensorflow lstm rnn

我想使用TensorFlow来解决受监督的问题。这很简单:

  • 字符串输入列表。该字符串的格式为:" words_10-06-2017"
  • 浮点数输出列表。标签是浮动:3.0

我想使用具有该格式的字符串集和一组浮动标签来训练模型。

系统信息

  • Windows 7 x64
  • Tensorflow 1.2
  • Python 3.5.2
  • IDE Pycharm 2017.1.1

我正在使用这段代码:

# Input placeholder
x = tf.placeholder(tf.string, shape=[None, 1, 1])

...

# Define a lstm cell with tensorflow
lstm_cell = tf.nn.rnn_cell.LSTMCell(self.first_label_neurons, forget_bias=1)
# Get lstm cell output
outputs, states = tf.nn.dynamic_rnn(lstm_cell, x, dtype=tf.string)
dropout = tf.nn.dropout(outputs[-1], keep_probably)
# Linear activation, using rnn inner loop last output
y_prediction = tf.matmul(dropout, weights) + biases

问题出在这一行:

outputs, states = tf.nn.dynamic_rnn(lstm_cell, x, dtype=tf.string)

这是输出:

ValueError:需要变量rnn / lstm_cell / kernel的初始化程序

这可能是TensorFlow问题吗?

感谢您的关注

全部输出:

File "D:\ComputerScience\workspaces\model.py", line 602, in rnn_lstm
    outputs, states = tf.nn.dynamic_rnn(lstm_cell, x, dtype=tf.string)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\rnn.py", line 574, in dynamic_rnn
    dtype=dtype)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\rnn.py", line 737, in _dynamic_rnn_loop
    swap_memory=swap_memory)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2770, in while_loop
    result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2599, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2549, in _BuildLoop
    body_result = body(*packed_vars_for_body)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\rnn.py", line 722, in _time_step
    (output, new_state) = call_cell()
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\rnn.py", line 708, in <lambda>
    call_cell = lambda: cell(input_t, state)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 180, in __call__
    return super(RNNCell, self).__call__(inputs, state)
  File "C:\Python352\lib\site-packages\tensorflow\python\layers\base.py", line 441, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 542, in call
    lstm_matrix = _linear([inputs, m_prev], 4 * self._num_units, bias=True)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 1017, in _linear
    initializer=kernel_initializer)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1065, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 962, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 360, in get_variable
    validate_shape=validate_shape, use_resource=use_resource)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 183, in _rnn_get_variable
    variable = getter(*args, **kwargs)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 352, in _true_getter
    use_resource=use_resource)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 690, in _get_single_variable
    name=name, shape=shape, dtype=dtype)
  File "C:\Python352\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 770, in _get_default_initializer
    % (name, dtype.base_dtype))
ValueError: An initializer for variable rnn/lstm_cell/kernel of <dtype: 'string'> is required

1 个答案:

答案 0 :(得分:1)

尝试为输入的矢量嵌入添加一个单词。基本上,张量流只能处理现在的int和float输入。因此,请对输入的单词序列进行矢量表示