Example #1
0
def model_fit(train_X, train_y, test_X, test_y):
    model_save_file = ''
    model_save = {}

    test_regressor = ['GBDT']
    regressors = {
        'GBDT': gbdt_classifier,
    }
    print('reading training and testing data...')
    # train_x, train_y, test_x, test_y = read_data()

    for regressor in test_regressor:
        print('******************* %s ********************' % regressor)
        start_time = time.time()
        model = regressors[regressor](train_X, train_y)
        print('training took %fs!' % (time.time() - start_time))
        t0 = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
        pickle.dump(model, open('../model/GBDT_model_%s.h5' % t0, 'wb'))
        predict = model.predict(test_X)
        # print('test_y: {}\npredict: {}'.format(test_y, predict))
        # score = model.score(test_x, test_y)
        calMetrix(__file__, predict, test_y)
        plt.figure()
        plt.plot(np.arange(len(predict)), test_y, 'go-', label='true value')
        plt.plot(np.arange(len(predict)),
                 predict,
                 'ro-',
                 label='predict value')
        plt.title('%s' % regressor)
        plt.legend()
        plt.show()
Example #2
0
def model_fit(train_X, train_y, test_X, test_y):
    # define model
    model = Sequential()
    # model.add(TimeDistributed(cnn))
    model.add(TimeDistributed(Convolution1D(128, 4, border_mode='same'), input_shape=train_X.shape[1:]))
    model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
    model.add(TimeDistributed(Flatten()))
    model.add(LSTM(128, return_sequences=True, name="lstm_layer0"))
    model.add(LSTM(128, return_sequences=False, name="lstm_layer1"))
    # model.add(LSTM(100, return_sequences=True, name="lstm_layer2"))
    model.add(Dense(output_dim, activation='sigmoid'))
    # model.add(GlobalAveragePooling1D(name="global_avg"))

    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    # %%
    model.fit(train_X, train_y,batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_data=(test_X, test_y))

    test_y = test_y.reshape(test_y.size, 1)
    predict_y = model.predict(test_X)
    predict_y = predict_y.reshape(predict_y.size, 1)
    predict_y = classifyRes(predict_y)
    calMetrix(__file__, predict_y, test_y)
    t0 = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
    model.save('../model/CNN_LSTM_model_%s.h5'%t0)
def model_fit(train_X, train_y, test_X, test_y):
    # train_X, train_y, test_X, test_y = read_data()
    outdim = train_y.shape[1]
    validation_number = int(0.9 * train_X.shape[0])
    validation_X = train_X[validation_number:]
    validation_y = train_y[validation_number:]
    train_X = train_X[:validation_number]
    train_y = train_y[:validation_number]

    model = Sequential()
    model.add(LSTM(100, input_shape=(train_X.shape[1], train_X.shape[2]), return_sequences=True, dropout=0.25))
    # print(model.layers)
    model.add(LSTM(100, return_sequences=True))
    model.add(LSTM(100, return_sequences=True))
    model.add(LSTM(50, return_sequences=False))
    # model.add(Dense(outdim))
    model.add(Dense(outdim, activation='sigmoid'))
    # model.add(Dense(outdim, activation='softmax'))
    # sigmoid and softmax are activation functions used by the neural network output layer
    # for binary discrimination and multi-class discrimination
    # binary cross-entropy and categorical cross-entropy are corresponding loss functions

    # model.add(Activation('linear'))

    # model.compile(loss='mse', optimizer='rmsprop')
    model.compile(optimizer='adam',
                  loss='binary_crossentropy',
                  # loss='categorical_crossentropy',
                  metrics=['accuracy']
                  )

    # fit network
    history = model.fit(train_X, train_y, epochs=100, batch_size=72, validation_data=(validation_X, validation_y),
                        verbose=0,
                        shuffle=True)

    # summarize performance of the model
    # scores = model.evaluate(train_X, train_y, verbose=0)
    # print(model.metrics_names)
    # print("model loss: %.2f%%" % (scores*100))

    # plot history
    # plt.plot(history.history['loss'], label='train')
    # plt.plot(history.history['val_loss'], label='validation')
    # plt.legend()
    # plt.grid()
    # plt.savefig('loss.png')
    # plt.clf()
    np.savetxt('loss.csv', history.history['loss'])
    np.savetxt('val_loss.csv', history.history['val_loss'])
    t0 = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
    model.save('../model/LSTM_model_%s.h5'%t0)

    # make a prediction
    y_pre = model.predict(test_X)
    # yhat = yhat.reshape(train_y.shape[1])
    print(y_pre.shape)
    y_pre = classifyRes(y_pre)
    calMetrix(__file__, y_pre, test_y)

    # inv_yhat = yhat
    # inv_test_y = test_y
    np.savetxt('forecast.csv', y_pre)
    np.savetxt('actual.csv', test_y)