【问题标题】:Keras (Tensorflow) Reshape Layer input errorKeras(Tensorflow)重塑图层输入错误
【发布时间】:2019-03-07 22:53:21
【问题描述】:

我有一个整形输入错误,我不知道为什么。 请求的形状是 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/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.

【问题讨论】:

  • 您能否提供实际工作代码来复制您的问题? Input 没有参数 batch_size。我想,否则,此代码不是您问题的根源。
  • 请确认我的编辑实际上是关于您的根本问题,下面的解决方案可以解决它。我几乎可以肯定这就是您所追求的,但想要对 OP 进行第二次检查。以防它没有适当地编辑您的问题(包括可以运行的代码)。

标签: tensorflow keras keras-layer tf.keras


【解决方案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)

现在一切都应该正常了,如果你想硬编码,记得指定批量大小。

【讨论】:

  • 是的,你完全正确,现在它运行了,我怎么会错过这个。谢谢你。
猜你喜欢
  • 1970-01-01
  • 2020-04-29
  • 2018-09-12
  • 2019-12-28
  • 2018-11-22
  • 1970-01-01
  • 2017-07-30
  • 2019-07-31
  • 1970-01-01
相关资源
最近更新 更多