Icon

DL 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.

URL: KNIME Deep Learning Integration Installation Guide https://docs.knime.com/latest/deep_learning_installation_guide/#dl_python_setup
URL: adapted from: Train a simple Multilayer Perceptron using TensorFlow 2 https://kni.me/w/Z1BLynW6P1l14odY
URL: TensorFlow 2 Tutorial: Get Started in Deep Learning With tf.keras https://machinelearningmastery.com/tensorflow-tutorial-deep-learning-with-tf-keras/
URL: Codeless Deep Learning with KNIME https://www.knime.com/codeless-deep-learning-book
URL: 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/
URL: (official) KNIME Python Integration Guide https://docs.knime.com/latest/python_installation_guide/index.html#_introduction
URL: Meta Collection about KNIME and Python https://kni.me/w/AvjrddXKOIoZYLV3
URL: (official) KNIME Deep Learning Integration Installation Guide https://docs.knime.com/latest/deep_learning_installation_guide/index.html#_introduction
URL: Medium: KNIME and Python — Setting up and managing Conda environments https://medium.com/@mlxl/knime-and-python-setting-up-and-managing-conda-environments-2ac217792539
URL: compact KNIME forum - "KNIME - Python and Deep Learning" https://forum.knime.com/t/tensorflow-keras-installation-conflict/48670/4?u=mlauber71
URL: KNIME Hub - Train a simple Multilayer Perceptron using TensorFlow 2 https://kni.me/w/0P1TaKHmebS4g8zv
URL: (knime forum hint) Tensorflow and python-flatbuffers<2.0 https://forum.knime.com/t/help-with-error-execute-failed-endvector-takes-1-positional-argument-but-2-were-given/38391/2?u=mlauber71

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 Workflow group:
https://kni.me/w/x8VnrmMzZAk94NlV

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

import tensorflow as tf

from TFModel import TFModel

import math

var_num_double_var = flow_variables['Number (double)']

num_inputs = var_num_double_var

num_classes = 2

num_hidden = math.floor(var_num_double_var / 2)

# Create a simple MLP using the tf.keras API

inp = 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 tf

import numpy as np

var_num_double_var = flow_variables['Number (double)']

# Get the training data from the table

x_train = input_table.iloc[:, :var_num_double_var].astype(np.float32).values

y_train = input_table.iloc[:, var_num_double_var:].astype(np.float32).values

# Set some training varibales

batch_size = 500

epochs = 100

input_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

Python Conda environment propagation. Please read this article for more details:


KNIME and Python — Setting up and managing Conda environments
https://medium.com/p/2ac217792539


KNIME and Python — Setting up Deep Learning Environments for Keras and TensorFlow 

https://medium.com/p/4b66003858f4

macOS version is work in progress

../model/adult_tensorflow2_mlp_model.zip
Model Reader
../data/deep_005_test.table
Table Reader
Activate Conda Environmentbased on Operating SystemWindows or macOSconfigure how to handle the environmentdefault = just check the names
py39_knime_tf2_cpu
test and validation together
Concatenate
../data/deep_009_validate.table
Table Reader
Train MLP
DL Python Network Learner
To one-hotvector
One to Many
CreateTensorFlow 2 MLP
DL Python Network Creator
GroupBy
Sort Targets
Column Resorter
get the numberof feature columns
Table Column to Variable
RowID
Merge Variables
Binary Classification Inspector
Number (double)
Row Filter (deprecated)
Execute onunseen data
TensorFlow 2 Network Executor
../model/adult_tensorflow2_mlp_model.zip Save for deployment
Model Writer
^(.*submission|solution).*$
Column Filter
solutionto string
Number to String
Extract Table Spec
Column Rename (deprecated)
number of Rows
Table Column to Variable
../data/deep_001_training.table
Table Reader
Extract Table Dimension

Nodes

Extensions

Links