コード例 #1
0
def example(x_train, y_train, x_test, y_test):

    from keras.callbacks import TensorBoard

    tensor_board = TensorBoard(log_dir='./Graph',
                               histogram_freq=1,
                               write_graph=True,
                               write_images=True)

    input1 = Input(shape=(28 * 28, ))
    hidden1 = Dense(512, activation='relu')(input1)
    hidden2 = Dense(256, activation='relu')(hidden1)
    output = Dense(10, activation='softmax')(hidden2)
    model = Model(inputs=input1, outputs=output)
    model.compile(optimizer='sgd',
                  loss='binary_crossentropy',
                  metrics=['accuracy', 'mean_squared_error'])

    model._get_distribution_strategy = lambda: None
    model.run_eagerly = lambda: None

    model.fit(x_train,
              y_train,
              epochs=5,
              validation_data=(x_test, y_test),
              callbacks=[tensor_board])
    test_loss, test_acc, test_mse = model.evaluate(x_test, y_test)
    print(
        f'Test loss: {test_loss}, test mse: {test_mse}, test accuracy: {test_acc}'
    )
    print(model.summary())
コード例 #2
0
def End_to_end_model(time_step, feature_dim, output_dim):
    frames_input = layers.Input(shape=(None, 299, 299, 3),
                                dtype='float32',
                                name='frame_input')

    InceptionModel = Inception_model()
    InceptionModel.load_weights('./out/Inception_weight.h5')
    feature_model = Model(
        inputs=InceptionModel.input,
        outputs=InceptionModel.get_layer('feature_out').output)
    # apply feature model to each time step of input
    feature_output = layers.TimeDistributed(feature_model,
                                            input_shape=(299, 299,
                                                         3))(frames_input)

    # apply lstm model to extracted feature
    lstmModel = LSTM_model(time_step, feature_dim, output_dim)
    # mask padding value
    # feature_output = layers.Masking(mask_value=0., input_shape=(time_step, feature_dim))(feature_output)
    model_output = lstmModel(inputs=feature_output)

    end_to_end_model = Model(inputs=frames_input, outputs=model_output)
    end_to_end_model._get_distribution_strategy = lambda: None

    return end_to_end_model
コード例 #3
0
def batchnorm_dropout(x_train, y_train, x_test, y_test):

    input1 = Input(shape=(28 * 28, ))
    hidden1 = Dense(512, activation='relu')(input1)
    hidden2 = BatchNormalization()(hidden1)
    hidden3 = Activation('relu')(hidden2)
    hidden4 = Dropout(0.5)(hidden3)

    hidden5 = Dense(256)(hidden4)
    hidden6 = BatchNormalization()(hidden5)
    hidden7 = Activation('relu')(hidden6)

    output = Dense(10, activation='softmax')(hidden7)
    model = Model(inputs=input1, outputs=output)
    model.compile(optimizer='Adam',
                  loss='binary_crossentropy',
                  metrics=['accuracy', 'mean_squared_error'])

    model._get_distribution_strategy = lambda: None
    model.run_eagerly = lambda: None

    model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))
    test_loss, test_acc, test_mse = model.evaluate(x_test, y_test)
    print(
        f'Test loss: {test_loss}, test mse: {test_mse}, test accuracy: {test_acc}'
    )
    print(model.summary())
    return model