简体   繁体   English

TimeseriesGenerator feed 和 Dense 层的形状不兼容 - Keras/Tensorflow

[英]Incompatible shapes of TimeseriesGenerator feed and Dense layer - Keras/Tensorflow

I have some dimension issues when I am using the Dense layer in combination with TimeseriesGenerator .当我将Dense层与TimeseriesGenerator结合使用时,我遇到了一些尺寸问题。

My training data looks like this:我的训练数据如下所示:

X = (1 000 000, 6)

y = (1 000 000, 2)

I put all of this in TimeseriesGenerator :我把所有这些都放在TimeseriesGenerator

train_gen = TimeseriesGenerator(X, y, length=4, batch_size=32, stride=1)

and I receive:我收到:

train_gen[0][0].shape
(32, 4, 6)

train_gen[0][1].shape
(32, 2)

afterwards I created a simple model:之后我创建了一个简单的模型:

optimizer = keras.optimizers.RMSprop()

model = Sequential()
model.add(Dense(12, input_shape=(4,6), activation='tanh'))
model.add(Dense(40, activation='tanh'))
model.add(Dense(2, activation='tanh'))

model.compile(loss='mean_absolute_error', optimizer= optimizer, metrics=['mean_absolute_error', 'accuracy'])

and the last step - fitting data:最后一步 - 拟合数据:

mw = model.fit_generator(generator=train_gen, epochs=1, verbose=1)

Now I get an error.现在我收到一个错误。 The last layer has a dimension issue:最后一层有一个维度问题:

InvalidArgumentError: Incompatible shapes: [32,4,2] vs. [32,2] [Op:Sub] name: loss/dense_44_loss/sub/

I assume the model wants to compare the [32,4,2] shaped output of the model with the given [32,2] shaped training data.我假设模型想要将模型的 [32,4,2] 形输出与给定的 [32,2] 形训练数据进行比较。

I haven't found a solution yet.我还没有找到解决办法。 I think I definitely need the TimeseriesGenerator due to the size of my original dataset which has 160 billion samples and I don't have enough RAM.我想我肯定需要TimeseriesGenerator因为我的原始数据集有 1600 亿个样本,而且我没有足够的 RAM。 Can someone help me?有人能帮我吗?

Your last layer has a dimensionality error, which you can simply fix by adding a Flatten() layer like this:你的最后一层有一个维度错误,你可以简单地通过添加一个 Flatten() 层来修复它,如下所示:

import numpy as np
from keras.preprocessing.sequence import TimeseriesGenerator
from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.optimizers import RMSprop

X = np.random.rand(996, 6)
y = np.random.rand(996, 2)

t_gen = TimeseriesGenerator(X, y, length=4, batch_size=32, stride=1)

optimizer = RMSprop()

model = Sequential()
model.add(Dense(12, input_shape=(4,6), activation='tanh'))
model.add(Dense(40, activation='tanh'))
model.add(Flatten())
model.add(Dense(2, activation='tanh'))

model.compile(loss='mean_absolute_error', 
    optimizer= optimizer, 
    metrics=['mean_absolute_error', 'accuracy'])


mw = model.fit_generator(generator=t_gen, epochs=1, verbose=1)

The result will be like this:结果将是这样的:

Epoch 1/1
31/31 [==============================] - 0s 12ms/step - loss: 0.2817 - mean_absolute_error: 0.2817 - accuracy: 0.4748

Your new model is like this:你的新模型是这样的:

在此处输入图片说明

Usually the TimeseriesGenerator is used for creating 3D data for timeseries applications and then you use LSTM to process this 3D data.通常TimeseriesGenerator用于为时间序列应用程序创建 3D 数据,然后您使用LSTM来处理此 3D 数据。

You can still use the TimeseriesGenerator together with Dense layers with some hacks.您仍然可以通过一些技巧将TimeseriesGeneratorDense层一起使用。

Change the length of the TimeseriesGenerator to be 1 .TimeseriesGeneratorlength更改为1

train_gen = TimeseriesGenerator(X, y, length=1, batch_size=32, stride=1)
#shapes : train_gen[0][0] : (32,1,6)

Now your data is essentially a 2D data and that middle dimension is just useless.现在你的数据本质上是一个二维数据,中间维度是没有用的。 So just create a Lambda layer which drops this middle dimension in your model as the first layer.因此,只需创建一个Lambda layer ,将模型中的这个中间维度作为第一层。

import keras.backend as K
from keras.layers import Lambda


optimizer = keras.optimizers.RMSprop()

model = Sequential()
model.add(Lambda(lambda inp: K.squeeze(inp, axis=1),input_shape=(1,6)))
model.add(Dense(12, activation='tanh'))
model.add(Dense(40, activation='tanh'))
model.add(Dense(2, activation='tanh'))
model.compile(loss='mean_absolute_error', optimizer= optimizer, metrics=['mean_absolute_error', 'accuracy'])

print(model.output_shape) #(None,2)

Your model output shape must match with your labels shape.您的模型输出形状必须与您的标签形状匹配。

mw = model.fit_generator(generator=train_gen, epochs=1, verbose=1) #runs

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

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