您必须在Tensorflow中编译模型错误

时间:2019-03-03 16:37:47

标签: tensorflow

我已经使用CIFAR-10数据集在tensorflow中构建了一个模型,并且尝试使用'tf.keras.preprocessing.image.ImageDataGenerator',但是却出现一个错误,我必须在使用之前编译我的模型它。我在急切模式下运行TensorFlow版本1.8.0。这是我的模特

data_gen = tf.keras.preprocessing.image.ImageDataGenerator (
    featurewise_center=True,
    featurewise_std_normalization=True,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True)  # randomly flip images

    # compute quantities required for featurewise normalization
    # (std, mean, and principal components if ZCA whitening is applied)
data_gen.fit(x_train)

def base_model():
    #input_layer = tf.keras.layers.Input(shape=(32, 32, 3), name="input_layer")
    num_classes=10
    #1
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Conv2D(32, (6,6), padding='same', input_shape=x_train.shape[1:],
                                     kernel_regularizer=keras.regularizers.l2(0.001)))
    model.add(tf.keras.layers.Activation('elu'))
    model.add(tf.keras.layers.BatchNormalization())
    #2
    model.add(tf.keras.layers.Conv2D(32, (3,3), padding='same',kernel_regularizer=keras.regularizers.l2(0.001)))
    model.add(tf.keras.layers.Activation('elu'))
    model.add(tf.keras.layers.BatchNormalization())
    model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))
    model.add(tf.keras.layers.Dropout(0.2))
    #3
    model.add(tf.keras.layers.Conv2D(64, (3,3), padding='same',kernel_regularizer=keras.regularizers.l2(0.001)))
    model.add(tf.keras.layers.Activation('elu'))
    model.add(tf.keras.layers.BatchNormalization())
    #4
    model.add(tf.keras.layers.Conv2D(64, (3,3), padding='same',kernel_regularizer=keras.regularizers.l2(0.001)))
    model.add(tf.keras.layers.Activation('elu'))
    model.add(tf.keras.layers.BatchNormalization())
    model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))
    model.add(tf.keras.layers.Dropout(0.3))
    #5
    model.add(tf.keras.layers.Conv2D(128, (3,3), padding='same',kernel_regularizer=keras.regularizers.l2(0.001)))
    model.add(tf.keras.layers.Activation('elu'))
    model.add(tf.keras.layers.BatchNormalization()) 
    #6
    model.add(tf.keras.layers.Conv2D(128, (3,3), padding='same',kernel_regularizer=keras.regularizers.l2(0.001)))
    model.add(tf.keras.layers.Activation('elu'))
    model.add(tf.keras.layers.BatchNormalization())
    model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))
    model.add(tf.keras.layers.Dropout(0.4))
    #7

    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))

    opt_rms = tf.train.AdamOptimizer(learning_rate=0.0001)

    model.compile(loss=tf.keras.losses.categorical_crossentropy, optimizer=opt_rms, metrics=['accuracy'])

    model.fit_generator(data_gen.flow(x_train, y_train_one_hot,batch_size=512),
                        epochs=5,validation_data=(x_test, y_test_one_hot))



    return model



base_model()

0 个答案:

没有答案