def main(chosen_device): exercice_number = 1 print('exercice {}\n=================='.format(exercice_number)) data_inputs, expected_outputs = generate_data( # See: https://github.com/guillaume-chevalier/seq2seq-signal-prediction/blob/master/datasets.py exercice_number=exercice_number, n_samples=None, window_size_past=None, window_size_future=None) print('data_inputs shape: {} => (n_samples, window_size_past, input_dim)'. format(data_inputs.shape)) print( 'expected_outputs shape: {} => (n_samples, window_size_future, output_dim)' .format(expected_outputs.shape)) sequence_length = data_inputs.shape[1] input_dim = data_inputs.shape[2] output_dim = expected_outputs.shape[2] batch_size = 100 epochs = 3 validation_size = 0.15 max_plotted_validation_predictions = 10 seq2seq_pipeline_hyperparams = HyperparameterSamples({ 'hidden_dim': 100, 'layers_stacked_count': 2, 'lambda_loss_amount': 0.0003, 'learning_rate': 0.006, 'window_size_future': sequence_length, 'output_dim': output_dim, 'input_dim': input_dim }) pipeline = Pipeline([ MiniBatchSequentialPipeline( [ ForEachDataInput(MeanStdNormalizer()), ToNumpy(), PlotPredictionsWrapper( Tensorflow2ModelStep( # See: https://github.com/Neuraxio/Neuraxle-TensorFlow create_model=create_model, create_loss=create_loss, create_optimizer=create_optimizer, expected_outputs_dtype=tf.dtypes.float32, data_inputs_dtype=tf.dtypes.float32, device_name=chosen_device, print_loss=True).set_hyperparams( seq2seq_pipeline_hyperparams)) ], batch_size=batch_size), ]).set_name('SignalPrediction') trainer = Trainer( epochs=epochs, validation_splitter=ValidationSplitter(test_size=validation_size), scoring_callback=ScoringCallback( metric_function=metric_3d_to_2d_wrapper(mean_squared_error), higher_score_is_better=False)) trial: Trial = trainer.train(pipeline=pipeline, data_inputs=data_inputs, expected_outputs=expected_outputs) plot_metrics( metric_name='mse', train_values=trial.validation_splits[0].metrics_results['main'] ['train_values'], validation_values=trial.validation_splits[0].metrics_results['main'] ['validation_values'], exercice_number=exercice_number) # Get trained pipeline pipeline = trial.get_trained_pipeline(split_number=0) # Get validation set with trainer.validation_split_function.split function. _, _, data_inputs_validation, expected_outputs_validation = trainer.validation_split_function.split( data_inputs=data_inputs, expected_outputs=expected_outputs) # Enable the plotting feature inside the PlotPredictionsWrapper wrapper step. pipeline.apply('toggle_plotting') pipeline.apply(method='set_max_plotted_predictions', max_plotted_predictions=max_plotted_validation_predictions) # Transform the trained pipeline to plot predictions pipeline.transform_data_container( DataContainer(data_inputs=data_inputs_validation[0], expected_outputs=expected_outputs_validation[0]))
# # Comparatively to what we see in the image, our neural network deals with # signal rather than letters. Also, we don't have the feedback mechanism # yet, which is to be implemented in the exercise 4. We do have the "GO" # token however. scaler = MinMaxScaler() result_scaler = MinMaxScaler() raw_data = pd.read_csv('train/train_data.csv') train_data = raw_data.iloc[:, 1:input_dim + 1] test_data = train_data.iloc[:, 0:output_dim] scale_train = scaler.fit_transform(train_data) scale_test = result_scaler.fit_transform(test_data) scale_train = scale_train[-420:] scale_test = scale_test[-420:] batch_x, batch_y = generate_data(scale_train, scale_test, batch_size, input_dim, output_dim) # Backward compatibility for TensorFlow's version 0.12: try: tf.nn.seq2seq = tf.contrib.legacy_seq2seq tf.nn.rnn_cell = tf.contrib.rnn tf.nn.rnn_cell.GRUCell = tf.contrib.rnn.GRUCell print("TensorFlow's version : 1.0 (or more)") except: print("TensorFlow's version : 0.12") # train or predict is_train = False tf.reset_default_graph() # sess.close()
import tensorflow as tf import tensorflow.keras as keras from datasets import generate_data import matplotlib.pyplot as plt from tensorflow.keras.layers import GRUCell, RNN, Dense, BatchNormalization from plotting import plot_metric, plot_predictions import tensorflow_addons as tfa from tensorflow_addons.rnn import LayerNormLSTMCell exercice_number = 2 print("exercice {}\n==================".format(exercice_number)) data_inputs, expected_outputs = generate_data( # See: https://github.com/guillaume-chevalier/seq2seq-signal-prediction/blob/master/datasets.py exercice_number=exercice_number, n_samples=50000, window_size_past=None, window_size_future=None, ) data_inputs.shape expected_outputs.shape sequence_length = data_inputs.shape[1] input_dim = data_inputs.shape[2] output_dim = expected_outputs.shape[2] inputs = keras.Input(shape=(None, input_dim), dtype=tf.dtypes.float32, name="encoder_inputs") encoder = RNN(
def main(chosen_device): exercice_number = 1 print('exercice {}\n=================='.format(exercice_number)) data_inputs, expected_outputs = generate_data( # See: https://github.com/guillaume-chevalier/seq2seq-signal-prediction/blob/master/datasets.py exercice_number=exercice_number, n_samples=None, window_size_past=None, window_size_future=None) print('data_inputs shape: {} => (n_samples, window_size_past, input_dim)'. format(data_inputs.shape)) print( 'expected_outputs shape: {} => (n_samples, window_size_future, output_dim)' .format(expected_outputs.shape)) sequence_length = data_inputs.shape[1] input_dim = data_inputs.shape[2] output_dim = expected_outputs.shape[2] batch_size = 100 epochs = 3 validation_size = 0.15 max_plotted_validation_predictions = 10 seq2seq_pipeline_hyperparams = HyperparameterSamples({ 'hidden_dim': 100, 'layers_stacked_count': 2, 'lambda_loss_amount': 0.0003, 'learning_rate': 0.006, 'window_size_future': sequence_length, 'output_dim': output_dim, 'input_dim': input_dim }) feature_0_metric = metric_3d_to_2d_wrapper(mean_squared_error) metrics = {'mse': feature_0_metric} signal_prediction_pipeline = Pipeline([ ForEachDataInput(MeanStdNormalizer()), ToNumpy(), PlotPredictionsWrapper( Tensorflow2ModelStep( # See: https://github.com/Neuraxio/Neuraxle-TensorFlow create_model=create_model, create_loss=create_loss, create_optimizer=create_optimizer, expected_outputs_dtype=tf.dtypes.float32, data_inputs_dtype=tf.dtypes.float32, print_loss=True).set_hyperparams(seq2seq_pipeline_hyperparams)) ]).set_name('SignalPrediction') pipeline = Pipeline([ EpochRepeater(ValidationSplitWrapper( MetricsWrapper(Pipeline([ TrainOnlyWrapper(DataShuffler()), MiniBatchSequentialPipeline([ MetricsWrapper(signal_prediction_pipeline, metrics=metrics, name='batch_metrics') ], batch_size=batch_size) ]), metrics=metrics, name='epoch_metrics', print_metrics=True), test_size=validation_size, scoring_function=feature_0_metric), epochs=epochs) ]) pipeline, outputs = pipeline.fit_transform(data_inputs, expected_outputs) plot_metrics(pipeline=pipeline, exercice_number=exercice_number) plot_predictions(data_inputs, expected_outputs, pipeline, max_plotted_validation_predictions)