简体   繁体   中英

TypeError: Expected int32, got list containing Tensors of type '_Message' instead … in Keras 2

I am running the example from the website: http://machinelearningmastery.com/time-series-forecasting-long-short-term-memory-network-python/

from pandas import DataFrame
from pandas import Series
from pandas import concat
from pandas import read_csv
from pandas import datetime
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from math import sqrt
from matplotlib import pyplot
import numpy

# date-time parsing function for loading the dataset
def parser(x):
    return datetime.strptime('190'+x, '%Y-%m')

# frame a sequence as a supervised learning problem
def timeseries_to_supervised(data, lag=1):
    df = DataFrame(data)
    columns = [df.shift(i) for i in range(1, lag+1)]
    columns.append(df)
    df = concat(columns, axis=1)
    df.fillna(0, inplace=True)
    return df

# create a differenced series
def difference(dataset, interval=1):
    diff = list()
    for i in range(interval, len(dataset)):
        value = dataset[i] - dataset[i - interval]
        diff.append(value)
    return Series(diff)

# invert differenced value
def inverse_difference(history, yhat, interval=1):
    return yhat + history[-interval]

# scale train and test data to [-1, 1]
def scale(train, test):
    # fit scaler
    scaler = MinMaxScaler(feature_range=(-1, 1))
    scaler = scaler.fit(train)
    # transform train
    train = train.reshape(train.shape[0], train.shape[1])
    train_scaled = scaler.transform(train)
    # transform test
    test = test.reshape(test.shape[0], test.shape[1])
    test_scaled = scaler.transform(test)
    return scaler, train_scaled, test_scaled

# inverse scaling for a forecasted value
def invert_scale(scaler, X, value):
    new_row = [x for x in X] + [value]
    array = numpy.array(new_row)
    array = array.reshape(1, len(array))
    inverted = scaler.inverse_transform(array)
    return inverted[0, -1]

