简体   繁体   English

不兼容的形状:Tensorflow/Keras Sequential LSTM with Autoencoder

[英]Incompatible Shapes: Tensorflow/Keras Sequential LSTM with Autoencoder

I am trying to set up an LSTM Autoencoder/Decoder for time series data and continually get Incompatible shapes error when trying to train the model. Following steps and using toy data from this example .我正在尝试为时间序列数据设置 LSTM 自动编码器/解码器,并在尝试训练 model 时不断出现Incompatible shapes错误。按照步骤并使用此示例中的玩具数据。 See below code and results.请参阅下面的代码和结果。 Note Tensorflow version 2.3.0.注意 Tensorflow 版本 2.3.0。

Create data.创建数据。 Put data into sequences to temporalize for LSTM in the form of (samples, timestamps, features).将数据放入序列中,以(样本、时间戳、特征)的形式对 LSTM 进行时间化。

timeseries = np.array([[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
                       [0.1**3, 0.2**3, 0.3**3, 0.4**3, 0.5**3, 0.6**3, 0.7**3, 0.8**3, 0.9**3]]).transpose()

timeseries_df = pd.DataFrame(timeseries)

def create_sequenced_dataset(X, time_steps=10):
    Xs, ys = [], []  # start empty list
    for i in range(len(X) - time_steps):  # loop within range of data frame minus the time steps
        v = X.iloc[i:(i + time_steps)].values  # data from i to end of the time step
        Xs.append(v)
        ys.append(X.iloc[i + time_steps].values)

    return np.array(Xs), np.array(ys)  # convert lists into numpy arrays and return

X, y = create_sequenced_dataset(timeseries_df, time_steps=3)
timesteps = X.shape[1]
n_features = X.shape[2]

Create the LSTM model with autoencoder/decoder given by the Repeat Vector and attempt to train the model.使用重复向量提供的自动编码器/解码器创建 LSTM model,并尝试训练 model。

model = Sequential()
model.add(LSTM(128, input_shape=(timesteps, n_features), return_sequences=False))
model.add(RepeatVector(timesteps))
model.add(LSTM(128, return_sequences=True))
model.add(TimeDistributed(Dense(n_features)))
model.compile(optimizer='adam', loss='mse')
model.summary()

model.fit(X, y, epochs=10, batch_size=4)

Consistently get error:一直报错:

tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [4,3,2] vs. [4,2]
     [[node gradient_tape/mean_squared_error/BroadcastGradientArgs (defined at <ipython-input-9-56896428cea9>:1) ]] [Op:__inference_train_function_10833]

X looks like: X 看起来像:

array([[[0.1  , 0.001],
        [0.2  , 0.008],
        [0.3  , 0.027]],
       [[0.2  , 0.008],
        [0.3  , 0.027],
        [0.4  , 0.064]],
       [[0.3  , 0.027],
        [0.4  , 0.064],
        [0.5  , 0.125]],
       [[0.4  , 0.064],
        [0.5  , 0.125],
        [0.6  , 0.216]],
       [[0.5  , 0.125],
        [0.6  , 0.216],
        [0.7  , 0.343]],
       [[0.6  , 0.216],
        [0.7  , 0.343],
        [0.8  , 0.512]]])

y looks like:你看起来像:

array([[0.4  , 0.064],
       [0.5  , 0.125],
       [0.6  , 0.216],
       [0.7  , 0.343],
       [0.8  , 0.512],
       [0.9  , 0.729]])

I hope the translator will correctly translate my idea.希望译者正确翻译我的想法。 I also did not understand at first what the problem was, but then I read the definition of an autoencoder again.我一开始也不明白问题是什么,但后来我又看了一遍自动编码器的定义。 Since this is an autoencoder, we apply X to the input and output (y does not participate in the model in any way, since we are trying to determine the dependencies in the data X and then recreate them).由于这是一个自动编码器,我们将 X 应用于输入和 output(y 不以任何方式参与 model,因为我们试图确定数据 X 中的依赖关系,然后重新创建它们)。 Some have code on this topic (y = x.copy ()), while here it applies (model.fit (X, X, epochs = 300, batch_size = 5, verbose = 0)).有些人有关于这个主题的代码(y = x.copy()),而在这里它适用(model.fit(X,X,epochs = 300,batch_size = 5,verbose = 0))。

As the message clearly says, it's the shape issue which you are passing to the model for fit.正如消息明确指出的那样,这是您要传递给 model 的形状问题。

From the above data which you have given X is having the shape of (6, 3, 2) and Y is having the shape of (6, 2) which is incompatible.根据您给出的上述数据,X 具有(6, 3, 2)的形状,而 Y 具有(6, 2)的形状,这是不兼容的。

Below is the modified code with the same input as per the example you have taken with X and Y having a shape (6,3,2) .下面是修改后的代码,其输入与您使用形状为(6,3,2)XY所采用的示例相同。

model = Sequential()
model.add(LSTM(128, input_shape=(timesteps, n_features), return_sequences=False))
model.add(RepeatVector(timesteps))
model.add(LSTM(128, return_sequences=True))
model.add(TimeDistributed(Dense(n_features)))
model.compile(optimizer='adam', loss='mse')
model.summary()  

model.fit(X,Y, epochs=10, batch_size=4)

Result:结果:

Epoch 1/10
2/2 [==============================] - 0s 5ms/step - loss: 0.0069
Epoch 2/10
2/2 [==============================] - 0s 4ms/step - loss: 0.0065
Epoch 3/10
2/2 [==============================] - 0s 4ms/step - loss: 0.0065
Epoch 4/10
2/2 [==============================] - 0s 4ms/step - loss: 0.0062
Epoch 5/10
2/2 [==============================] - 0s 4ms/step - loss: 0.0059
Epoch 6/10
2/2 [==============================] - 0s 4ms/step - loss: 0.0053
Epoch 7/10
2/2 [==============================] - 0s 5ms/step - loss: 0.0048
Epoch 8/10
2/2 [==============================] - 0s 5ms/step - loss: 0.0046
Epoch 9/10
2/2 [==============================] - 0s 5ms/step - loss: 0.0044
Epoch 10/10
2/2 [==============================] - 0s 6ms/step - loss: 0.0043
<tensorflow.python.keras.callbacks.History at 0x7ff352f9ccf8>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidArgumentError:与Keras LSTM Net不兼容的形状 - InvalidArgumentError: Incompatible shapes with Keras LSTM Net ValueError:使用 RandomizedSearchCV 的 Tensorflow LSTM 中的形状不兼容 - ValueError: Shapes are incompatible in Tensorflow LSTM using RandomizedSearchCV 使用 Keras 和 TensorFlow 构建 LSTM Sequential 模型 - Using Keras and TensorFlow building LSTM Sequential model 层顺序输入与层不兼容:LSTM 中的形状错误 - Input of layer sequential is incompatible with the layer: shapes error in LSTM Keras LSTM 层序贯_10的输入0与层不兼容 - Keras LSTM Input 0 of layer sequential_10 is incompatible with the layer 在 Keras 中实现 LSTM。 ValueError:层顺序与层不兼容 - Implementing LSTM in Keras. ValueError: layer sequential is incompatible with the layer Keras LSTM TensorFlow错误:“形状必须等于等级,但必须为1和0” - Keras LSTM TensorFlow Error: 'Shapes must be equal rank, but are 1 and 0' LSTM自动编码器关于Keras尺寸的问题 - LSTM autoencoder Issue on the dimension with Keras 如何向在 python 中构建为顺序 keras 模型的 LSTM 自动编码器添加注意层? - How to add an attention layer to LSTM autoencoder built as sequential keras model in python? TimeseriesGenerator feed 和 Dense 层的形状不兼容 - Keras/Tensorflow - Incompatible shapes of TimeseriesGenerator feed and Dense layer - Keras/Tensorflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM