Python CNN LSTM(值错误步幅的长度应为 1、1 或 3,但为 2)
Posted
技术标签:
【中文标题】Python CNN LSTM(值错误步幅的长度应为 1、1 或 3,但为 2)【英文标题】:Python CNN LSTM (Value Error strides should be of length 1, 1 or 3 but was 2) 【发布时间】:2020-09-17 12:55:42 【问题描述】:我一直在尝试在 mnist 数据集上训练一个 convlstm 模型,以拓宽我在模型开发方面的知识。我无法逃避标题中包含的错误。感谢任何帮助或提示!
我知道步幅的默认值为 (1,1),但不确定如何设置 2。
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, CuDNNLSTM, TimeDistributed, Reshape
from keras.utils import to_categorical
from keras.layers.convolutional import Conv2D, Conv3D
from keras.layers.pooling import MaxPooling2D, MaxPool3D
from keras.layers.core import Flatten
def prep_pixels(train, test):
# convert from integers to floats
train_norm = train.astype('float32')
test_norm = test.astype('float32')
# normalize to range 0-1
train_norm = train_norm / 255.0
test_norm = test_norm / 255.0
# return normalized images
return train_norm, test_norm
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape((x_train.shape[0], 28, 28, 1))
x_test = x_test.reshape((x_test.shape[0], 28, 28, 1))
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
x_train, x_test = prep_pixels(x_train, x_test)
model = Sequential()
model.add(TimeDistributed(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))))
model.add(TimeDistributed((MaxPooling2D((2, 2)))))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(32, activation='relu', return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))
opt = tf.keras.optimizers.Adam(lr=1e-3, decay=1e-5)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model.fit(x_train, y_train, epochs=1, validation_data=(x_test, y_test))
错误
model.fit(x_train, y_train, epochs=1, validation_data=(x_test, y_test))
strides = _get_sequence(strides, n, channel_index, "strides")
ValueError: strides 的长度应为 1、1 或 3,但为 2
【问题讨论】:
你可以打电话给model.add(TimeDistributed(Conv2D(64, (5, 5), padding='same', strides = 2)))
和model.add(Activation('relu'))
而不是strides = _get_sequence(strides, n, channel_index, "strides") and let me know about its exception.
感谢您的帮助。 'strides = _get_sequence(strides, n, channel_index, "strides")' 是错误的一部分。它不在我的代码中。另外,我尝试了你所说的,它说和上面一样的错误。
【参考方案1】:
您似乎还没有为您的 ConvLSTM 创建窗口数据集。因此,您可能希望在致电 model.fit
d_train = tf.keras.preprocessing.sequence.TimeseriesGenerator(x_train, y_train, length=5, batch_size=64) # window size = 5
d_test = tf.keras.preprocessing.sequence.TimeseriesGenerator(x_test, y_test, length=5)
model.fit(d_train, epochs=1, validation_data=d_test)
为了与您的损失函数保持一致,您需要禁用返回序列(或添加另一个不返回序列的层)。
model.add(tf.keras.layers.LSTM(32, activation='relu', return_sequences=False))
【讨论】:
当我添加它时,它给了我一个运行时错误,说“您必须在使用它之前编译您的模型。”虽然我在适合它之前正在编译。对 return_sequence 的良好调用完全错过了 将输入大小添加到您的第一个 Conv 层model.add(TimeDistributed(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), input_shape=(5, 28, 28, 1))) # where 5 is the window size (be consistent)
谢谢 Varun,你是对的。我没有意识到我必须多次指定输入形状。我给了你一票。以上是关于Python CNN LSTM(值错误步幅的长度应为 1、1 或 3,但为 2)的主要内容,如果未能解决你的问题,请参考以下文章
使用 CNN 和 LSTM 在 Tensorflow 中占位符大小和类型的错误