<jupyter_start><jupyter_text>Se crea tres modelos basado en las RNNs convencionales, una LSTM y por último basado en unidades GRU. Se busca hacer comparación del rendimiento de los modelos.<jupyter_code>def model_LSTM(X_train, y_train, X_test, y_test, n_steps):
    
    model = Sequential()
    model.add(LSTM(units=50, input_shape=(X_train.shape[1],4)))
    model.add(Dropout(0.2))
    model.add(Dense(units=1))
    
    model.compile(optimizer='adam',loss='mse', metrics=['mae', 'acc'])
    

    model.fit(X_train.reshape(X_train.shape[0],n_steps,4),y_train,epochs=2,batch_size=32)

    metricsTrain = model.evaluate(X_train, y_train)
    metricsTest = model.evaluate(X_test,y_test)

    
    return metricsTrain, metricsTest, model
def model_GRU(X_train, y_train, X_test, y_test, n_steps):
    
    model = Sequential()
    model.add(SimpleRNN(units=50, input_shape=(X_train.shape[1],4)))
    model.add(Dropout(0.2))
    model.add(Dense(units=1))
    
    model.compile(optimizer='adam',loss='mse', metrics=['mae', 'acc'])
    

    model.fit(X_train.reshape(X_train.shape[0],n_steps,4),y_train,epochs=2,batch_size=32)

    metricsTrain = model.evaluate(X_train, y_train)
    metricsTest = model.evaluate(X_test,y_test)

    
    return metricsTrain, metricsTest, model
def model_RNN(X_train, y_train, X_test, y_test, n_steps):
    
    model = Sequential()
    model.add(SimpleRNN(units=50, input_shape=(X_train.shape[1],4)))
    model.add(Dropout(0.2))
    model.add(Dense(units=1))
    
    model.compile(optimizer='adam',loss='mse', metrics=['mae', 'acc'])
    

    model.fit(X_train.reshape(X_train.shape[0],n_steps,4),y_train,epochs=2,batch_size=32)

    metricsTrain = model.evaluate(X_train, y_train)
    metricsTest = model.evaluate(X_test,y_test)

    
    return metricsTrain, metricsTest, model
def plot_metrics(title, metrics, loop_back):
    x = np.arange(1,loop_back)
    plt.figure(figsize=(15,15))
    plt.suptitle(title, size=16)
    legends = ["Loss","Mean absolute error","Accuracy"]
    plot = 1
    tam =6

    for train,test in metrics:
        plt.subplot(tam, 1, plot)
        plt.plot(x,train, label="Train")
        plt.plot(x, test, 'r', label = "Test")
        plt.title(legends[plot-1]+" train vs test", y = 0.5)
        plt.legend()
        plot+= 1

    plt.show()
def plot_performance(title, models):
    plt.figure(figsize=(15,15))
    plt.suptitle(title, size=16)
    tam = len(models)
    plot = 1
           
    full_dataset = dataset[0:9622,:].copy()
    scaler = MinMaxScaler(feature_range=(0, 1))
    fullN = scaler.fit_transform(full_dataset)
    
           
    for i, model in enumerate(models):
        loop_back = i
        
        #testN = scaler.transform(test)
        full_train, y_train = split_sequence(fullN, loop_back+1)

        full_predicted = model.predict(full_train)
        plt.subplot(tam,1, plot)
        plt.plot(full_predicted, color='blue',label='Predicted', alpha=0.6)
 
        plt.plot(X_train[:,0][:,3], color='violet',label='X_train')
        #plt.plot(fullN[:,0][:,3], color='violet',label='X_train')
        #plt.plot(X_test[:,0][:,3], color='brown', label = 'X_test')
 
        plt.title('Performance using '+str(loop_back+1) + " loop back", y = 0.3)
        plt.xlim(0, 12000)
        #plt.ylim(0, 0.12)
        plt.legend()
      
        plot +=1

    plt.show()
def train_rnn(trainN, testN,loop_back=1):   

    n_features = trainN.shape[1]

    X_train, y_train = split_sequence(trainN, loop_back)
    y_train = y_train[:,3]
    X_test, y_test = split_sequence(testN, loop_back)
    y_test  = y_test[:,3]
    
    print("Xtrain shape: ", X_train.shape)
    print("ytrain: ",y_train.shape)
    print("Steps in: ",loop_back)
    print("RNN")
    metricsTrain, metricsTest,model = model_RNN(X_train, y_train, X_test, y_test, loop_back)
    
    return metricsTrain, metricsTest, model
def train_model(trainN, testN,loop_back, type_model):   

    n_features = trainN.shape[1]

    X_train, y_train = split_sequence(trainN, loop_back)
    y_train = y_train[:,3]
    X_test, y_test = split_sequence(testN, loop_back)
    y_test  = y_test[:,3]
    
    print("Xtrain shape: ", X_train.shape)
    print("ytrain: ",y_train.shape)
    print("Steps in: ",loop_back)
   
    
    if type_model == 1:
        print("RNN")
        metricsTrain, metricsTest,model = model_RNN(X_train, y_train, X_test, y_test, loop_back)
    elif type_model == 2:
        print("LSTM")
        metricsTrain, metricsTest,model = model_LSTM(X_train, y_train, X_test, y_test, loop_back)
    elif type_model == 3:
        print("GRU")
        metricsTrain, metricsTest,model = model_GRU(X_train, y_train, X_test, y_test, loop_back)
        
    
    return metricsTrain, metricsTest, model
