Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Trainer.train from the new Automl loop #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ from datasets import generate_data
from datasets import metric_3d_to_2d_wrapper
from neuraxle_tensorflow.tensorflow_v1 import TensorflowV1ModelStep
from neuraxle_tensorflow.tensorflow_v2 import Tensorflow2ModelStep
from plotting import plot_metrics
from train_values import plot_metrics
from steps import MeanStdNormalizer, ToNumpy, PlotPredictionsWrapper

%matplotlib inline
Expand Down
17 changes: 7 additions & 10 deletions plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@ def plot_predictions(data_inputs, expected_outputs, predicted_outputs, save=Fals
plt.show()


def plot_metrics(pipeline, exercice_number):
mse_train = pipeline.get_step_by_name('epoch_metrics').get_metric_train('mse')
print('last mse train: {}'.format(mse_train[-1]))
print('best mse train: {}'.format(min(mse_train)))

mse_validation = pipeline.get_step_by_name('epoch_metrics').get_metric_validation('mse')
print('last mse validation: {}'.format(mse_validation[-1]))
print('best mse validation: {}'.format(min(mse_validation)))
def plot_metrics(metric_name, train_values, validation_values, exercice_number):
print('last mse train: {}'.format(train_values[-1]))
print('best mse train: {}'.format(min(train_values)))
print('last {} validation: {}'.format(metric_name, validation_values[-1]))
print('best {} validation: {}'.format(metric_name, min(validation_values)))

plot_metric(
mse_train,
mse_validation,
train_values,
validation_values,
xlabel='epoch',
ylabel='mse',
title='Exercice {} Model Mean Squared Error'.format(exercice_number)
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tensorflow==2.1.0
neuraxle==0.3.1
neuraxle_tensorflow==0.1.0
-e git://github.com/Neuraxio/Neuraxle-Tensorflow.git@save-tensorflow-v2-with-context#egg=neuraxle_tensorflow
-e git://github.com/Neuraxio/Neuraxle.git@master#egg=neuraxle
111 changes: 57 additions & 54 deletions seq2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import tensorflow as tf
from neuraxle.data_container import DataContainer
from neuraxle.hyperparams.space import HyperparameterSamples
from neuraxle.metaopt.random import ValidationSplitWrapper
from neuraxle.metrics import MetricsWrapper
from neuraxle.metaopt.auto_ml import Trainer, ValidationSplitter
from neuraxle.metaopt.callbacks import ScoringCallback
from neuraxle.metaopt.trial import Trial
from neuraxle.pipeline import Pipeline, MiniBatchSequentialPipeline
from neuraxle.steps.data import EpochRepeater, DataShuffler
from neuraxle.steps.flow import TrainOnlyWrapper
from neuraxle.steps.loop import ForEachDataInput
from neuraxle_tensorflow.tensorflow_v1 import TensorflowV1ModelStep
from neuraxle_tensorflow.tensorflow_v2 import Tensorflow2ModelStep
from sklearn.metrics import mean_squared_error
from tensorflow_core.python.client import device_lib
from tensorflow_core.python.keras import Input, Model
Expand All @@ -18,8 +19,6 @@

from datasets import generate_data
from datasets import metric_3d_to_2d_wrapper
from neuraxle_tensorflow.tensorflow_v1 import TensorflowV1ModelStep
from neuraxle_tensorflow.tensorflow_v2 import Tensorflow2ModelStep
from plotting import plot_metrics
from steps import MeanStdNormalizer, ToNumpy, PlotPredictionsWrapper

Expand Down Expand Up @@ -178,59 +177,63 @@ def main(chosen_device):
'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))

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')

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)


def plot_predictions(data_inputs, expected_outputs, pipeline, max_plotted_predictions):
_, _, data_inputs_validation, expected_outputs_validation = \
pipeline.get_step_by_name('ValidationSplitWrapper').split(data_inputs, expected_outputs)
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('set_max_plotted_predictions', max_plotted_predictions)
pipeline.apply(
method='set_max_plotted_predictions',
max_plotted_predictions=max_plotted_validation_predictions
)

signal_prediction_pipeline = pipeline.get_step_by_name('SignalPrediction')
signal_prediction_pipeline.transform_data_container(DataContainer(
data_inputs=data_inputs_validation,
expected_outputs=expected_outputs_validation
# Transform the trained pipeline to plot predictions
pipeline.transform_data_container(DataContainer(
data_inputs=data_inputs_validation[0],
expected_outputs=expected_outputs_validation[0]
))


Expand Down
3 changes: 3 additions & 0 deletions steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
from neuraxle.base import NonFittableMixin, BaseStep, Identity, ExecutionContext
from neuraxle.data_container import DataContainer
from neuraxle.hyperparams.space import RecursiveDict
from neuraxle.steps.output_handlers import InputAndOutputTransformerMixin
from neuraxle.union import FeatureUnion

Expand Down Expand Up @@ -80,9 +81,11 @@ def __init__(self, plotting_function: Callable, max_plotted_predictions, enabled

def set_max_plotted_predictions(self, max_plotted_predictions):
self.max_plotted_predictions = max_plotted_predictions
return RecursiveDict()

def toggle_plotting(self):
self.enabled = not self.enabled
return RecursiveDict()

def transform(self, data_inputs):
raise NotImplementedError('must be used inside a pipeline')
Expand Down
15 changes: 15 additions & 0 deletions train_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def plot_metrics(metric_name, train_values, metric_validation, exercice_number):
print('last mse train: {}'.format(train_values[-1]))
print('best mse train: {}'.format(min(mse_train)))

mse_validation = 0
print('last mse validation: {}'.format(mse_validation[-1]))
print('best mse validation: {}'.format(min(mse_validation)))

plot_metric(
mse_train,
mse_validation,
xlabel='epoch',
ylabel='mse',
title='Exercice {} Model Mean Squared Error'.format(exercice_number)
)