InvalidArgumentError:要重塑的输入是具有50个值的张量,但请求的形状为1

时间:2019-05-24 10:12:41

标签: python tensorflow keras deep-learning conv-neural-network

我正在使用CNN的Keras功能API,该模型如下所示:输入是大小为50x50x1的图像,并且有2个输出层,其中一个带有3个标签,第二个带有7个标签,隐藏层包含:卷积层> MaxPooling层>展平层>具有128个单位的完全连接的层。

在第一个时期出现错误:

  

InvalidArgumentError:要重塑的输入是具有50个值的张量,但是   要求的形状为0 [[{{node   training_4 / Adam / gradients / loss_4 / outc_loss / Mean_grad / Reshape}}]

错误消息中的值50取决于其随批次大小而变化的批次大小,但是请求形状值有时会更改(0、1、1065353216等)

我无法理解问题所在,并且阅读过类似的问题,但没有一个帮助我。我是新手。 预先感谢您的帮助:)

我的代码是:

layer_inp = keras.Input(shape=(50,50,1))

layer_int = Conv2D(32, (3,3), activation='relu')(layer_inp)
layer_int = MaxPooling2D(pool_size=(2,2))(layer_int)
layer_int = Flatten()(layer_int)

layer_int = Dense(128, activation='sigmoid')(layer_int)

layer_color_out = Dense(3, activation='softmax', name='outc')(layer_int)
layer_piece_out = Dense(7, activation='softmax', name='outp')(layer_int)

classifier = keras.Model(inputs=layer_inp, outputs=[layer_color_out, layer_piece_out])

losses = {'outc':'categorical_crossentropy', 'outp':'categorical_crossentropy'}
weights = {'outc':1.0, 'outp':1.0}
metrices = {'outc':'accuracy', 'outp':'accuracy'}

classifier.compile(optimizer='adam', loss=losses, loss_weights=weights, metrics=metrices)

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1./255,
                                  zoom_range=0.2,
                                  vertical_flip=True,
                                  horizontal_flip=True)

def batch_generator(generator):
    while True:
        X_temp, y_temp = next(generator)
        yield X_temp, [y_temp[:,:3], y_temp[:,3:]]

# X_train.shape is (3000,50,50,1), y_train.shape is (3000,10)
train_datagen.fit(X_train)
train_set = train_datagen.flow(X_train, y_train, batch_size=50)

classifier.fit_generator(batch_generator(train_set),
                        steps_per_epoch=500,
                        epochs=25,)

错误回溯为:

Epoch 1/25
Traceback (most recent call last):

  File "<ipython-input-8-7de0904e0d57>", line 3, in <module>
    epochs=25,)

  File "C:\Users\AMIT\Anaconda3\envs\venv-der\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)

  File "C:\Users\AMIT\Anaconda3\envs\venv-der\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
    initial_epoch=initial_epoch)

  File "C:\Users\AMIT\Anaconda3\envs\venv-der\lib\site-packages\keras\engine\training_generator.py", line 217, in fit_generator
    class_weight=class_weight)

  File "C:\Users\AMIT\Anaconda3\envs\venv-der\lib\site-packages\keras\engine\training.py", line 1217, in train_on_batch
    outputs = self.train_function(ins)

  File "C:\Users\AMIT\Anaconda3\envs\venv-der\lib\site-packages\keras\backend\tensorflow_backend.py", line 2715, in __call__
    return self._call(inputs)

  File "C:\Users\AMIT\Anaconda3\envs\venv-der\lib\site-packages\keras\backend\tensorflow_backend.py", line 2675, in _call
    fetched = self._callable_fn(*array_vals)

  File "C:\Users\AMIT\Anaconda3\envs\venv-der\lib\site-packages\tensorflow\python\client\session.py", line 1439, in __call__
    run_metadata_ptr)

  File "C:\Users\AMIT\Anaconda3\envs\venv-der\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))

InvalidArgumentError: Input to reshape is a tensor with 50 values, but the requested shape has 0
     [[{{node training/Adam/gradients/loss/outc_loss/Sum_1_grad/Reshape}}]]

0 个答案:

没有答案