如何在CNN-LSTM模型上应用model.fit()函数?

时间:2020-06-02 04:58:11

标签: python tensorflow lstm recurrent-neural-network cnn

我试图用它来将图像分为两类。我也应用了model.fit()函数,但是显示错误。

ValueError:传递形状为(90,1)的目标数组以输出形状为(None,10),同时将其用作损失binary_crossentropy。这种损失会导致目标与输出的形状相同。

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, LSTM
import pickle
import numpy as np

X = np.array(pickle.load(open("X.pickle","rb")))
Y = np.array(pickle.load(open("Y.pickle","rb")))

#scaling our image data
X = X/255.0
model = Sequential()

model.add(Conv2D(64 ,(3,3), input_shape = (300,300,1)))

# model.add(MaxPooling2D(pool_size = (2,2)))

model.add(tf.keras.layers.Reshape((16, 16*512)))
model.add(LSTM(128, activation='relu', return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(128, activation='relu'))
model.add(Dropout(0.2))

model.add(Dense(32, activation='relu'))
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='binary_crossentropy', optimizer=opt,
             metrics=['accuracy'])

# model.summary()
model.fit(X, Y, batch_size=32, epochs = 2, validation_split=0.1)

1 个答案:

答案 0 :(得分:1)

如果您的问题是绝对的,则问题是您使用的是 binary_crossentropy而不是categorical_crossentropy ;确保您确实有分类问题,而不是二进制分类问题。

另外,请注意,如果您的标签是简单的整数格式,例如[1,2,3,4 ...]并且未进行一次热编码,则loss_function应该为sparse_categorical_crossentropy,而不是{{ 1}}。

如果您确实有二进制分类问题,例如上述错误,请确保:

  1. 损失是binary_crossentroy + categorical_crossentropy
  2. 损失是categorical_crossentropy + Dense(1,activation='sigmoid')