In [1]:
from bokeh.models import Legend, ColumnDataSource
from bokeh.plotting import figure, show, output_notebook

from bokeh.resources import INLINE
output_notebook(resources=INLINE)

import datetime
Loading BokehJS ...
In [2]:
import pandas as pd
#df_minute_gap = pd.read_csv("../data/global_active_power_minute_v2.csv", na_filter=True, index_col=(["Year", "Month", "Week", "Day", "Hour", "Minute"]), parse_dates=True)
#df_minute_gap = pd.read_csv("../data/global_active_power_minute_v3.csv", na_filter=True, index_col=(["Date_Time"]), parse_dates=True)
#df_minute_gap.head()
#df_minute_gap.tail()


#df_hour_gap = pd.read_csv("../data/global_active_power_hour_v2.csv", na_filter=True, index_col="Date_Time", parse_dates=True)
df_hour_gap = pd.read_csv("../data/global_active_power_v4.csv", na_filter=True, index_col="Date_Time", parse_dates=True)

df_hour_gap.head(5)
#df_hour_gap.tail()
Out[2]:
Global_active_power hour month day week dayofweek dayofyear is_weekend weekday_name year Date DateTime is_holiday
Date_Time
2006-12-16 17:00:00 4.222889 17 12 16 50 5 350 1 Saturday 2006 2006-12-16 2006-12-16 17:00:00 0
2006-12-16 18:00:00 3.632200 18 12 16 50 5 350 1 Saturday 2006 2006-12-16 2006-12-16 18:00:00 0
2006-12-16 19:00:00 3.400233 19 12 16 50 5 350 1 Saturday 2006 2006-12-16 2006-12-16 19:00:00 0
2006-12-16 20:00:00 3.268567 20 12 16 50 5 350 1 Saturday 2006 2006-12-16 2006-12-16 20:00:00 0
2006-12-16 21:00:00 3.056467 21 12 16 50 5 350 1 Saturday 2006 2006-12-16 2006-12-16 21:00:00 0
In [3]:
import numpy as np
#time_start = '2006-12-16'
time_start = '2006-12-16T17H00m00s'
#time_end = '2006-12-19T16H00m00s'
#time_end = '2007-12-19T16H00m00s'
time_end = '2010-12-31T23H23m59s'
#time_end = '2010-12-31'

df = df_hour_gap.loc[time_start:time_end]
print(df.shape)

#print(df.loc[time_start:time_end,"Global_active_power"].values.shape)
#print(df)
(34589, 13)
In [4]:
from sklearn.preprocessing import MinMaxScaler

def preprocess_normalize_data(data_frame, columns, row_length=24, overlap=0, normalize=False):

    rows_float = (len(data_frame) * ((row_length + overlap)/row_length))/row_length - 1
    #print("Rows_float",rows_float)
    rows = int(rows_float)
    features = len(columns)

    X = np.zeros((rows, row_length, features))
    y = np.zeros((rows, row_length, features))

    scalers = {}
#    print(values.shape)

    for i in range(0, features):
        v = df[columns[i]].values.astype(float)
        if normalize:
            scaler = MinMaxScaler(feature_range=(0, 1))
            v = scaler.fit_transform(v.reshape(-1,1))
            v = v.reshape(v.shape[0])
#            print(v.reshape(v.shape[0]).shape)
#            print(v.shape)
            scalers[columns[i]]=scaler
            
        for j in range(0, X.shape[0]):
#            print(df[columns[i]].values[j*(row_length-overlap):j*(row_length-overlap)+row_length])
#           values[j,:,i] = df[columns[i]].values[j*(row_length-overlap):j*(row_length-overlap)+row_length]
            X[j,:,i] = v[j*(row_length-overlap):j*(row_length-overlap)+row_length]
            y[j,:,i] = v[j*(row_length-overlap)+row_length:j*(row_length-overlap)+2*row_length]                
       
#X = np.delete(X, -1, axis =0)
#y = np.delete(y, 0, axis=0)

        
#    print(values_gap[0:4,:])
#    print(values[-1-4:-1,:])
    return X, y, scalers


row_length = 60
overlap = 0
#columns = ["Global_active_power"]
#columns = ["Global_active_power", "hour"]
#columns = ["Global_active_power", "hour", "month", "day", "week", "dayofweek", "dayofyear", "is_weekend", "year"]
#columns = ["Global_active_power", "hour", "month", "day", "week", "dayofweek", "dayofyear", "is_weekend", "is_holiday", "year"]
columns = ["Global_active_power", "day", "dayofweek", "hour"]

