Icon

kn_​deeplearn_​050_​tensorflow2_​basic_​mlp

Train a simple Multilayer Perceptron using TensorFlow 2

Train a simple Multilayer Perceptron using TensorFlow 2 for a binary classification

This workflow shows how to train a simple multilayer perceptron for classification. It is demonstrated how the "DL Python Network Creator" can be used to create a simple neural network using the tf.keras API and how the "DL Python Network Learner" can be used to train the created network on data.
Please note this example should demonstrate how to set up the deep learning environment with Tensor Flow 2 and provide a working simple example.

adapted from: https://kni.me/w/Z1BLynW6P1l14odY

please download the complete DeepLearning (Keras, Tensorflow, H2O.ai) Workflow group:
https://hub.knime.com/mlauber71/spaces/Public/latest/kn_example_deeplearning_keras_tensorflow_classification~G8jl-DTMCBqoxyB9/

------------------

In order to run the example, please make sure you have the following KNIME extensions installed:

* KNIME Deep Learning - TensorFlow 2 Integration (Labs)

You also need a local Python installation that includes TensorFlow 2. Please refer to https://docs.knime.com/latest/deep_learning_installation_guide/#dl_python_setup for installation recommendations and further information.












Train a simple Multilayer Perceptron using TensorFlow 2 for a binary classificationThis workflow shows how to train a simple multilayer perceptron for classification. It is demonstrated how the "DL Python Network Creator" can be used to create a simple neural network using the tf.keras API and how the "DL Python Network Learner" can be used to train the created network on data.Please note this example should demonstrate how to set up the deep learning environment with Tensor Flow 2 and provide a working simple example.adapted from: https://kni.me/w/Z1BLynW6P1l14odYplease download the complete DeepLearning (Keras, Tensorflow, H2O.ai) Workflow group:https://hub.knime.com/mlauber71/spaces/Public/latest/kn_example_deeplearning_keras_tensorflow_classification~G8jl-DTMCBqoxyB9/ Please note this is just an example for a DL network architecture.You might have to come up with your own settings!!!Or read the relevant KNIME book: https://www.knime.com/codeless-deep-learning-book this node should make sure you have a running Tensor Flow 2 (deep Learning ready Python environment if the complicated Model Quality Metanode does not work you can stick to the"Binary Classification Inspector" node # you will need a working Miniforge or Miniconda installation# please note - the popular Anaconda package has changed its licensing recently. # So make sure you comply with that. Or use Miniforge with conda-forge for the time being# https://docs.conda.io/en/latest/miniconda.html# check the conda environmentsconda info --envsKNIME and Python — Setting up and managing Conda environmentshttps://medium.com/@mlxl/knime-and-python-setting-up-and-managing-conda-environments-2ac217792539KNIME Python Integration Guidehttps://docs.knime.com/latest/python_installation_guide/index.html#_introductionMeta Collection about KNIME and Pythonhttps://kni.me/w/AvjrddXKOIoZYLV3KNIME Deep Learning Integration Installation Guidehttps://docs.knime.com/latest/deep_learning_installation_guide/index.html#_introduction import tensorflow as tffrom TFModel import TFModelimport mathvar_num_double_var = flow_variables['Number (double)']num_inputs = var_num_double_varnum_classes = 2num_hidden = math.floor(var_num_double_var / 2)# Create a simple MLP using the tf.keras APIinp = tf.keras.Input((num_inputs,))hid = tf.keras.layers.Dense(num_hidden, activation='relu')(inp)oup = tf.keras.layers.Dense(num_classes, activation='softmax')(hid)output_network = tf.keras.Model(inputs=inp, outputs=oup) import tensorflow as tfimport numpy as npvar_num_double_var = flow_variables['Number (double)']# Get the training data from the tablex_train = input_table.iloc[:,:var_num_double_var].valuesy_train = input_table.iloc[:,var_num_double_var:].values# Set some training varibalesbatch_size = 32epochs = 100input_network.compile(optimizer='adam', loss='categorical_crossentropy')input_network.fit(x=x_train, y=y_train, batch_size=batch_size, epochs=epochs, shuffle=True)output_network = input_network # KNIME and Python — Setting up and managing Conda environments# https://medium.com/@mlxl/knime-and-python-setting-up-and-managing-conda-environments-2ac217792539# conda update -n base -c conda-forge conda# conda env create -f="C:\Users\a123456789\knime-workspace\hub\kn_example_deeplearning_keras_tensorflow_classification\data\knime_py_tensorflow.yml"# conda remove --name knime_py_tensorflow --all# conda env create -f="/Users/m_lauber/Dropbox/knime-workspace/hub/kn_example_deeplearning_keras_tensorflow_classification/data/knime_py_tensorflow.yml"# conda activate knime_py_tensorflow# conda update -n knime_py_tensorflow --update-all# conda env update -n knime_py_tensorflow --file="/Users/m_lauber/Dropbox/knime-workspace/hub/kn_example_deeplearning_keras_tensorflow_classification/data/knime_py_tensorflow.yml"# conda config --get channels# conda deactivatename: knime_py_tensorflow # Name of the created environmentchannels: # Repositories to search for packages# - defaults# - anaconda- conda-forgedependencies: # List of packages that should be installed- python=3.9 # Python- py4j # used for KNIME <-> Python communication- nomkl # Prevents the use of Intel's MKL# conda install -n knime_py_tensorflow -c conda-forge pandas numpy --force-reinstall- pandas # Table data structures- jedi<=0.17.2 # Python script autocompletion- python-dateutil # Date and Time utilities- numpy # N-dimensional arrays- cairo # SVG support- pillow # Image inputs/outputs- matplotlib # Plotting- pyarrow=6.0 # Arrow serialization- IPython # Notebook support- nbformat # Notebook support- scipy # Notebook support# https://github.com/tensorflow/tensorflow/issues/51590# conda install -n knime_py_tensorflow -c conda-forge python-flatbuffers=1.12# search for the available package versions on conda-forge# conda search conda-forge::python-flatbuffers- python-flatbuffers<2.0 # because tensorflow expects a version before 2- h5py<3.0 # must be < 3.0 because they changed whether str or byte is returned- protobuf>3.12 # Lower protobuf versions do not work with TensorFlow 2- libiconv # MDF Reader node- asammdf=5.19.14 # MDF Reader node- pip- pip: - onnx - onnx-tf - tensorflow==2.5 - tensorflow-hub - JPype1 # Databases Propagate R environmentfor KNIME on MacOS withMinicondaconfigure how to handle the environmentdefault = just check the namestensor2.conda.environmentknime_py_tensorflow=> for Windows environment using theofficial KNIME recommendationsPython 3.9note: python-flatbuffers<2.0 for Tensorflow 2.5To one-hotvectorCreateTensorFlow 2 MLPTrain MLPSort TargetsExecute onunseen data../model/adult_tensorflow2_mlp_model_nw_writer.zipSave for deployment../model/adult_tensorflow2_mlp_model.zip../data/deep_001_training.table../data/deep_005_test.table../data/deep_009_validate.tabletest and validation togethersolutionto string^(.*submission|solution).*$get the numberof feature columnsbinary classification modelswith R. Results:/model/validation/number of RowsNumber (double)../model/adult_tensorflow2_mlp_model.zipSave for deploymentPropagate R environmentfor KNIME on Windows withMinicondaconfigure how to handle the environmentdefault = just check the namestensor2.conda.environmentknime_py_tensorflow=> for MacOSX environment using theofficial KNIME recommendationsPython 3.9note: python-flatbuffers<2.0 for Tensorflow 2.5 knime_r_environment knime_py_tensorflow_win One to Many DL PythonNetwork Creator DL PythonNetwork Learner Column Resorter TensorFlow 2Network Executor TensorFlow 2Network Writer TensorFlow 2Network Reader Table Reader Table Reader Table Reader Concatenate Number To String Column Filter Column Rename Extract Table Spec GroupBy RowID Table Columnto Variable Merge Variables Model QualityClassification - Graphics Binary ClassificationInspector Extract TableDimension Table Columnto Variable collect meta data Row Filter Model Writer knime_r_environment_windows knime_py_tensorflow_mac Train a simple Multilayer Perceptron using TensorFlow 2 for a binary classificationThis workflow shows how to train a simple multilayer perceptron for classification. It is demonstrated how the "DL Python Network Creator" can be used to create a simple neural network using the tf.keras API and how the "DL Python Network Learner" can be used to train the created network on data.Please note this example should demonstrate how to set up the deep learning environment with Tensor Flow 2 and provide a working simple example.adapted from: https://kni.me/w/Z1BLynW6P1l14odYplease download the complete DeepLearning (Keras, Tensorflow, H2O.ai) Workflow group:https://hub.knime.com/mlauber71/spaces/Public/latest/kn_example_deeplearning_keras_tensorflow_classification~G8jl-DTMCBqoxyB9/ Please note this is just an example for a DL network architecture.You might have to come up with your own settings!!!Or read the relevant KNIME book: https://www.knime.com/codeless-deep-learning-book this node should make sure you have a running Tensor Flow 2 (deep Learning ready Python environment if the complicated Model Quality Metanode does not work you can stick to the"Binary Classification Inspector" node # you will need a working Miniforge or Miniconda installation# please note - the popular Anaconda package has changed its licensing recently. # So make sure you comply with that. Or use Miniforge with conda-forge for the time being# https://docs.conda.io/en/latest/miniconda.html# check the conda environmentsconda info --envsKNIME and Python — Setting up and managing Conda environmentshttps://medium.com/@mlxl/knime-and-python-setting-up-and-managing-conda-environments-2ac217792539KNIME Python Integration Guidehttps://docs.knime.com/latest/python_installation_guide/index.html#_introductionMeta Collection about KNIME and Pythonhttps://kni.me/w/AvjrddXKOIoZYLV3KNIME Deep Learning Integration Installation Guidehttps://docs.knime.com/latest/deep_learning_installation_guide/index.html#_introduction import tensorflow as tffrom TFModel import TFModelimport mathvar_num_double_var = flow_variables['Number (double)']num_inputs = var_num_double_varnum_classes = 2num_hidden = math.floor(var_num_double_var / 2)# Create a simple MLP using the tf.keras APIinp = tf.keras.Input((num_inputs,))hid = tf.keras.layers.Dense(num_hidden, activation='relu')(inp)oup = tf.keras.layers.Dense(num_classes, activation='softmax')(hid)output_network = tf.keras.Model(inputs=inp, outputs=oup) import tensorflow as tfimport numpy as npvar_num_double_var = flow_variables['Number (double)']# Get the training data from the tablex_train = input_table.iloc[:,:var_num_double_var].valuesy_train = input_table.iloc[:,var_num_double_var:].values# Set some training varibalesbatch_size = 32epochs = 100input_network.compile(optimizer='adam', loss='categorical_crossentropy')input_network.fit(x=x_train, y=y_train, batch_size=batch_size, epochs=epochs, shuffle=True)output_network = input_network # KNIME and Python — Setting up and managing Conda environments# https://medium.com/@mlxl/knime-and-python-setting-up-and-managing-conda-environments-2ac217792539# conda update -n base -c conda-forge conda# conda env create -f="C:\Users\a123456789\knime-workspace\hub\kn_example_deeplearning_keras_tensorflow_classification\data\knime_py_tensorflow.yml"# conda remove --name knime_py_tensorflow --all# conda env create -f="/Users/m_lauber/Dropbox/knime-workspace/hub/kn_example_deeplearning_keras_tensorflow_classification/data/knime_py_tensorflow.yml"# conda activate knime_py_tensorflow# conda update -n knime_py_tensorflow --update-all# conda env update -n knime_py_tensorflow --file="/Users/m_lauber/Dropbox/knime-workspace/hub/kn_example_deeplearning_keras_tensorflow_classification/data/knime_py_tensorflow.yml"# conda config --get channels# conda deactivatename: knime_py_tensorflow # Name of the created environmentchannels: # Repositories to search for packages# - defaults# - anaconda- conda-forgedependencies: # List of packages that should be installed- python=3.9 # Python- py4j # used for KNIME <-> Python communication- nomkl # Prevents the use of Intel's MKL# conda install -n knime_py_tensorflow -c conda-forge pandas numpy --force-reinstall- pandas # Table data structures- jedi<=0.17.2 # Python script autocompletion- python-dateutil # Date and Time utilities- numpy # N-dimensional arrays- cairo # SVG support- pillow # Image inputs/outputs- matplotlib # Plotting- pyarrow=6.0 # Arrow serialization- IPython # Notebook support- nbformat # Notebook support- scipy # Notebook support# https://github.com/tensorflow/tensorflow/issues/51590# conda install -n knime_py_tensorflow -c conda-forge python-flatbuffers=1.12# search for the available package versions on conda-forge# conda search conda-forge::python-flatbuffers- python-flatbuffers<2.0 # because tensorflow expects a version before 2- h5py<3.0 # must be < 3.0 because they changed whether str or byte is returned- protobuf>3.12 # Lower protobuf versions do not work with TensorFlow 2- libiconv # MDF Reader node- asammdf=5.19.14 # MDF Reader node- pip- pip: - onnx - onnx-tf - tensorflow==2.5 - tensorflow-hub - JPype1 # Databases Propagate R environmentfor KNIME on MacOS withMinicondaconfigure how to handle the environmentdefault = just check the namestensor2.conda.environmentknime_py_tensorflow=> for Windows environment using theofficial KNIME recommendationsPython 3.9note: python-flatbuffers<2.0 for Tensorflow 2.5To one-hotvectorCreateTensorFlow 2 MLPTrain MLPSort TargetsExecute onunseen data../model/adult_tensorflow2_mlp_model_nw_writer.zipSave for deployment../model/adult_tensorflow2_mlp_model.zip../data/deep_001_training.table../data/deep_005_test.table../data/deep_009_validate.tabletest and validation togethersolutionto string^(.*submission|solution).*$get the numberof feature columnsbinary classification modelswith R. Results:/model/validation/number of RowsNumber (double)../model/adult_tensorflow2_mlp_model.zipSave for deploymentPropagate R environmentfor KNIME on Windows withMinicondaconfigure how to handle the environmentdefault = just check the namestensor2.conda.environmentknime_py_tensorflow=> for MacOSX environment using theofficial KNIME recommendationsPython 3.9note: python-flatbuffers<2.0 for Tensorflow 2.5 knime_r_environment knime_py_tensorflow_win One to Many DL PythonNetwork Creator DL PythonNetwork Learner Column Resorter TensorFlow 2Network Executor TensorFlow 2Network Writer TensorFlow 2Network Reader Table Reader Table Reader Table Reader Concatenate Number To String Column Filter Column Rename Extract Table Spec GroupBy RowID Table Columnto Variable Merge Variables Model QualityClassification - Graphics Binary ClassificationInspector Extract TableDimension Table Columnto Variable collect meta data Row Filter Model Writer knime_r_environment_windows knime_py_tensorflow_mac

Nodes

Extensions

Links