Пример #1
0
def custom_fit(model,
               callbacks,
               x_train,
               y_train,
               x_test,
               y_test,
               epochs,
               batch_size,
               dir_name,
               compare_title,
               draw_step=10,
               verbose=1):
    epochs_step = int(epochs / draw_step)

    if dir_name != None:
        os.mkdir(dir_name)

    full_loss_history = np.empty(0)

    for init_epoch in np.arange(0, epochs, step=epochs_step):
        save = False if dir_name == None else True

        if save:
            save_path = dir_name + "/" + "val_loss.png"
        else:
            save_path = None

        history = model.fit(x=x_train,
                            y=y_train,
                            batch_size=batch_size,
                            epochs=init_epoch + epochs_step,
                            verbose=verbose,
                            callbacks=callbacks,
                            validation_data=(x_test, y_test),
                            initial_epoch=init_epoch)

        full_loss_history = np.append(full_loss_history,
                                      history.history['val_loss'])

        plt.plot(np.transpose(x_test)[0], y_test, '.')
        plt.plot(np.transpose(x_test)[0], model.predict(x_test), '.')
        plt.legend(('function', 'approximation'),
                   loc='upper left',
                   shadow=True)
        plt.title(compare_title + "\nval_loss = %.4f\nepoch = %d" %
                  (history.history["val_loss"][history.epoch.__len__() - 1],
                   init_epoch + epochs_step))

        if dir_name != None:
            plt.savefig(
                dir_name + "/" + "%.d_compare_%.4f.png" %
                (init_epoch + epochs_step,
                 history.history["val_loss"][history.epoch.__len__() - 1]),
                dpi=200)

        plt.show()
        plt.close()

        if (history.epoch.__len__() - 1 != epochs_step):
            epochs = init_epoch + history.epoch.__len__()
            break

    gr.plot_graphic(x=np.arange(1, epochs + 1),
                    y=full_loss_history,
                    x_label='epochs',
                    y_label='val_loss',
                    title="val_loss" + ' history',
                    save_path=save_path,
                    save=save,
                    show=True)

    return model
Пример #2
0
    # 4 model fitting
    model.compile(optimizer=optimizer, loss='mse', metrics=['mse'])

    history = model.fit(x=x_train,
                        y=y_train,
                        batch_size=batch_size,
                        epochs=epochs,
                        verbose=verbose,
                        callbacks=callbacks,
                        validation_data=(x_test, y_test))

    gr.plot_graphic(x=history.epoch,
                    y=np.array(history.history["val_loss"]),
                    x_label='epochs',
                    y_label='val_loss',
                    title="val_loss" + ' history',
                    save=False,
                    show=True)

    plt_x_zero = np.empty(0)
    plt_y_zero = np.empty(0)

    plt_x_one = np.empty(0)
    plt_y_one = np.empty(0)

    plt_x_two = np.empty(0)
    plt_y_two = np.empty(0)

    plt_x_three = np.empty(0)
    plt_y_three = np.empty(0)
Пример #3
0
    model = Sequential()

    model.add(Dense(first_layer_nur, input_dim=1, kernel_initializer='glorot_normal', activation='linear'))
    model.compile(loss='mean_squared_error', optimizer=SGD(lr=lr), metrics=['mae'])

    # 2.1.3-------------------------------------------------------
    history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=verbose)

    score = model.evaluate(x_test, y_test, verbose=verbose)

    print("\nabsolute_error on train data\t %.f%%" % (history.history['mean_absolute_error'][epochs - 1] * 100))
    print("\nabsolute_error on testing data %.f%%" % (score[1] * 100))
    print("loss on train data %.f%%" % (history.history['loss'][epochs - 1] * 100))

    gr.plot_graphic(x=history.epoch, y=np.array(history.history['loss']),
                    x_label='epochs', y_label='loss', title='mean_squared_error history', show=True)

    gr.plot_graphic(x=history.epoch, y=np.array(history.history['mean_absolute_error']),
                    x_label='epochs', y_label='accuracy', title='mean_absolute_error history', show=True)

    plt.plot(np.append(x_train, x_test), model.predict(np.append(x_train, x_test)), '.')
    plt.plot(np.append(x_train, x_test), np.append(y_train, y_test), '.')

    plt.legend(('approximation', 'function'), loc='upper left', shadow=True)

    plt.show()
    plt.close()

    # 2.2-------------------------------------------------------
    # Выбор скорости обучения