X, y, scalers = preprocess_normalize_data(df, columns, row_length=row_length, overlap=overlap, normalize=True)
#X, y, scalers = preprocess_normalize_data(df, columns, row_length=row_length, overlap=overlap, normalize=False)
#df_values, scalers = preprocess_normalize_data(df, columns, row_length=row_length, overlap=overlap, normalize=False)

print(X.shape)
print(y.shape)
#feature_nr = 0
#print(X[:,:,feature_nr])
#print(y[:,:,feature_nr])
#print(scalers[columns[feature_nr]].inverse_transform(X[0:3,:,feature_nr]))
#print(scalers[columns[feature_nr]].inverse_transform(y[0:3,:,feature_nr]))
(575, 60, 4)
(575, 60, 4)
In [5]:
#df_values = X
#df_values.shape
#df_values.tofile("../data/df_values.txt.gz", sep=";", format="%s")

#t = np.fromfile("../data/df_values.txt.gz", sep=";")
#t.shape
#t.reshape(1441,24,7)
In [6]:
#train_X = np.delete(df_values, -1, axis=0)
#train_y = np.delete(df_values, 0, axis=0)
# do not include year (last column) in training
#train_X = X[:,:,0:-2]
#train_y = y[:,:,0:-2]

# article - first configuration
train_X = np.concatenate((X[:,:,:1], y[:,:,1:]), axis=2)
train_y = y[:,:,:1]

print(train_X.shape)
print(train_y.shape)

#print(train_X[0:2, :, :])
#print(train_X[0:2, :, 0])
#print(train_y[0,:,:])
(575, 60, 4)
(575, 60, 1)
In [7]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(train_X[0,:,0], label = "gap X")
#plt.plot(train_X_gap_cumsum[0,:], label="cumsum X")

plt.plot(train_y[0,:,0], label = "gap y")
#plt.plot(train_y_gap_cumsum[0,:], label="cumsum y")

#plt.plot(train_X[0,:,0], label = "gap X")
#plt.plot(train_X[0,:,1], label = "cumsum X")

#plt.plot(train_y[0,:,0], label = "gap y")
#plt.plot(train_y[0,:,1], label = "cumsum y")

plt.legend()
plt.show()
In [8]:
import keras
from keras.layers import Input, Embedding, LSTM, Dense, Flatten, Dropout, Conv1D, MaxPooling1D
from keras.layers.advanced_activations import LeakyReLU, PReLU
from keras.models import Model
import numpy as np
from keras import backend as K
K.clear_session()

timesteps = row_length #timesteps
features = train_X.shape[2] # len(columns) #features
y_features = train_y.shape[2] # features # 1...

### reshape input to be 3D [samples, timesteps, features]
##(batch_size, timesteps, input_dim)
##  [samples, timesteps, features]

############## INPUTS MAIN #############################################################################################

# for LSTM Inputs
#main_input = Input(shape=(prediction_steps, input_dim), dtype='float32', name='main_input')
main_input = Input(shape=(timesteps,features), batch_shape=(1,timesteps,features), dtype='float32', name='main_input')

# for Conv1D Inputs (sequence_length, features)
#main_input = Input(shape=(timesteps,features), batch_shape=(None,timesteps,features), dtype='float32', name='main_input')

############## INPUTS TIME ##############


############## HIDDEN #############################################################################################

filters = 4
kernel_size = 10

#keras.layers.LSTM(units, activation='tanh', recurrent_activation='hard_sigmoid', use_bias=True, kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', unit_forget_bias=True, kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, recurrent_constraint=None, bias_constraint=None, dropout=0.0, recurrent_dropout=0.0, implementation=1, return_sequences=False, return_state=False, go_backwards=False, stateful=False, unroll=False)
#x = LSTM(30)(main_input)

#keras.layers.Conv1D(filters, kernel_size, strides=1, padding='valid', dilation_rate=1, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)
#x = Conv1D(filters, kernel_size, strides=1, padding='same', dilation_rate=1, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)(main_input)
#x = Conv1D(2, 1)(main_input)
#x = Flatten()(x)

#x = Dense(20)(x)
#x = MaxPooling1D(pool_size = 2, padding='same')(x)

x = LSTM(20, return_sequences=True)(main_input)
#x, states_h, states_c = LSTM(24, return_sequences=True, return_state=True)(main_input)
#x = LSTM(10, input_shape=(24, 2), stateful=True, return_sequences=True, return_state=True)(main_input)
x = LSTM(20, return_sequences=True)(x)

#x = Dense(20)(x)
#x = Dense(30)(x)
#x = Dense(30, activation='linear')(x)
#x = Dropout(0.3)(x)
#x = Dense(30, activation='sigmoid')(x)

