<jupyter_start><jupyter_text>Define different the model to test<jupyter_code>def RULmodel_SN_1(input_shape):
    
    l2_lambda_regularization = 0.20
    l1_lambda_regularization = 0.10
    
    #Create a sequential model
    model = Sequential()
    
    #Add the layers for the model
    model.add(Dense(20, input_dim=input_shape, activation='relu', kernel_initializer='glorot_normal', 
                    kernel_regularizer=regularizers.l1_l2(l1=l1_lambda_regularization, l2=l2_lambda_regularization), 
                    name='fc1'))
    model.add(Dense(20, input_dim=input_shape, activation='relu', kernel_initializer='glorot_normal', 
                    kernel_regularizer=regularizers.l1_l2(l1=l1_lambda_regularization, l2=l2_lambda_regularization), 
                    name='fc2'))
    model.add(Dense(1, activation='linear', name='out'))
    
    return model
def get_compiled_model(shape, model_type='ann'):
    
    K.clear_session()  #Clear the previous tensorflow graph
    
    #To test the model without randomness

    
    #Shared parameters for the models
    optimizer = Adam(lr=0.001,beta_1=0.5)
    
    #optimizer = SGD(lr=0.001, momentum=0.0, decay=0.0, nesterov=False)
    
    lossFunction = "mean_squared_error"
    metrics = ["mse"]
    model = None

    #Create and compile the models

    if model_type=='ann':
        model = RULmodel_SN_1(shape)
        model.compile(optimizer = optimizer, loss = lossFunction, metrics = metrics)
    elif model_type=='lstm':
        model = RULmodel_LSTM(shape)
        model.compile(optimizer = optimizer, loss = lossFunction, metrics = metrics)
    else:
        pass

    return model<jupyter_output><empty_output><jupyter_text>Create data Handler<jupyter_code>#Selected as per CNN paper
features = ['T2', 'T24', 'T30', 'T50', 'P2', 'P15', 'P30', 'Nf', 'Nc', 'epr', 'Ps30', 'phi', 'NRf', 'NRc', 
                     'BPR', 'farB', 'htBleed', 'Nf_dmd', 'PCNfR_dmd', 'W31', 'W32']
selected_indices = np.array([2, 3, 4, 7, 8, 9, 11, 12, 13, 14, 15, 17, 20, 21])
selected_features = list(features[i] for i in selected_indices-1)
data_folder = '../CMAPSSData'

window_size = 30
window_stride = 1
max_rul = 125

dHandler_cmaps = CMAPSSDataHandler(data_folder, 1, selected_features, max_rul, window_size, window_stride)
#dHandler_cmaps.load_data(verbose=1, cross_validation_ratio=0.2)<jupyter_output><empty_output><jupyter_text>Run experiments<jupyter_code>#datasets = [1, 2, 3, 4]
datasets = [2]
iterations = 1

window_sizes = {1:24,2:17,3:24,4:17}
strides = {1:1,2:1,3:1,4:1}
max_ruls = {1:129, 2:139, 3:129, 4:139}

scores ={1:[], 2:[], 3:[], 4:[]}
models = {1:RULmodel_SN_1}


#Create tunable model

num_features = len(selected_features)

shape = len(selected_features)*window_size
#file = open("ResultsBestModelAllDatasetsSingleSet.csv", "w")
#csvfile = csv.writer(file, lineterminator='\n')

tempScoresRMSE = []
tempScoresRHS = []
tempTime = []

