Skip to content

Commit

Permalink
Fixed bugs for LSTM models in config json and code
Browse files Browse the repository at this point in the history
  • Loading branch information
stonescenter committed Feb 13, 2020
1 parent def2952 commit a6a18e5
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 129 deletions.
68 changes: 68 additions & 0 deletions config-lstm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"paths": {
"save_dir": "results",
"log_dir": "logs"
},
"data": {
"filename": "./dataset/phi025-025_eta025-025_filtered.csv",
"train_split": 0.70,
"normalise": true
},
"training": {
"epochs": 20,
"batch_size": 32,
"save_model": true,
"load_model": false,
"use_gpu": true
},
"model": {
"name": "lstm",
"loss": "mse",
"optimizer": "RMSprop",
"layers": [
{
"type": "lstm",
"neurons": 200,
"input_timesteps": 4,
"input_features": 3,
"return_seq": true
},
{
"type": "lstm",
"neurons": 200,
"return_seq": false
},
{
"type": "dense",
"neurons": 400,
"activation": "tanh"
},
{
"type": "activation",
"activation": "tanh"
},
{
"type": "repeatvector",
"neurons": 50
},
{
"type": "lstm",
"neurons": 200,
"return_seq": true
},
{
"type": "lstm",
"neurons": 200,
"return_seq": true
},
{
"type": "timedistributed",
"neurons": 3
},
{
"type": "activation",
"activation": "linear"
}
]
}
}
51 changes: 51 additions & 0 deletions config-paralel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"paths": {
"save_dir": "results",
"log_dir": "logs"
},
"data": {
"filename": "./dataset/phi025-025_eta025-025_filtered.csv",
"train_split": 0.70,
"normalise": true
},
"training": {
"epochs": 20,
"batch_size": 128,
"save_model": true,
"load_model": false,
"use_gpu": true
},
"model": {
"name": "lstm-paralel",
"loss": "mse",
"optimizer": "RMSprop",
"layers": [
{
"type": "lstm-paralel",
"neurons": 400,
"input_timesteps": 4,
"input_features": 3,
"return_seq": false
},
{
"type": "dense",
"neurons": 400,
"activation": "relu"
},
{
"type": "dropout",
"rate": 0.2
},
{
"type": "dense",
"neurons": 20,
"activation": "relu"
},
{
"type": "dense",
"neurons": 3,
"activation": "linear"
}
]
}
}
36 changes: 25 additions & 11 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
{
"paths": {
"save_dir": "results",
"log_dir": "logs"
},
"data": {
"filename": "./dataset/event_2000_2020_train.csv",
"train_split": 0.80,
"filename": "./dataset/phi025-025_eta025-025_filtered.csv",
"train_split": 0.70,
"normalise": true
},
"training": {
"epochs": 30,
"batch_size": 20
"epochs": 20,
"batch_size": 32,
"save_model": true,
"load_model": true,
"use_gpu": true
},

"model": {
"name": "lstm",
"loss": "mse",
"optimizer": "adam",
"save_dir": "results/",
"optimizer": "RMSprop",
"layers": [
{
"type": "lstm",
"neurons": 20,
"neurons": 400,
"input_timesteps": 4,
"input_features": 3,
"return_seq": true
Expand All @@ -28,11 +33,20 @@
},
{
"type": "lstm",
"neurons": 20,
"neurons": 400,
"input_timesteps": 4,
"input_features": 3,
"return_seq": true
},
"return_seq": false
},
{
"type": "dropout",
"rate": 0.2
},
{
"type": "dense",
"neurons": 20,
"activation": "relu"
},
{
"type": "dense",
"neurons": 3,
Expand Down
17 changes: 7 additions & 10 deletions core/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def __init__(self, configs):
set_random_seed(42)

def load_model(self):
if self.exist_model(self.save_fname):
print('[Model] Loading model from file %s' % self.save_fname)
self.model = load_model(self.save_fname)
if self.exist_model(self.save_fnameh5):
print('[Model] Loading model from file %s' % self.save_fnameh5)
self.model = load_model(self.save_fnameh5)
return True
else:
print('[Model] Can not load the model from file %s' % self.save_fname)
Expand All @@ -71,11 +71,11 @@ def train(self, x, y, epochs, batch_size):
timer.start()
print('[Model] Training Started')
print('[Model] %s epochs, %s batch size' % (epochs, batch_size))

#print('[Model] Shape of data train: ', x.shape)
#save_fname = os.path.join(save_dir, '%s-e%s.h5' % (dt.datetime.now().strftime('%d%m%Y-%H%M%S'), str(epochs)))
callbacks = [
EarlyStopping(monitor='val_loss', patience=2),
ModelCheckpoint(filepath=self.save_fname, monitor='val_loss', save_best_only=True)
ModelCheckpoint(filepath=self.save_fnameh5, monitor='val_loss', save_best_only=True)
]
history = self.model.fit(
x,
Expand All @@ -87,11 +87,8 @@ def train(self, x, y, epochs, batch_size):
)

if self.save == True:
self.save_model(self.save_fnameh5)
#self.save_architecture(self.save_fname)

self.model.summary()

self.save_model(self.save_fnameh5)

print('[Model] Training Completed. Model h5 saved as %s' % self.save_fnameh5)
print('[Model] Model train with structure:', self.model.inputs)
timer.stop()
Expand Down
18 changes: 11 additions & 7 deletions core/models/lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ def __init__(self, configs):
super().__init__(configs)
self.c = configs

def build_model(self, configs):
def build_model(self):
timer = Timer()
timer.start()

print('[Model] Creating model..')
self.name = configs['model']['name']
configs = self.c

configs = self.c

for layer in configs['model']['layers']:

neurons = layer['neurons'] if 'neurons' in layer else None
Expand All @@ -33,6 +34,8 @@ def build_model(self, configs):
input_timesteps = layer['input_timesteps'] if 'input_timesteps' in layer else None
input_features = layer['input_features'] if 'input_features' in layer else None

print('input_features %s input_timesteps %s ' % ( input_features, input_timesteps))

if layer['type'] == 'lstm':
self.model.add(LSTM(neurons, input_shape=(input_timesteps, input_features), return_sequences=return_seq))
if layer['type'] == 'dense':
Expand All @@ -41,8 +44,8 @@ def build_model(self, configs):
self.model.add(Dropout(dropout_rate))
if layer['type'] == 'repeatvector':
self.model.add(RepeatVector(neurons))
if layer['type'] == 'timedistributed':
self.model.add(TimeDistributed(Dense(input_features)))
if layer['type'] == 'timedistributed':
self.model.add(TimeDistributed(Dense(neurons)))
if layer['type'] == 'activation':
self.model.add(Activation('linear'))

Expand All @@ -60,7 +63,7 @@ def __init__(self, configs):
super().__init__(configs)
self.c = configs

def build_model(self, configs):
def build_model(self):
timer = Timer()
timer.start()

Expand All @@ -69,6 +72,7 @@ def build_model(self, configs):
# this model is not sequencial
self.model = None
configs = self.c

for layer in configs['model']['layers']:

neurons = layer['neurons'] if 'neurons' in layer else None
Expand Down Expand Up @@ -145,7 +149,7 @@ def build_model(self):

self.model = Model(inputs=[first_input, second_input], outputs=output)
self.model.compile(loss=configs['model']['loss'], optimizer=configs['model']['optimizer'], metrics=['accuracy'])

print(self.model.summary())
print('[Model] Model Compiled with structure:', self.model.inputs)
self.save_architecture(self.save_fname)

Expand Down
62 changes: 36 additions & 26 deletions main-paralel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@

import numpy as np

# def parse_args():
# """Parse arguments."""
# # Parameters settings
# parser = argparse.ArgumentParser(description="LSTM implementation ")

# # Dataset setting
# parser.add_argument('--data_prefix', type=str, default="./dataset/2020_100_sorted.csv", help='Data file')
# parser.add_argument('--save_model', type=str, default="./results/model_lstm.h5", help='Model for inference')

# # Training parameters setting
# parser.add_argument('--neurons', type=int, default=1000, help='number of epochs to train [10, 200, 500]')
# parser.add_argument('--batch_size', type=int, default=1, help='number of batch')

# parser.add_argument('--epochs', type=int, default=15, help='number of epochs to train [10, 200, 500]')
# parser.add_argument('--lr', type=float, default=0.001, help='learning rate [0.001] reduced by 0.1 after each 10000 iterations')
def parse_args():
"""Parse arguments."""
# Parameters settings
parser = argparse.ArgumentParser(description="LSTM implementation ")

# Dataset setting
parser.add_argument('--config', type=str, default="config.json", help='Configuration file')

# # parse the arguments
# args = parser.parse_args()
# parse the arguments
args = parser.parse_args()

# return args
return args

def gpu():
import tensorflow as tf
Expand All @@ -59,12 +51,26 @@ def no_gpu():
sess = tf.Session(config=config)
set_session(sess)

def manage_models(config):

type_model = config['model']['name']
model = None

if type_model == 'lstm': #simple LSTM
model = ModelLSTM(config)
elif type_model == 'lstm-paralel':
model = ModelLSTMParalel(config)
elif type_model == 'cnn':
model = ModelCNN(config)

return model

def main():

#args = parse_args()
args = parse_args()

# load configurations of model and others
configs = json.load(open('config-paralel.json', 'r'))
configs = json.load(open(args.config, 'r'))

# create defaults dirs
output_path = configs['paths']['save_dir']
Expand Down Expand Up @@ -93,7 +99,7 @@ def main():
data = Dataset(data_dir, KindNormalization.Zscore)

X, X_, y = data.prepare_training_data(FeatureType.Divided, normalise=True,
cilyndrical=False)
cilyndrical=True)

# reshape data
X = data.reshape3d(X, time_steps, num_features)
Expand All @@ -107,7 +113,11 @@ def main():
print('[Data] shape data y_test.shape:', y_test.shape)


model = ModelLSTMParalel(configs)
model = manage_models(configs)

if model is None:
print('Please instance model')
return

loadModel = configs['training']['load_model']

Expand Down Expand Up @@ -176,16 +186,16 @@ def main():

#Save data to plot
X, X_, y = data.prepare_training_data(FeatureType.Divided, normalise=False,
cilyndrical=False)
cilyndrical=True)
X_train, X_test, X_train_, X_test_, y_train, y_test = train_test_split(X, X_, y,
test_size=1-split, random_state=42)

y_pred = pd.DataFrame(y_predicted_orig)
y_true = pd.DataFrame(y_test_orig)

y_true.to_csv(os.path.join(output_path, 'y_true.csv'), header=False, index=False)
y_pred.to_csv(os.path.join(output_path, 'y_pred.csv'), header=False, index=False)
X_test.to_csv(os.path.join(output_path, 'x_test.csv'), header=False, index=False)
y_true.to_csv(os.path.join(output_path, 'y_true_cylin.csv'), header=False, index=False)
y_pred.to_csv(os.path.join(output_path, 'y_pred_cylin.csv'), header=False, index=False)
X_test.to_csv(os.path.join(output_path, 'x_test_cylin.csv'), header=False, index=False)
print('[Output] Results saved at %', output_path)
if __name__=='__main__':
main()
Expand Down
Loading

0 comments on commit a6a18e5

Please sign in to comment.