#x = LeakyReLU(alpha=.01)(x)
#x = Dense(50, activation='relu', kernel_initializer='random_uniform', bias_initializer='zeros')(x)


############## HIDDEN TIME ##############

############## CONCATENATE ##############

#x = keras.layers.concatenate([lstm_output, input_hour, input_day, input_month, input_dayofweek, input_dayofyear, input_is_weekend])
#time_layers = keras.layers.concatenate([input_hour, input_day, input_month, input_dayofweek, input_dayofyear, input_is_weekend])
#time_layers = keras.layers.concatenate([ihour, iday, imonth, idoweek, idoyear, iisweeknd])
#time_layers = keras.layers.concatenate([ihour, iday, imonth, idoweek, iisweeknd])
#time_layers = keras.layers.concatenate([ihour, iday, imonth, idoweek, iisweeknd])

############## HIDDEN ###################


#x = Dense(20, activation='relu')(lstm_output)
#x = Dense(20, activation='relu')(x)
#x = Dropout(0.3)(x)

#x = Conv1D(filters, kernel_size, strides=1, padding='same', dilation_rate=1, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)(x)

#x = Dense(20, activation='relu')(x)


main_output = LSTM(y_features, return_sequences=True, name='main_output')(x)
#main_output = LSTM(1, return_sequences=True, name='main_output')(x)

# define the input ant outputs of the model

#model = Model(inputs=[main_input, input_hour, input_day, input_month, input_dayofweek, input_dayofyear, input_is_weekend], outputs=[main_output, lstm_output])
#model = Model(inputs=[main_input, input_hour, input_day, input_month, input_dayofweek, input_dayofyear, input_is_weekend], outputs=[main_output, conv_output])
#model = Model(inputs=[main_input, decoder_input], outputs=[main_output])

model = Model(inputs=[main_input], outputs=[main_output])
model.summary()
/Users/iszilagy/miniconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
/Users/iszilagy/miniconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
main_input (InputLayer)      (1, 60, 4)                0         
_________________________________________________________________
lstm_1 (LSTM)                (1, 60, 20)               2000      
_________________________________________________________________
lstm_2 (LSTM)                (1, 60, 20)               3280      
_________________________________________________________________
main_output (LSTM)           (1, 60, 1)                88        
=================================================================
Total params: 5,368
Trainable params: 5,368
Non-trainable params: 0
_________________________________________________________________
In [9]:
from keras.optimizers import Adam

#optimizer = keras.optimizers.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False)
#optimizer = keras.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
#optimizer = keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
#optimizer = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
#optimizer = Adam(lr=1, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0001)
optimizer = Adam(lr=0.001)
#optimizer = Adam(lr=0.01)

#optimizer = 'adam'
In [10]:
#compile the model
model.compile(optimizer=optimizer,
#              metrics=['mse', 'mae', 'mape', 'cosine'],
#              loss={'main_output': 'mean_squared_error'},
              loss={'main_output': 'mean_squared_error'},
#              loss={'main_output': 'mean_squared_error', 'lstm_output': 'mean_squared_error'},
#              loss_weights={'main_output': 0.5, 'lstm_output': .5})
#              loss_weights={'main_output': 1.}
             )
In [12]:
history = model.fit(
    {'main_input': train_X}, {'main_output': train_y},
    validation_split=0.25, epochs=100, batch_size=1, verbose=1, shuffle=False)
