python - 值错误 : object __array__ method not producing an array

标签 python tensorflow keras

尝试从以下链接运行代码: https://machinelearningmastery.com/how-to-use-the-timeseriesgenerator-for-time-series-forecasting-in-keras/ 获取错误:ValueError:对象 array 方法未生成数组

keras版本:2.3.0-tf

请帮忙。谢谢!

# univariate one step problem with mlp
from numpy import array
from keras.models import Sequential
from keras.layers import Dense
from keras.preprocessing.sequence import TimeseriesGenerator
# define dataset
series = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# define generator
n_input = 2
generator = TimeseriesGenerator(series, series, length=n_input, batch_size=8)
# define model
model = Sequential()
model.add(Dense(100, activation='relu', input_dim=n_input))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit_generator(generator, steps_per_epoch=1, epochs=200, verbose=0)
# make a one step prediction out of sample
x_input = array([9, 10]).reshape((1, n_input))
yhat = model.predict(x_input, verbose=0)
print(yhat)

错误:

ValueError                                Traceback (most recent call last)
    <ipython-input-8-550aa8802f57> in <module>()
         11 # define model
         12 model = Sequential()
    ---> 13 model.add(Dense(100, activation='relu', input_dim=n_input))
         14 model.add(Dense(1))
         15 model.compile(optimizer='adam', loss='mse')
-----------------
   ~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
         94       dtype = dtypes.as_dtype(dtype).as_datatype_enum
         95   ctx.ensure_initialized()
    ---> 96   return ops.EagerTensor(value, ctx.device_name, dtype)
         97 
         98 

    ValueError: object __array__ method not producing an array

最佳答案

我能够使用 TF 2.2.0Keras 2.3.0 在 Jupyter 和 Google Colab 中执行您的代码。

为了社区的利益,请引用下面的完整代码和输出。

import tensorflow as tf
import keras
print(keras.__version__)
print (tf.__version__)
from numpy import array
from keras.models import Sequential
from keras.layers import Dense
from keras.preprocessing.sequence import TimeseriesGenerator
# define dataset
series = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# define generator
n_input = 2
generator = TimeseriesGenerator(series, series, length=n_input, batch_size=8)
# define model
model = Sequential()
model.add(Dense(100, activation='relu', input_dim=n_input))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit_generator(generator, steps_per_epoch=1, epochs=200, verbose=0)
# make a one step prediction out of sample
x_input = array([9, 10]).reshape((1, n_input))
yhat = model.predict(x_input, verbose=0)
print(yhat)

输出:

2.3.1
2.2.0
[[11.588003]]

如果您的问题仍然存在,请告诉我,我很乐意为您提供帮助。

关于python - 值错误 : object __array__ method not producing an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62022631/

相关文章:

python - TensorFlow 运算符重载

python - 在 Flask 应用程序中使用 uwsgi 部署时,keras 预测会卡住

python - 在 Flask 中嵌入 Panel 应用程序 - 图在小部件更改时不更新

python - 一个类轮通过一个可迭代的(生成器)

tensorflow - 当不再需要时如何从内存中释放张量?

python - 如何在 tensorflow 中制作 reshape 层?

Python函数导入文件名和城市名称

python - 如何根据参数指定调用哪个__init__?

python - Keras NN 中的输入节点

python - 失败前提条件错误: Table not initialized