【问题标题】:ValueError: object __array__ method not producing an arrayValueError: object __array__ 方法不产生数组
【发布时间】:2020-05-26 12:54:29
【问题描述】:

尝试从以下链接运行代码: 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

【问题讨论】:

  • 代码运行良好。是否尝试过更改 tensorflow 的版本?
  • 感谢@Yoskutik 的回复!我试过 tensorflow 1.8.0 没用。您使用的是哪个版本?
  • 我使用版本:2.2.0
  • 我也在用2.2.0
  • 其实我现在没有任何想法。是否尝试在 Google Colab 中运行您的代码?如果仍然调用错误,则问题出在代码中的其他地方

标签: python tensorflow keras


【解决方案1】:

我可以使用 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]]

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

【讨论】:

    猜你喜欢
    • 2021-04-17
    • 1970-01-01
    • 2017-01-30
    • 2023-04-04
    • 1970-01-01
    • 2018-02-26
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多