#    validation_split=0.2, epochs=10, batch_size=10, verbose=1, shuffle=False)
Train on 431 samples, validate on 144 samples
Epoch 1/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0128 - val_loss: 0.0112
Epoch 2/100
431/431 [==============================] - 50s 116ms/step - loss: 0.0129 - val_loss: 0.0112
Epoch 3/100
431/431 [==============================] - 49s 114ms/step - loss: 0.0129 - val_loss: 0.0112
Epoch 4/100
431/431 [==============================] - 49s 115ms/step - loss: 0.0128 - val_loss: 0.0112
Epoch 5/100
431/431 [==============================] - 50s 115ms/step - loss: 0.0127 - val_loss: 0.0112
Epoch 6/100
431/431 [==============================] - 49s 115ms/step - loss: 0.0127 - val_loss: 0.0113
Epoch 7/100
431/431 [==============================] - 49s 114ms/step - loss: 0.0127 - val_loss: 0.0113
Epoch 8/100
431/431 [==============================] - 49s 114ms/step - loss: 0.0127 - val_loss: 0.0113
Epoch 9/100
431/431 [==============================] - 49s 115ms/step - loss: 0.0127 - val_loss: 0.0114
Epoch 10/100
431/431 [==============================] - 49s 115ms/step - loss: 0.0127 - val_loss: 0.0112
Epoch 11/100
431/431 [==============================] - 49s 114ms/step - loss: 0.0129 - val_loss: 0.0111
Epoch 12/100
431/431 [==============================] - 50s 115ms/step - loss: 0.0128 - val_loss: 0.0111
Epoch 13/100
431/431 [==============================] - 50s 116ms/step - loss: 0.0127 - val_loss: 0.0109
Epoch 14/100
431/431 [==============================] - 50s 116ms/step - loss: 0.0127 - val_loss: 0.0111
Epoch 15/100
431/431 [==============================] - 50s 117ms/step - loss: 0.0125 - val_loss: 0.0111
Epoch 16/100
431/431 [==============================] - 50s 116ms/step - loss: 0.0125 - val_loss: 0.0113
Epoch 17/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0126 - val_loss: 0.0111
Epoch 18/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0125 - val_loss: 0.0111
Epoch 19/100
431/431 [==============================] - 52s 122ms/step - loss: 0.0124 - val_loss: 0.0111
Epoch 20/100
431/431 [==============================] - 52s 121ms/step - loss: 0.0125 - val_loss: 0.0110
Epoch 21/100
431/431 [==============================] - 50s 117ms/step - loss: 0.0127 - val_loss: 0.0110
Epoch 22/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0126 - val_loss: 0.0110
Epoch 23/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0125 - val_loss: 0.0112
Epoch 24/100
431/431 [==============================] - 51s 117ms/step - loss: 0.0126 - val_loss: 0.0110
Epoch 25/100
431/431 [==============================] - 51s 117ms/step - loss: 0.0126 - val_loss: 0.0111
Epoch 26/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0123 - val_loss: 0.0111
Epoch 27/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0123 - val_loss: 0.0111
Epoch 28/100
431/431 [==============================] - 52s 120ms/step - loss: 0.0124 - val_loss: 0.0112
Epoch 29/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0124 - val_loss: 0.0112
Epoch 30/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0124 - val_loss: 0.0113
Epoch 31/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0124 - val_loss: 0.0111
Epoch 32/100
431/431 [==============================] - 52s 121ms/step - loss: 0.0123 - val_loss: 0.0112
Epoch 33/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0122 - val_loss: 0.0112
Epoch 34/100
431/431 [==============================] - 50s 115ms/step - loss: 0.0125 - val_loss: 0.0112
Epoch 35/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0123 - val_loss: 0.0112
Epoch 36/100
431/431 [==============================] - 51s 117ms/step - loss: 0.0123 - val_loss: 0.0112
Epoch 37/100
431/431 [==============================] - 50s 116ms/step - loss: 0.0124 - val_loss: 0.0113
Epoch 38/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0123 - val_loss: 0.0112
Epoch 39/100
431/431 [==============================] - 50s 117ms/step - loss: 0.0121 - val_loss: 0.0112
Epoch 40/100
431/431 [==============================] - 51s 117ms/step - loss: 0.0121 - val_loss: 0.0113
Epoch 41/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0123 - val_loss: 0.0112
Epoch 42/100
431/431 [==============================] - 52s 122ms/step - loss: 0.0125 - val_loss: 0.0113
Epoch 43/100
431/431 [==============================] - 53s 124ms/step - loss: 0.0121 - val_loss: 0.0113
Epoch 44/100
431/431 [==============================] - 52s 120ms/step - loss: 0.0121 - val_loss: 0.0114
Epoch 45/100
431/431 [==============================] - 52s 120ms/step - loss: 0.0124 - val_loss: 0.0113
Epoch 46/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0120 - val_loss: 0.0112
Epoch 47/100
431/431 [==============================] - 52s 122ms/step - loss: 0.0123 - val_loss: 0.0113
Epoch 48/100
431/431 [==============================] - 52s 121ms/step - loss: 0.0121 - val_loss: 0.0113
Epoch 49/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0120 - val_loss: 0.0114
Epoch 50/100
431/431 [==============================] - 50s 116ms/step - loss: 0.0119 - val_loss: 0.0113
Epoch 51/100
431/431 [==============================] - 52s 120ms/step - loss: 0.0119 - val_loss: 0.0113
Epoch 52/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0121 - val_loss: 0.0113
Epoch 53/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0121 - val_loss: 0.0112
Epoch 54/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0119 - val_loss: 0.0113
Epoch 55/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0124 - val_loss: 0.0112
Epoch 56/100
431/431 [==============================] - 52s 121ms/step - loss: 0.0128 - val_loss: 0.0112
Epoch 57/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0125 - val_loss: 0.0114
Epoch 58/100
431/431 [==============================] - 50s 115ms/step - loss: 0.0121 - val_loss: 0.0113
Epoch 59/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0119 - val_loss: 0.0114
Epoch 60/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0118 - val_loss: 0.0113
Epoch 61/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0119 - val_loss: 0.0113
Epoch 62/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0118 - val_loss: 0.0115
Epoch 63/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0119 - val_loss: 0.0113
Epoch 64/100
431/431 [==============================] - 52s 120ms/step - loss: 0.0119 - val_loss: 0.0114
Epoch 65/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0117 - val_loss: 0.0115
Epoch 66/100
431/431 [==============================] - 51s 117ms/step - loss: 0.0119 - val_loss: 0.0115
Epoch 67/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0117 - val_loss: 0.0113
Epoch 68/100
431/431 [==============================] - 50s 117ms/step - loss: 0.0117 - val_loss: 0.0112
Epoch 69/100
431/431 [==============================] - 50s 115ms/step - loss: 0.0119 - val_loss: 0.0116
Epoch 70/100
431/431 [==============================] - 50s 115ms/step - loss: 0.0116 - val_loss: 0.0116
Epoch 71/100
431/431 [==============================] - 50s 117ms/step - loss: 0.0114 - val_loss: 0.0116
Epoch 72/100
431/431 [==============================] - 50s 117ms/step - loss: 0.0116 - val_loss: 0.0115
Epoch 73/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0117 - val_loss: 0.0116
Epoch 74/100
431/431 [==============================] - 52s 122ms/step - loss: 0.0116 - val_loss: 0.0116
Epoch 75/100
431/431 [==============================] - 48s 112ms/step - loss: 0.0115 - val_loss: 0.0117
Epoch 76/100
431/431 [==============================] - 50s 115ms/step - loss: 0.0117 - val_loss: 0.0116
Epoch 77/100
431/431 [==============================] - 53s 122ms/step - loss: 0.0118 - val_loss: 0.0116
Epoch 78/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0115 - val_loss: 0.0116
Epoch 79/100
431/431 [==============================] - 50s 115ms/step - loss: 0.0114 - val_loss: 0.0115
Epoch 80/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0115 - val_loss: 0.0118
Epoch 81/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0113 - val_loss: 0.0117
Epoch 82/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0118 - val_loss: 0.0115
Epoch 83/100
431/431 [==============================] - 49s 114ms/step - loss: 0.0116 - val_loss: 0.0116
Epoch 84/100
431/431 [==============================] - 49s 113ms/step - loss: 0.0114 - val_loss: 0.0116
Epoch 85/100
431/431 [==============================] - 50s 117ms/step - loss: 0.0114 - val_loss: 0.0117
Epoch 86/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0115 - val_loss: 0.0117
Epoch 87/100
431/431 [==============================] - 50s 116ms/step - loss: 0.0115 - val_loss: 0.0114
Epoch 88/100
431/431 [==============================] - 49s 113ms/step - loss: 0.0115 - val_loss: 0.0115
Epoch 89/100
431/431 [==============================] - 48s 112ms/step - loss: 0.0114 - val_loss: 0.0116
Epoch 90/100
431/431 [==============================] - 51s 117ms/step - loss: 0.0112 - val_loss: 0.0115
Epoch 91/100
431/431 [==============================] - 50s 116ms/step - loss: 0.0113 - val_loss: 0.0116
Epoch 92/100
431/431 [==============================] - 51s 118ms/step - loss: 0.0117 - val_loss: 0.0117
Epoch 93/100
431/431 [==============================] - 49s 114ms/step - loss: 0.0118 - val_loss: 0.0113
Epoch 94/100
431/431 [==============================] - 51s 119ms/step - loss: 0.0117 - val_loss: 0.0114
Epoch 95/100
431/431 [==============================] - 50s 116ms/step - loss: 0.0112 - val_loss: 0.0115
Epoch 96/100
431/431 [==============================] - 50s 116ms/step - loss: 0.0115 - val_loss: 0.0116
Epoch 97/100
431/431 [==============================] - 49s 114ms/step - loss: 0.0113 - val_loss: 0.0116
Epoch 98/100
431/431 [==============================] - 51s 117ms/step - loss: 0.0114 - val_loss: 0.0116
Epoch 99/100
431/431 [==============================] - 52s 120ms/step - loss: 0.0111 - val_loss: 0.0116
Epoch 100/100
431/431 [==============================] - 50s 116ms/step - loss: 0.0112 - val_loss: 0.0117
In [23]:
from keras.models import load_model
model.save('../models/EnergyPredictionDLNN_v5_100epochs_v3_article_lstm_standard.h5')
#model = load_model('../models/EnergyPredictionDLNN_v5_50epochs_v1_rmse_0.0057.h5')
In [13]:
import matplotlib.pyplot as plt
%matplotlib inline

#print(history.history['loss'])
plt.plot(history.history['loss'], "o-", label='loss')
plt.plot(history.history['val_loss'], "o-", label='val_loss')

#plt.plot(history.history['main_output_loss'], label='main_output_loss')
#plt.plot(history.history['val_main_output_loss'], label='val_main_output_loss')

#plt.plot(history.history['lstm_output_loss'], label='lstm_output_loss')
#plt.plot(history.history['val_lstm_output_loss'], label='val_lstm_output_loss')

plt.ylabel('loss')
plt.xlabel('epoch')

plt.legend()
#plt.savefig('../figures/loss_output_main_lstml.png')
plt.show()
In [14]:
#row = 1166 # When full : Trained on 1152 rows from a total of 1440 rows
row = 450 # When full : Trained on 1152 rows from a total of 1440 rows
#row = 1 # When full : Trained on 1152 rows from a total of 1440 rows

predicted_main_out = model.predict({'main_input': train_X[row,:,:].reshape(1, train_X.shape[1],train_X.shape[2])})
print(predicted_main_out.shape)
(1, 60, 1)
In [15]:
import matplotlib.pyplot as plt
%matplotlib inline


#plt.plot(train_X[row,:,0], label = "gap X")
#plt.plot(train_X_gap_cumsum[0,:], label="cumsum X")

plt.plot(train_y[row,:,0], label = "real gap")
#plt.plot(train_y[0,:,0], label = "real gap")
plt.plot(predicted_main_out[0,:,0].reshape(-1), label="predicted gap")

#plt.plot(train_X[0,:,0], label = "gap X")
#plt.plot(train_X[0,:,1], label = "cumsum X")

#plt.plot(train_y[0,:,0], label = "gap y")
#plt.plot(train_y[0,:,1], label = "cumsum y")


plt.legend()
plt.show()
In [16]:
from math import sqrt
from sklearn.metrics import mean_squared_error


#print(test_data.shape)
#print(test_additional_data.shape)

#print(out_main.shape)
#print(out_aux.shape)
#print(test_labels.shape)

#rmse = sqrt(mean_squared_error(np.nan_to_num(predicted_out, 0),test_y))
#rmse = sqrt(mean_squared_error(predicted_main_out[:,:,0],test_y[:,:,0])) #.reshape((test_y.shape[0], test_y.shape[1]))))
rmse = sqrt(mean_squared_error(predicted_main_out[:,:,0].reshape(-1), train_y[row,:,0].reshape(-1))) #.reshape((test_y.shape[0], test_y.shape[1]))))
#rmse_cum = sqrt(mean_squared_error(predicted_main_out_gap_cumsum,test_y[:,:,1].reshape((test_y.shape[0], test_y.shape[1]))))
#rmse2 = sqrt(mean_squared_error(predicted_lstm_out,test_y))
##rmse_main = sqrt(mean_squared_error(out_main, test_labels))

rmse2 = sqrt(mean_squared_error(scalers[columns[0]].inverse_transform(predicted_main_out[0,:,0].reshape(-1,1)), scalers[columns[0]].inverse_transform(train_y[0,:,0].reshape(-1,1)))) #.reshape((test_y.shape[0], test_y.shape[1]))))


print('Scaled RMSE main: %.3f' % rmse)
print('Real RMSE : %.3f' % rmse2)
Scaled RMSE main: 0.107
Real RMSE : 0.827
In [17]:
import matplotlib.pyplot as plt
%matplotlib inline

plot_start = 0
plot_end = -1

fig1, ax11 = plt.subplots(figsize=(20, 10), dpi=100)

ax11.plot(scalers[columns[0]].inverse_transform(train_y[0,:,0].reshape(-1,1)), 'r-', label='real data gap')
ax11.plot(scalers[columns[0]].inverse_transform(predicted_main_out[0,:,0].reshape(-1,1)), 'b-', label='predicted gap')

ax11.legend()

ax11.set_xlabel('Hours')
ax11.set_ylabel('Global_active_power', color='r')
Out[17]:
Text(0,0.5,'Global_active_power')
In [56]:
from math import sqrt
from sklearn.metrics import mean_squared_error

def getRMSE(row_start=0, row_end=-1):
    rmse1_all = []
    rmse2_all = []
    
    if row_end == -1:
        row_end = train_X.shape[0]

    predictions = np.empty((0, row_length))
    real_values = np.empty((0, row_length))
        
    for i in range(row_start, row_end):
        predicted_main_out = model.predict({'main_input': train_X[i,:,:].reshape(1, train_X.shape[1],train_X.shape[2])})
        
        predictions = np.append(predictions, scalers[columns[0]].inverse_transform(predicted_main_out[0,:,0].reshape(1,-1)), axis=0)
        real_values = np.append(real_values, scalers[columns[0]].inverse_transform(train_y[i,:,0].reshape(1,-1)), axis=0)
#        print(predictions.shape)
#        print(scalers[columns[0]].inverse_transform(predicted_main_out[0,:,0].reshape(1,-1)).shape)
#        print(predicted_main_out)
#        print(train_y[i,:,0])
        
        rmse1_all.append(sqrt(mean_squared_error(predicted_main_out[0,:,0].reshape(-1), train_y[i,:,0].reshape(-1)))) #.reshape((test_y.shape[0], test_y.shape[1]))))
        rmse2_all.append(sqrt(mean_squared_error(scalers[columns[0]].inverse_transform(predicted_main_out[:,:,0].reshape(-1,1)), scalers[columns[0]].inverse_transform(train_y[i,:,0].reshape(-1,1))))) #.reshape((test_y.shape[0], test_y.shape[1]))))
    print(predictions.shape)
    print(real_values.shape)

#    rmse_real = sqrt(mean_squared_error(predictions.reshape(-1), real_values.reshape(-1))) #.reshape((test_y.shape[0], test_y.shape[1]))))
    rmse_real = sqrt(mean_squared_error(predictions.reshape(-1,1), real_values.reshape(-1,1))) #.reshape((test_y.shape[0], test_y.shape[1]))))       
    
    return rmse1_all, rmse2_all, rmse_real, predictions, real_values

row_start = 1081
row_start = 431
#row_start = 0

#rmse1_all, rmse2_all, rmse_real = getRMSE(train_X, train_y, row_start=1153)
#rmse1_all, rmse2_all, rmse_real, predictions, real_values = getRMSE(row_start=1153, row_end=-1)
rmse1_all, rmse2_all, rmse_real, predictions, real_values = getRMSE(row_start=row_start, row_end=-1)
#rmse1_all, rmse2_all, rmse_real, predictions, real_values = getRMSE(row_start=0, row_end=2)
#rmse1_all, rmse2_all, rmse_real, predictions, real_values = getRMSE()


#print(rmse1_all)
#print(rmse2_all)
print('RMSE Real on all predictions : %.3f' % rmse_real)

print('Scaled RMSE1 ALL : %.3f' % np.mean(rmse1_all))

print('Mean RMSE2 ALL: %.3f' % np.mean(rmse2_all))
print('Max RMSE2 ALL: %.3f' % np.max(rmse2_all))
print('Min RMSE2 ALL: %.3f' % np.min(rmse2_all))
print('Standard deviation: %.3f' % np.std(rmse2_all))

print(real_values.shape)
print(predictions.shape)
(144, 60)
(144, 60)
RMSE Real on all predictions : 0.696
Scaled RMSE1 ALL : 0.104
Mean RMSE2 ALL: 0.671
Max RMSE2 ALL: 1.309
Min RMSE2 ALL: 0.322
Standard deviation: 0.184
(144, 60)
(144, 60)
In [63]:
best_prediction_index = np.argmin(rmse2_all)
worst_prediction_index = np.argmax(rmse2_all)

print('Index of Max RMSE : %d' % worst_prediction_index)
print('Index of Min RMSE : %d' % best_prediction_index)

best_real = real_values[best_prediction_index, :].reshape(-1, order='C')
best_predicted = predictions[best_prediction_index, :].reshape(-1, order='C')

plt.figure(figsize=(24,6))
plt.title("Best RMSE:  %.3f" % sqrt(mean_squared_error(best_real, best_predicted)))
plt.plot(best_real, ".-",label = "real gap")
plt.plot(best_predicted,"+-", label="predicted gap")

plt.legend()
plt.show()

worst_real = real_values[worst_prediction_index, :].reshape(-1, order='C')
worst_predicted = predictions[worst_prediction_index, :].reshape(-1, order='C')

plt.figure(figsize=(24,6))
plt.title("Worst RMSE:  %.3f" % sqrt(mean_squared_error(worst_real,worst_predicted)))
plt.plot(worst_real, ".-",label = "real gap")
plt.plot(worst_predicted,"+-", label="predicted gap")

plt.legend()
plt.show()
Index of Max RMSE : 9
Index of Min RMSE : 101
In [58]:
real_values.shape
#train_y.shape
Out[58]:
(144, 60)

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.std.html

The standard deviation is the square root of the average of the squared deviations from the mean, i.e., std = sqrt(mean(abs(x - x.mean())**2)).

