我有一个重塑输入错误,我不知道为什么。 要求的形状为1058400,即(1,21168)乘以50的批量大小。 我不明白的是677376的表观输入大小。 我不知道这个值从哪里来。重塑之前的层是一个平坦的层,当我定义重塑层的目标形状时,我直接使用它的形状。
模型编译得很好,我使用Tensorflow作为后端,因此它在运行时定义。但是该错误仅在我输入日期时出现。
代码:
import numpy as np
import tensorflow as tf
import keras.backend as K
from keras import Model
from keras.layers import LSTM, Conv2D, Dense, Flatten, Input, Reshape
from keras.optimizers import Adam
config = tf.ConfigProto(allow_soft_placement=True)
sess = tf.Session(config=config)
K.set_session(sess)
input = Input(batch_shape=(50, 230, 230, 1))
conv1 = Conv2D(
filters=12, kernel_size=(7, 7), strides=(1, 1), padding="valid", activation="relu"
)(input)
conv2 = Conv2D(
filters=24, kernel_size=(5, 5), strides=(1, 1), padding="valid", activation="relu"
)(conv1)
conv3 = Conv2D(
filters=48, kernel_size=(3, 3), strides=(2, 2), padding="valid", activation="relu"
)(conv2)
conv4 = Conv2D(
filters=48, kernel_size=(5, 5), strides=(5, 5), padding="valid", activation="relu"
)(conv3)
conv_out = Flatten()(conv4)
conv_out = Reshape(target_shape=(1, int(conv_out.shape[1])))(conv_out)
conv_out = Dense(128, activation="relu")(conv_out)
rnn_1 = LSTM(128, stateful=True, return_sequences=True)(conv_out)
rnn_2 = LSTM(128, stateful=True, return_sequences=True)(rnn_1)
rnn_3 = LSTM(128, stateful=True, return_sequences=False)(rnn_2)
value = Dense(1, activation="linear")(rnn_3)
policy = Dense(5, activation="softmax")(rnn_3)
model = Model(inputs=input, outputs=[value, policy])
adam = Adam(lr=0.001)
model.compile(loss="mse", optimizer=adam)
model.summary()
out = model.predict(np.random.randint(1, 5, size=(50, 230, 230, 1)))
print(out)
摘要:
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (50, 230, 230, 1) 0
__________________________________________________________________________________________________
conv2d (Conv2D) (50, 224, 224, 12) 600 input_1[0][0]
__________________________________________________________________________________________________
conv2d_1 (Conv2D) (50, 220, 220, 24) 7224 conv2d[0][0]
__________________________________________________________________________________________________
conv2d_2 (Conv2D) (50, 109, 109, 48) 10416 conv2d_1[0][0]
__________________________________________________________________________________________________
conv2d_3 (Conv2D) (50, 21, 21, 48) 57648 conv2d_2[0][0]
__________________________________________________________________________________________________
flatten (Flatten) (50, 21168) 0 conv2d_3[0][0]
__________________________________________________________________________________________________
reshape (Reshape) (50, 1, 21168) 0 flatten[0][0]
__________________________________________________________________________________________________
dense (Dense) (50, 1, 128) 2709632 reshape[0][0]
__________________________________________________________________________________________________
lstm (LSTM) (50, 1, 128) 131584 dense[0][0]
__________________________________________________________________________________________________
lstm_1 (LSTM) (50, 1, 128) 131584 lstm[0][0]
__________________________________________________________________________________________________
lstm_2 (LSTM) (50, 128) 131584 lstm_1[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (50, 1) 129 lstm_2[0][0]
__________________________________________________________________________________________________
dense_2 (Dense) (50, 5) 645 lstm_2[0][0]
==================================================================================================
Total params: 3,181,046
Trainable params: 3,181,046
Non-trainable params: 0
编辑:
上述代码错误:
Traceback (most recent call last):
File "foo.py", line 45, in <module>
out = model.predict(np.random.randint(1, 5, size=(50, 230, 230, 1)))
File "/home/vyz/.conda/envs/stackoverflow/lib/python3.6/site-packages/keras/engine/training.py", line 1157, in predict
'Batch size: ' + str(batch_size) + '.')
ValueError: In a stateful network, you should only pass inputs with a number of samples that can be divided by the batch size. Found: 50 samples. Batch size: 32.
答案 0 :(得分:1)
重要提示:我已经编辑了您的问题,因此它可以实际运行并代表您的问题。 Input
应该采用batch_shape
当前提供的格式。下次,请确保您的代码可以正常使用,
解决方案非常简单;您传递到网络的批次的尺寸错误。
677376 / 21168 = 32
是 predict所期望的默认批次大小。您应该指定它是否不同(在您的情况下为50),如下所示:
out = model.predict(np.random.randint(1, 5, size=(50, 230, 230, 1)), batch_size=50)
现在一切都可以正常工作,如果要对其进行硬编码,请记住指定批次大小。