#Run experiments
for i in datasets:
    
    min_rmse = 1000
    bestModel = None
    bestIndex = 0
    
    print("Working on dataset " + str(i))
        
    tempScoresRMSE = np.zeros((iterations,1))
    tempScoresRHS = np.zeros((iterations,1))
    tempTime = np.zeros((iterations,1))
        
    input_shape = window_sizes[i]*num_features #For simple ANN

    for j in range(iterations):

        #Create keras model
        print("Iteration "+str(j+1))
        #file.write("Iteration "+str(j+1)+'\n\n')

        model = get_compiled_model(input_shape, model_type='ann')
        tModel = SequenceTunableModelRegression('ModelRUL_SN_1', model, lib_type='keras', data_handler=dHandler_cmaps)

        #Load data
        min_max_scaler = MinMaxScaler()
        tModel.data_handler.change_dataset(i)
        tModel.data_handler.sequence_length = window_sizes[i]
        tModel.data_handler.sequence_stride = strides[i]
        tModel.data_handler.max_rul = max_ruls[i]

        tModel.data_handler.data_scaler = None
        tModel.data_scaler = min_max_scaler

        tModel.load_data(unroll=True, verbose=1, cross_validation_ratio=0)
        #tModel.data_handler.print_data()
        #tModel.print_data()

        #Train model
        tModel.epochs = 200

        tModel.train_model(verbose=1)
        tModel.evaluate_model(['rhs', 'rmse'], round=2)

        cScores = tModel.scores
        rmse = math.sqrt(cScores['score_1'])
        rmse2 = cScores['rmse']
        rhs = cScores['rhs']
        time = tModel.train_time

        if rmse2 < min_rmse:
            best_model_weights = tModel.model.get_weights()
            #bestModel.set_weights(tModel.model.get_weights())
            bestIndex = j
            min_rmse = rmse2

        tempScoresRMSE[j] = rmse2
        tempScoresRHS[j] = rhs
        tempTime[j] = time

    print(tempScoresRMSE)
    print('Best Score')
    print(min_rmse)
    
    #save best model to file
    #serialize model to JSON

    #print(best_model_weights)
    model = get_compiled_model(input_shape, model_type='ann')
    bestModel = SequenceTunableModelRegression('ModelRUL_SN_1', model, lib_type='keras', data_handler=dHandler_cmaps).model
    bestModel.set_weights(best_model_weights)
    model_json = bestModel.to_json()
    with open("bestRULModel_dataset_"+str(i)+".json", "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    bestModel.save_weights("bestRULModel_dataset_"+str(i)+".h5")
    print("Saved best model for dataset "+str(i)+" to disk")
# load json and create model
json_file = open('bestRULModel_dataset_2.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("bestRULModel_dataset_2.h5")
print("Loaded model from disk")


#Shared parameters for the models
optimizer = Adam(lr=0, beta_1=0.5)
lossFunction = "mean_squared_error"
metrics = ["mse"]

#Selected as per CNN paper
selected_features = ['T24', 'T30', 'T50', 'P30', 'Nf', 'Nc', 'Ps30', 'phi', 'NRf', 'NRc', 
                     'BPR', 'htBleed', 'W31', 'W32']

#Create and compile the models
#nFeatures = len(selected_features)
#shapeSN = nFeatures*windowSize
#modelRULSN = RULmodel_SN_1(shapeSN)
loaded_model.compile(optimizer = optimizer, loss = lossFunction, metrics = metrics)

#min_max_scaler = MinMaxScaler(feature_range=(-1, 1))
min_max_scaler = MinMaxScaler()

tModel = SequenceTunableModelRegression('ModelRUL_SN_1', loaded_model, lib_type='keras', data_handler=dHandler_cmaps)

i = 2

tModel.data_handler.change_dataset(i)
tModel.data_handler.sequence_length = window_sizes[i]
tModel.data_handler.sequence_stride = strides[i]
tModel.data_handler.max_rul = max_ruls[i]

tModel.data_handler.data_scaler = None
tModel.data_scaler = min_max_scaler

tModel.load_data(unroll=True, verbose=1, cross_validation_ratio=0)
#tModel.print_data()
#print(tModel.model.get_weights())
tModel.get_model_description()


# evaluate loaded model on test data
tModel.evaluate_model(['rhs', 'rmse'], round=2)
print("scores")

#print(tModel.y_pred)

cScores = tModel.scores
print(cScores)
rmse = math.sqrt(cScores['score_1'])
rmse2 = cScores['rmse']
rhs = cScores['rhs']
time = tModel.train_time

print(cScores)
print("RMSE: {}".format(rmse))
print("RMSE2: {}".format(rmse2))
print("RHS: {}".format(rhs))
print("Time : {} seconds".format(time))<jupyter_output>259/259 [==============================] - 0s 512us/step
scores
{'loss': 1221.6244099149374, 'score_1': 1162.0175140263032, 'rhs': 159.51125585952417, 'rmse': 34.037340385825395}
{'loss': 1221.6244099149374, 'score_1': 1162.0175140263032, 'rhs': 159.51125585952417, 'rmse': 34.037340385825395}
RMSE: 34.088377990545446
RMSE2: 34.037340385825395
RHS: 159.51125585952417
Time : None seconds
{'fraction_non_alphanumeric': 0.13312613312613314, 'fraction_numerical': 0.052059052059052056, 'mean_word_length': 4.172806430006698, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '236311', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2787, 'n_words': 515, 'path': 'code/SaveBestModel.ipynb', 'repo_name': 'dlaredo/NASA_RUL_-CMAPS-', 'size': '7722'}