Exemple #1
0
def data_pred_plot(prediction):
    data_pred = pd.DataFrame(columns=['observed', 'prediction'],
                             index=np.arange(0, int(len(y_test)), 1))
    data_pred['observed'] = y_test
    data_pred['prediction'] = prediction
    from keras.metrics import RootMeanSquaredError
    m = RootMeanSquaredError()
    m.update_state(np.array(data_pred['observed']),
                   np.array(data_pred['prediction']))
    return (m.result().numpy(), data_pred.plot())
def train_model(feature: Series, label: Series, learning_rate, number_epochs,
                batch_size):
    # Create model
    model = Sequential()
    model.add(Dense(units=1, activation='relu', input_shape=(1, )))
    model.compile(optimizer=RMSprop(lr=learning_rate),
                  loss='mean_squared_error',
                  metrics=[RootMeanSquaredError()])

    # Train model
    model_hist = model.fit(x=feature,
                           y=label,
                           epochs=number_epochs,
                           batch_size=batch_size)

    # Get root mean squared error and epoch data
    df_hist = DataFrame(model_hist.history)
    root_mean_squared_error = df_hist['root_mean_squared_error']

    # Get weight, bias
    weights = model.get_weights()
    weight = weights[0]
    bias = weights[1]

    return weight, bias, root_mean_squared_error, model_hist.epoch
Exemple #3
0
def build_model(input_shape):
    model = models.Sequential()
    model.add(layers.Dense(64, activation='relu', input_shape=(input_shape, )))
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dense(1))
    model.compile(optimizer='adam',
                  loss='mse',
                  metrics=[RootMeanSquaredError()])

    return model
Exemple #4
0
 def build(self, print_model=False):
     self.model = Sequential()
     for layer_config in self.model_config['layers']:
         if layer_config['type'] == 'lstm':
             layer_config['days_for_predict'] = self.days_for_predict
             layer_config['feature_num'] = self.feature_num
             break
     for layer_config in self.model_config['layers']:
         self.model.add(layer_dict[layer_config['type']](layer_config))
     if print_model:
         print(self.model.summary())
     self.model.compile(
         loss=keras.losses.MeanSquaredError(),
         optimizer=Adam(learning_rate=0.01),
         metrics=[MeanAbsolutePercentageError(), RootMeanSquaredError()]
     )
Exemple #5
0
def train_given_optimiser(optimiser):
    model = Sequential()
    model.add(Dense(2, input_dim=1))
    model.add(Activation(activation='relu'))

    model.compile(optimizer=optimiser,
                  loss='msle',
                  metrics=['mse', 'mape',
                           RootMeanSquaredError(), 'msle'])

    score = model.evaluate(X_train, y_train, verbose=2)

    print("Optimiser: ", optimiser)
    print("Before Training:", list(zip(model.metrics_names, score)))
    model.fit(X_train, y_train, epochs=10, batch_size=32, verbose=0)
    score = model.evaluate(X_test, y_test, verbose=0)
    print("After Training:", list(zip(model.metrics_names, score)))
    print(score)
    print(model.metrics_names)
    print(" \n ")
Exemple #6
0
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
print(hello)

### Simple neural network: # Model 1: comments as input and views as output.

# Specify the modelmodel = Sequential()
model_1_neuron = Sequential()
model_1_neuron.add(Dense(1, input_dim=1, activation="relu"))
model_1_neuron.compile(
    loss='mse',
    optimizer='adam',
    #metrics = ["mse", "RMSE", "mape"])
    metrics=[
        MeanSquaredError(),
        RootMeanSquaredError(),
        MeanAbsoluteError(),
        MeanAbsolutePercentageError(),
        MeanSquaredLogarithmicError()
    ])

# Fit the model, or in other words, train the model.

#Train the model and make predictions
history = model_1_neuron.fit(X_train,
                             y_train,
                             epochs=100,
                             batch_size=32,
                             verbose=0,
                             validation_split=0.2)
score = model_1_neuron.evaluate(X_test, y_test, verbose=0)
Exemple #7
0
#       'Station43', 'Station44', 'Station46', 'Station48', 'Station6',
#       'Station69', 'Station74', 'Station78', 'Station79', 'Station80',
#       'Station81', 'Station82', 'Station83', 'Station84', 'Station85',
#       'Station86', 'Station87', 'Station88']
n_features = X.shape[2]
n_train = 9500

# define model
model = Sequential()
model.add(
    LSTM(100, return_sequences=True, input_shape=(n_input_steps, n_features)))
model.add(LSTM(100, return_sequences=True))
model.add(LSTM(100))
model.add(Dense(50, activation='relu'))
model.add(Dense(n_output_steps))
model.compile(optimizer='adam', loss='mse', metrics=[RootMeanSquaredError()])
print("model defined")
# fit model
model.fit(X, y, epochs=3, verbose=1, batch_size=32)
print("model fit")
# demonstrate prediction
x_input = X[9500 + 1, :, :]
x_input = x_input.reshape((1, x_input.shape[0], x_input.shape[1]))
yhat = model.predict(x_input, verbose=1)
print("predicted")
y_real = y[9500 + 1, :]
yhat = scalerC.inverse_transform(yhat)
yreal = scalerC.inverse_transform(y_real)
model.save('Models_save/LSTM_Vanilla_station82_Meteo')

testScore = math.sqrt(mean_squared_error(y_real[:], yhat[0, :]))