如何修复“ TypeError:'函数'对象不可下标”?

时间:2019-08-13 23:29:21

标签: python tensorflow

滚动自定义估计器以探索时间序列中的RNN预测,我无法将模型函数的'features'参数转换为序列,以便提供 到:

tf.nn.static_rnn(
    cell,
    inputs,
    initial_state=None,
    dtype=None,
    sequence_length=None,
    scope=None
)
 (2nd parameter).

为了将“功能”(从我的输入函数返回的字典)转换为时间顺序,我需要执行以下操作:

x = tf.split(features['f1'], 8, 1)

这是我的缩写fn模型:

    def rnn_model_fn(features, labels, mode, params):  
        # 0. Reformat input shape to become a sequence; i.e. break it into the series time steps
        x   = tf.split(features['f1'], 8, 1)

        # 1. configure the RNN
        lstm_cell  = tf.keras.layers.LSTMCell(LSTM_SIZE)
        outputs, _ = tf.contrib.rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
...

这是堆栈跟踪:

Traceback (most recent call last):
  File "E:\19\IronCondorTrade\ICondorIV.py", line 124, in <module>
    tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\training.py", line 473, in train_and_evaluate
    return executor.run()
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\training.py", line 613, in run
    return self.run_local()
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\training.py", line 714, in run_local
    saving_listeners=saving_listeners)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 367, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1158, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1188, in _train_model_default
    features, labels, ModeKeys.TRAIN, self.config)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1146, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "E:\19\IronCondorTrade\ICondorIV.py", line 18, in rnn_model_fn
    x   = tf.split(features['f1'], 8, 1)
TypeError: 'function' object is not subscriptable

当我运行代码时(从IDLE内部)发生错误。我希望它可以继续构建RNN网络,但会卡在-TypeError:'function'对象不可下标-。尝试了各种获取features参数的方法,但是它们都返回相同的消息类型,即我认为的是字典,而是函数。神秘到足以成为我的第一篇SO帖子。任何指导深表感谢。

0 个答案:

没有答案