【问题标题】:Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array无法将符号张量 (lstm/strided_slice:0) 转换为 numpy 数组
【发布时间】:2021-09-23 10:25:03
【问题描述】:

我希望有人回答以下错误代码,我正在研究有监督的 ML,但遇到了错误。

我的库详细信息(由于我将上述软件包都降级为上面提到的当前软件包详细信息,许多开发人员说要降级它可以工作的版本,但在我的情况下它不起作用):

  • Numpy : 1.18.5 当前版本(之前是 1.20.3)
  • TensorFlow:2.5.0 当前版本(之前是 2.4.1)
  • Python:3.8.8
  • Keras:2.4.3

代码如下:

# Defining the LSTM model to be fit
model = Sequential()
model.add(LSTM(85, input_shape=(1, 53)))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
# Fitting the model
history = model.fit(train_X, train_y, epochs=70, batch_size=175, validation_data=(test_X, test_y), verbose=2, shuffle=False)
# Plotting the training progression
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.show()

还有错误:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-10-251aaaf9021e> in <module>
      1 # Defining the LSTM model to be fit
      2 model = Sequential()
----> 3 model.add(LSTM(85, input_shape=(train_X.shape[1], train_X.shape[2])))
      4 model.add(Dense(1))
      5 model.compile(loss='mae', optimizer='adam')

 NotImplementedError: Cannot convert a symbolic Tensor (lstm_2/strided_slice:0) to a numpy array. 
   This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

【问题讨论】:

  • 您的代码与您附加的错误不同。代码有model.add(LSTM(85, input_shape=(1, 53))) 行,但错误提示这是`model.add(LSTM(85, input_shape=(train_X.shape[1], train_X.shape[2])))`。
  • 你知道怎么解决
  • @Kaveh input_shape=(train_X.shape[1], train_X.shape[2]) ,其中 train_X.shape[1] =1 和 train_X.shape[2]=53
  • 请阅读ml标签的描述。
  • @molbdnilo 我的问题与 LSTM 有关

标签: python-3.x numpy tensorflow machine-learning lstm


【解决方案1】:

在添加依赖于层input_shape的LSTM模型时得到了解决方案,因此将形状更改为

model = Sequential()
model.add(LSTM(85, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')

# Fitting the model
history = model.fit(train_X, train_y, epochs=70, batch_size=175, validation_data=(test_X, test_y), verbose=2, shuffle=False)
# Plotting the training progression
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.show()

【讨论】:

  • 你能解释一下答案吗?
猜你喜欢
  • 2021-05-15
  • 2022-01-12
  • 1970-01-01
  • 2021-10-17
  • 2022-10-05
  • 1970-01-01
  • 2021-12-15
  • 2020-10-31
  • 2020-02-17
相关资源
最近更新 更多