Keras (Tensorflow) Reshape Layer 输入错误

Keras (Tensorflow) Reshape Layer input error

我有一个重塑输入错误,我不知道为什么。 请求的形状是 1058400,即 (1, 21168) 乘以批量大小 50。 我不明白的是 677376 的表观输入大小。 我不知道这个值是从哪里来的。 reshape之前的层是一个flatten层,我在定义Reshape层的目标形状时直接使用它的形状

模型编译得很好,我使用 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/Whosebug/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.

问题版本

重要提示:我已经编辑了您的问题,因此它实际运行并代表了您的问题。 Input 应采用当前提供的 batch_shape。下次请确保您的代码有效,会更容易。

解决方案

解决方法很简单;您传递到网络的批次尺寸错误。

677376 / 21168 = 32 它是 批处理的默认大小 ,这是 predict 所期望的。如果它不同(在你的例子中是 50),你应该指定它,像这样:

out = model.predict(np.random.randint(1, 5, size=(50, 230, 230, 1)), batch_size=50)

现在一切都应该工作正常,如果你想硬编码,请记住指定批处理大小。