Beispiel #1
0
def test(X, y, initializer):

    title = f" test {type(initializer).__name__} "
    print("-" * 20 + title + "-" * 20)

    # create RBF network as keras sequential model
    model = Sequential()
    rbflayer = RBFLayer(10,
                        initializer=initializer,
                        betas=2.0,
                        input_shape=(1, ))
    outputlayer = Dense(1, use_bias=False)

    model.add(rbflayer)
    model.add(outputlayer)

    model.compile(loss='mean_squared_error', optimizer=RMSprop())

    # fit and predict
    model.fit(X, y, batch_size=50, epochs=2000, verbose=0)

    y_pred = model.predict(X)

    # show graph
    plt.plot(X, y_pred)  # prediction
    plt.plot(X, y)  # response from data
    plt.plot([-1, 1], [0, 0], color='black')  # zero line
    plt.xlim([-1, 1])

    # plot centers
    centers = rbflayer.get_weights()[0]
    widths = rbflayer.get_weights()[1]
    plt.scatter(centers, np.zeros(len(centers)), s=20 * widths)

    plt.show()

    # calculate and print MSE
    y_pred = y_pred.squeeze()
    print(f"MSE: {MSE(y, y_pred):.4f}")

    # saving to and loading from file
    filename = f"rbf_{type(initializer).__name__}.h5"
    print(f"Save model to file {filename} ... ", end="")
    model.save(filename)
    print("OK")

    print(f"Load model from file {filename} ... ", end="")
    newmodel = load_model(filename, custom_objects={'RBFLayer': RBFLayer})
    print("OK")

    # check if the loaded model works same as the original
    y_pred2 = newmodel.predict(X).squeeze()
    print("Same responses: ", all(y_pred == y_pred2))
    # I know that I compared floats, but results should be identical

    # save, widths & weights separately
    np.save("centers", centers)
    np.save("widths", widths)
    np.save("weights", outputlayer.get_weights()[0])
Beispiel #2
0
    rbflayer = RBFLayer(10,
                        initializer=InitCentersRandom(X),
                        betas=2.0,
                        input_shape=(1,))
    model.add(rbflayer)
    model.add(Dense(1))

    model.compile(loss='mean_squared_error',
                  optimizer=RMSprop())

    model.fit(X, y,
              batch_size=50,
              epochs=2000,
              verbose=1)

    y_pred = model.predict(X)

    print(rbflayer.get_weights())

    
    plt.plot(X, y_pred)
    plt.plot(X, y)
    plt.plot([-1, 1], [0, 0], color='black')
    plt.xlim([-1, 1])
    
    centers = rbflayer.get_weights()[0]
    widths = rbflayer.get_weights()[1]
    plt.scatter(centers, np.zeros(len(centers)), s=20*widths)
    
    plt.show()
Beispiel #3
0
               optimizer=RMSprop(),
               metrics=['accuracy', 'mse', 'mae'])
 
 history = model.fit(X_train, y_train,
                     batch_size=50,
                     epochs=200,
                     verbose=1)
 
 y_pred = model.predict(X_test)
 
 # get time
 end = time.time()
 time_total = end-start 
 
 y_pred_denormalization = scaler.inverse_transform(y_pred)   
 y_test_denormalization = scaler.inverse_transform(y_test)
 
 error_graph()
 graph()
 
 print(rbflayer.get_weights())
 # MAPE
 mape = mape(y_test_denormalization, y_pred_denormalization)
 # RMSE
 rmse = np.sqrt(mean_squared_error(
         y_test_denormalization, y_pred_denormalization))
 
 forecast_result = scaler.inverse_transform(model.predict(
         data_bm.iloc[-1:,0:1]))