def evaluate_loop_back(trainN, testN, type_model):
    lossTest, lossTrain=[], []
    accTest, accTrain = [], []
    mseTest, mseTrain = [], []
    models = []
    for loop_back in np.arange(1,5,1):
        metricsTrain, metricsTest, model = train_model(trainN, testN,loop_back, type_model)
        lossTrain  += [metricsTrain[0]]
        lossTest  += [metricsTest[0]]
        mseTrain  += [metricsTrain[1]]
        mseTest  += [metricsTest[1]]
        accTrain  += [metricsTrain[2]]
        accTest  += [metricsTest[2]]
        models.append(model)
    return [(lossTrain, lossTest), (mseTrain, mseTest), (accTrain, accTest)], models<jupyter_output><empty_output><jupyter_text>Entrenamiento de los modelos RNN, LSTM, GRU<jupyter_code>from DataPreparationRNN import *
from sklearn.preprocessing import MinMaxScaler
look_back=3
#dataset = datasetO.values
dataset = data.astype('float32')
dataset = np.array(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
trainN = scaler.fit_transform(train)
testN = scaler.transform(test)<jupyter_output><empty_output><jupyter_text>Entrenamiento con RNN<jupyter_code>metricsRNN, models = evaluate_loop_back(trainN, testN,1)
plot_metrics("RNN",metricsRNN,5)
plot_performance("RNN",models)<jupyter_output><empty_output><jupyter_text>Entrenamiento con LSTM<jupyter_code>metricsLSTM, modelsLSTM = evaluate_loop_back(trainN, testN,2)
plot_metrics("LSTM",metricsLSTM,5)
plot_performance("LSTM",modelsLSTM)<jupyter_output><empty_output><jupyter_text>Entrenamiento con GRU<jupyter_code>metricsGRU, modelsGRU = evaluate_loop_back(trainN, testN,3)
plot_metrics("GRU",metricsGRU,5)
plot_performance("GRU",modelsGRU)

#plt.plot(X_train[:,0][:,2], color='violet',label='X_test')


#plt.style.use('seaborn-white')

plt.figure(figsize=(18,10))   
#x = np.linspace(0,2*pi, 100)

plt.subplot(5,1, 1)

#predicted_stock_price_12 = regressor12.predict(X_test) 
plt.plot(X_train[:,0][:,0], color='green',label='voltaje_train')
#plt.plot(X_test[:,0][:,0], color='red',label='X_test')
plt.title('Performance using LSTM       times ahead = 12')
plt.xlabel('Pasos')
plt.ylabel('LSTM')
#plt.xlim(0, 20)
#plt.ylim(0, 0.12)
plt.legend()

plt.subplot(5,1, 2)

#predicted_stock_price_12 = regressor12.predict(X_test) 
plt.plot(X_train[:,0][:,1], color='violet',label='corriente_train')
#plt.plot(X_test[:,0][:,0], color='red',label='X_test')
plt.title('Performance using LSTM       times ahead = 12')
plt.xlabel('Pasos')
plt.ylabel('LSTM')
#plt.xlim(0, 20)
#plt.ylim(0, 0.12)
plt.legend()


plt.subplot(5,1, 3)

predicted_stock_price_3 = model.predict(X_test) 
plt.plot(X_train[:,0][:,2], color='green',label='temperatura_train')
#plt.plot(X_test[:,0][:,0], color='red',label='X_test')
plt.title('Performance using LSTM       times ahead = 3')
plt.xlabel('Pasos')
plt.ylabel('LSTM')
#plt.xlim(0, 20)
#plt.ylim(0, 0.12)
plt.legend()

plt.subplot(5,1, 4)

#predicted_stock_price_6 = regressor6.predict(X_test) 
plt.plot(X_train[:,0][:,3], color='red',label='carga_train')
#plt.plot(X_test[:,0][:,0], color='red',label='X_test')
plt.title('Performance using LSTM       times ahead = 6')
plt.xlabel('Pasos')
plt.ylabel('LSTM')
#plt.xlim(0, 20)
#plt.ylim(0, 0.12)
plt.legend()

plt.subplot(5,1, 5)

#predicted_stock_price_12 = regressor12.predict(X_test) 
#plt.plot(X_train[:,0][:,1], color='green',label='Temperatura_train')
plt.plot(predicted_stock_pricernn, color='blue',label='Predicted')
##plt.plot(X_test[:,0][:,0], color='red',label='X_test')
plt.title('Performance using LSTM       times ahead = 12')
plt.xlabel('Pasos')
plt.ylabel('LSTM')
#plt.xlim(0, 20)
#plt.ylim(0, 0.12)
plt.legend()


plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15970201373530438, 'fraction_numerical': 0.02630659993015947, 'mean_word_length': 4.5218508997429305, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '585884', 'license': 'MIT', 'n_tokens_mistral': 3503, 'n_tokens_neox': 3296, 'n_words': 525, 'path': 'Main.ipynb', 'repo_name': 'deiry/Predict_Charge', 'size': '8591'}