In [59]:
import matplotlib.pyplot as plt
%matplotlib inline

#plt.plot(train_X[row,:,0], label = "gap X")
#plt.plot(train_X_gap_cumsum[0,:], label="cumsum X")

timewindows_per_figure = 2
figures = predictions.shape[1]/timewindows_per_figure
#figures = 5# predictions.shape[1]/timewindows_per_figure

for i in range(0, int(figures)):
    real = np.empty((0,row_length))
    predict = np.empty((0,row_length))

    real = np.append(real,real_values[i*timewindows_per_figure:i*timewindows_per_figure+timewindows_per_figure, :].reshape(-1, order='C'))
    predict = np.append(predict,predictions[i*timewindows_per_figure:i*timewindows_per_figure+timewindows_per_figure, :].reshape(-1, order='C'))

#    hour = scalers[columns[1]].inverse_transform(train_y[row_start + i*timewindows_per_figure:row_start + i*timewindows_per_figure+timewindows_per_figure, :, 1]).reshape(-1, order='C')
#    month = scalers[columns[2]].inverse_transform(train_y[row_start + i*timewindows_per_figure:row_start + i*timewindows_per_figure+timewindows_per_figure, :, 2]).reshape(-1, order='C')
#    day = scalers[columns[3]].inverse_transform(train_y[row_start + i*timewindows_per_figure:row_start + i*timewindows_per_figure+timewindows_per_figure, :, 3]).reshape(-1, order='C')
#    year = scalers[columns[8]].inverse_transform(y[row_start + i*timewindows_per_figure:row_start + i*timewindows_per_figure+timewindows_per_figure, :, 8]).reshape(-1, order='C')
#    print(month)

    plt.figure(figsize=(24,6))
    plt.title("Figure  %d - RMSE:  %.3f" % (i+1, sqrt(mean_squared_error(real, predict))))
    plt.plot(real, ".-",label = "real gap")
    plt.plot(predict,"+-", label="predicted gap")
    
#    ax_x = np.core.defchararray.add((year.astype(int)).astype(str), "-")
#    ax_x = np.core.defchararray.add(ax_x, (month.astype(int)).astype(str))
#    ax_x = np.core.defchararray.add(ax_x, "-")
#    ax_x = np.core.defchararray.add(ax_x, (day.astype(int)).astype(str))
#    ax_x = np.core.defchararray.add(ax_x, "T")
#    ax_x = np.core.defchararray.add(ax_x, (hour.astype(int)).astype(str))
#    ax_x = np.core.defchararray.add(ax_x, "h00")
#    plt.xticks(np.arange(len(hour)), ax_x, rotation=45)
#    print(ax_x)
    
    plt.legend()
    plt.show()
    
In [64]:
# Convert this notebook to README file/format/page
!jupyter nbconvert --to markdown EnergyPredictionDLNN_v5.ipynb
!mv EnergyPredictionDLNN_v5.md README_EnergyPredictionDLNN_v5.md
[NbConvertApp] Converting notebook EnergyPredictionDLNN_v5.ipynb to markdown
/Users/iszilagy/miniconda3/lib/python3.6/site-packages/nbconvert/filters/datatypefilter.py:41: UserWarning: Your element with mimetype(s) dict_keys(['application/javascript', 'application/vnd.bokehjs_load.v0+json']) is not able to be represented.
  mimetypes=output.keys())
[NbConvertApp] Support files will be in EnergyPredictionDLNN_v5_files/
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Making directory EnergyPredictionDLNN_v5_files
[NbConvertApp] Writing 44343 bytes to EnergyPredictionDLNN_v5.md
In [61]:
import json
import os.path
import re
import ipykernel
import requests
from notebook import notebookapp

def get_notebook_name():
    """
    Return the full path of the jupyter notebook.
    """
    kernel_id = re.search('kernel-(.*).json',
                          ipykernel.connect.get_connection_file()).group(1)
    servers = notebookapp.list_running_servers()
    for ss in servers:
        if ss['token']:
            token = '?token={token:s}'.format(token=ss['token'])
        else:
            token = ''
        response = requests.get('{url:s}api/sessions{token:s}'
                                ''.format(url=ss['url'], token=token))
        for nn in json.loads(response.text):
            if nn['kernel']['id'] == kernel_id:
                relative_path = nn['notebook']['path']
                return os.path.join(ss['notebook_dir'], relative_path)

# Convert this notebook to README file/format/page
            
get_notebook_name()
Out[61]:
'/Users/iszilagy/Documents/NotebooksJupyterLab/notebooks-srv-mips/EnergyPrediction/EnergyPredictionDLNN_v5.ipynb'