# fit an LSTM network to training data
def fit_lstm(train, batch_size, nb_epoch, neurons):
    X, y = train[:, 0:-1], train[:, -1]
    X = X.reshape(X.shape[0], 1, X.shape[1])
    model = Sequential()
    model.add(LSTM(neurons, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    for i in range(nb_epoch):
        model.fit(X, y, epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
        model.reset_states()
    return model

# make a one-step forecast
def forecast_lstm(model, batch_size, X):
    X = X.reshape(1, 1, len(X))
    yhat = model.predict(X, batch_size=batch_size)
    return yhat[0,0]

# load dataset
series = read_csv('shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)

# transform data to be stationary
raw_values = series.values
diff_values = difference(raw_values, 1)

# transform data to be supervised learning
supervised = timeseries_to_supervised(diff_values, 1)
supervised_values = supervised.values

# split data into train and test-sets
train, test = supervised_values[0:-12], supervised_values[-12:]

# transform the scale of the data
scaler, train_scaled, test_scaled = scale(train, test)

# repeat experiment
repeats = 30
error_scores = list()
for r in range(repeats):
    # fit the model
    lstm_model = fit_lstm(train_scaled, 1, 3000, 4)
    # forecast the entire training dataset to build up state for forecasting
    train_reshaped = train_scaled[:, 0].reshape(len(train_scaled), 1, 1)
    lstm_model.predict(train_reshaped, batch_size=1)
    # walk-forward validation on the test data
    predictions = list()
    for i in range(len(test_scaled)):
        # make one-step forecast
        X, y = test_scaled[i, 0:-1], test_scaled[i, -1]
        yhat = forecast_lstm(lstm_model, 1, X)
        # invert scaling
        yhat = invert_scale(scaler, X, yhat)
        # invert differencing
        yhat = inverse_difference(raw_values, yhat, len(test_scaled)+1-i)
        # store forecast
        predictions.append(yhat)
    # report performance
    rmse = sqrt(mean_squared_error(raw_values[-12:], predictions))
    print('%d) Test RMSE: %.3f' % (r+1, rmse))
    error_scores.append(rmse)

# summarize results
results = DataFrame()
results['rmse'] = error_scores
print(results.describe())
results.boxplot()
pyplot.show()

But got the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-a64098fe2161> in <module>()
    100 for r in range(repeats):
    101         # fit the model
--> 102         lstm_model = fit_lstm(train_scaled, 1, 3000, 4)
    103         # forecast the entire training dataset to build up state for forecasting
    104         train_reshaped = train_scaled[:, 0].reshape(len(train_scaled), 1, 1)

<ipython-input-9-a64098fe2161> in fit_lstm(train, batch_size, nb_epoch, neurons)
     64         X = X.reshape(X.shape[0], 1, X.shape[1])
     65         model = Sequential()
---> 66         model.add(LSTM(neurons, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True))
     67         model.add(Dense(1))
     68         model.compile(loss='mean_squared_error', optimizer='adam')

/usr/local/lib/python3.4/dist-packages/keras/models.py in add(self, layer)
    434                 # and create the node connecting the current layer
    435                 # to the input layer we just created.
--> 436                 layer(x)
    437 
    438             if len(layer.inbound_nodes) != 1:

/usr/local/lib/python3.4/dist-packages/keras/layers/recurrent.py in __call__(self, inputs, initial_state, **kwargs)
    260         # modify the input spec to include the state.
    261         if initial_state is None:
--> 262             return super(Recurrent, self).__call__(inputs, **kwargs)
    263 
    264         if not isinstance(initial_state, (list, tuple)):

/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
    567                                          '`layer.build(batch_input_shape)`')
    568                 if len(input_shapes) == 1:
--> 569                     self.build(input_shapes[0])
    570                 else:
    571                     self.build(input_shapes)

/usr/local/lib/python3.4/dist-packages/keras/layers/recurrent.py in build(self, input_shape)
   1041                                         initializer=bias_initializer,
   1042                                         regularizer=self.bias_regularizer,
-> 1043                                         constraint=self.bias_constraint)
   1044         else:
   1045             self.bias = None

/usr/local/lib/python3.4/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     85                 warnings.warn('Update your `' + object_name +
     86                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 87             return func(*args, **kwargs)
     88         wrapper._original_function = func
     89         return wrapper

/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
    389         if dtype is None:
    390             dtype = K.floatx()
--> 391         weight = K.variable(initializer(shape), dtype=dtype, name=name)
    392         if regularizer is not None:
    393             self.add_loss(regularizer(weight))

/usr/local/lib/python3.4/dist-packages/keras/layers/recurrent.py in bias_initializer(shape, *args, **kwargs)
   1033                         self.bias_initializer((self.units,), *args, **kwargs),
   1034                         initializers.Ones()((self.units,), *args, **kwargs),
-> 1035                         self.bias_initializer((self.units * 2,), *args, **kwargs),
   1036                     ])
   1037             else:

/usr/local/lib/python3.4/dist-packages/keras/backend/tensorflow_backend.py in concatenate(tensors, axis)
   1721         return tf.sparse_concat(axis, tensors)
   1722     else:
-> 1723         return tf.concat([to_dense(x) for x in tensors], axis)
   1724 
   1725 

/usr/local/lib/python3.4/dist-packages/tensorflow/python/ops/array_ops.py in concat(concat_dim, values, name)
    865       ops.convert_to_tensor(concat_dim,
    866                             name="concat_dim",
--> 867                             dtype=dtypes.int32).get_shape(
    868                             ).assert_is_compatible_with(tensor_shape.scalar())
    869       return identity(values[0], name=scope)

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
    655 
    656         if ret is None:
--> 657           ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    658 
    659         if ret is NotImplemented:

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    178                                          as_ref=False):
    179   _ = as_ref
--> 180   return constant(v, dtype=dtype, name=name)
    181 
    182 

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
    161   tensor_value = attr_value_pb2.AttrValue()
    162   tensor_value.tensor.CopyFrom(
--> 163       tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
    164   dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
    165   const_tensor = g.create_op(

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape)
    351       nparray = np.empty(shape, dtype=np_dt)
    352     else:
--> 353       _AssertCompatible(values, dtype)
    354       nparray = np.array(values, dtype=np_dt)
    355       # check to them.

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_util.py in _AssertCompatible(values, dtype)
    288     else:
    289       raise TypeError("Expected %s, got %s of type '%s' instead." %
--> 290                       (dtype.name, repr(mismatch), type(mismatch).__name__))
    291 
    292 

TypeError: Expected int32, got list containing Tensors of type '_Message' instead.

I read the website discussion, knowing some old version of keras has such problem, so I updated to Keras==2.0.6 but still have the same problems ...

Any idea what can be done to fix this error? Thanks!

Your TensorFlow is too old, you should at least try TensorFlow 1.1. I believe Keras 2.0 requires at least TensorFlow 1.0.

I solved this problem like so:

I only take tensorflow_backend.py in concatenate(tensors, axis)

Change this:

return tf.concat([to_dense(x) for x in tensors], axis)  

To this:

return tf.concat(axis,[to_dense(x) for x in tensors])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM