我正在尝试定义 LSTM 并收到错误“TypeError: add() missing 1 required positional argument: 'layer'”

Posted

技术标签:

【中文标题】我正在尝试定义 LSTM 并收到错误“TypeError: add() missing 1 required positional argument: \'layer\'”【英文标题】:I am trying to define LSTM and getting the error "TypeError: add() missing 1 required positional argument: 'layer'"我正在尝试定义 LSTM 并收到错误“TypeError: add() missing 1 required positional argument: 'layer'” 【发布时间】:2021-01-19 23:43:40 【问题描述】:
```

x_train.shape (1271, 322) x_scaler = MinMaxScaler() x_train = x_scaler.fit_transform(x_train) y_train.shape (1271, 161) y_scaler = MinMaxScaler() y_train = y_scaler.fit_transform(y_train) x_train = x_train.reshape(1271, 322, 1) reg = 顺序 reg.add(LSTM(units = 200, activation = 'relu', input_shape = (322, 1))) reg.add(密集(161))


TypeError                                 Traceback (most recent call last)
<ipython-input-43-ab4dcb49e16c> in <module>()
      1 reg = Sequential
----> 2 reg.add(LSTM(units = 200, activation = 'relu', input_shape = (322, 1)))
      3 reg.add(Dense(161))

/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    455     self._self_setattr_tracking = False  # pylint: disable=protected-access
    456     try:
--> 457       result = method(self, *args, **kwargs)
    458     finally:
    459       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

TypeError: add() missing 1 required positional argument: 'layer'

【问题讨论】:

【参考方案1】:

这个模型定义对我来说很好用:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import Sequential
from tensorflow.keras.layers import LSTM,Dense


reg = Sequential()
reg.add(LSTM(units = 200, activation = 'relu', input_shape = (322, 1))) 
reg.add(Dense(161))

还要检查你是否写过 reg = Sequential ,我认为应该是 reg = Sequential()

【讨论】:

尝试在 Sequential 后面加上括号“()”,应该可以解决问题。

以上是关于我正在尝试定义 LSTM 并收到错误“TypeError: add() missing 1 required positional argument: 'layer'”的主要内容,如果未能解决你的问题,请参考以下文章

如何在张量流中创建独立的 LSTM 单元?

Keras LSTM 层输入形状

通过自定义 LSTM 时的形状错误

用于太空入侵者 RL (Keras) 的 LSTM 网络

为啥我会收到 Keras LSTM RNN input_shape 错误?

实际上我正在尝试从 Nestjs 测试一个函数并收到该错误: