ValueError: object __array__ method not producing an array

Viewed 2350

Trying to run a code from the link below: https://machinelearningmastery.com/how-to-use-the-timeseriesgenerator-for-time-series-forecasting-in-keras/ Getting Error: ValueError: object array method not producing an array

Version of keras: 2.3.0-tf

Kindly help. Thanks!

# 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)

Error:

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
1 Answers

I am able to execute your code both in Jupyter and Google Colab using TF 2.2.0 and Keras 2.3.0.

Please refer complete code and output in below for the benefit of the community.

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)

Output:

2.3.1
2.2.0
[[11.588003]]

If your issue still persist, please let me know i am happy to help you.

Related