# Introduction to Deep Learning with Code[¶](https://knowledge.dataiku.com/latest/kb/analytics-ml/deep-learning-code/index.html#introduction-to-deep-learning-with-code "Permalink to this headline")

You can define a Deep Learning architecture using the Keras library to build a custom model in Dataiku’s Visual Machine Learning tool. You can then train, deploy, and score the model like any other model created and managed in Dataiku DSS.

Deep learning offers extremely flexible modeling of the relationships between a target and its input features, and is used in a variety of challenging applications, such as image processing, text analysis, and time series, in addition to models for structured data.

## Objectives[¶](https://knowledge.dataiku.com/latest/kb/analytics-ml/deep-learning-code/index.html#objectives "Permalink to this headline")

This how-to walks through the Deep Learning interface in Dataiku DSS, using a simple multilayer perceptron (MLP) for classification.

## Prerequisites[¶](https://knowledge.dataiku.com/latest/kb/analytics-ml/deep-learning-code/index.html#prerequisites "Permalink to this headline")

You should have some experience with Visual Machine Learning in Dataiku. We’ll use the project from that tutorial.

You should have some familiarity with Keras. While building a deep learning model in Dataiku requires some comfort with coding, its inclusion in the Visual ML tool means that the model lifecycle is managed by Dataiku.

You will need access to a code environment with the necessary libraries. When creating a code environment, you can add sets of packages on the **Packages to Install** tab. Choose the Visual Deep Learning package set that corresponds to the hardware you’re running on.

## The Deep Learning Model[¶](https://knowledge.dataiku.com/latest/kb/analytics-ml/deep-learning-code/index.html#the-deep-learning-model "Permalink to this headline")

In a Visual Analysis for the dataset *customers\_labeled* which is part of the Scoring Basics Tutorial , create a new model with:

* **Prediction** as the task,

* *high\_revenue* as the target variable

* **Expert mode** as the prediction style

* **Deep learning** as the Expert mode, then click **Create**

This creates a new machine learning task and opens the Design tab for the task. Like the ML task you worked with in the Visual ML tutorial, there are common settings for handling the target, features, train/test set, and evaluation metrics.

What is markedly different is that the Modeling section where you would specify algorithms settings has been replaced by a Deep Modeling section where you specify the Architecture and Training settings for your deep learning model.

Note

Dataiku should automatically discover the code environment you’ve created for deep learning, and set it as the runtime environment for this ML task. If, after creating this ML task, you receive a warning that no deep learning code environment is available, then you need to create one or ensure that you have permission to use the deep learning code environment.

### Deep Learning Architecture[¶](https://knowledge.dataiku.com/latest/kb/analytics-ml/deep-learning-code/index.html#deep-learning-architecture "Permalink to this headline")

In the Deep Learning Model Architecture, you supply the Keras code that defines the architecture of your deep learning model and then compiles it. This is done through two functions, `build\_model()` and `compile\_model`, that are used to standardized how deep learning models are handled in the Dataiku DSS implementation.

#### build\_model()[¶](https://knowledge.dataiku.com/latest/kb/analytics-ml/deep-learning-code/index.html#build-model "Permalink to this headline")

The `build\_model()`` function is where you define the model architecture using the Keras functional API; the Sequential model API is not supported. It has two parameters that help you to define the architecture:

* `input\_shapes` is a dictionary of the model inputs. More complex architectures can accept multiple inputs, but for now, we’ll simply work with the main set of inputs that includes all the features.

* `n\_classes` is the number of classes in a categorical target. Dataiku will supply this number when this function is called during model training; here, we are simply using it as a variable in the architecture definition

The default code provided by Dataiku defines a multilayer perceptron with two hidden layers.

The following snippet defines the input layer, extracting the `main` input from the dictionary of model inputs:

§ input\_main = Input(shape=input\_shapes["main"], name="main")

The following code defines the hidden layers. The first hidden layer is a Dense layer with 64 units, using the rectified linear unit Activation function to transform the input layer. The second hidden layer has similar parameters, but transforms the first input layer.

§ x = Dense(64, activation='relu')(input\_main)

§ x = Dense(64, activation='relu')(x)

The following code defines the output layer, which is a Dense layer with a number of units equal to the number of classes in the target, using the softmax activation function.

§ predictions = Dense(n\_classes, activation='softmax')(x)

Finally, we construct the Keras model object with the input and output layers, then return the model as the output of the function.

§ model = Model(inputs=[input\_main], outputs=predictions)

§ return model

#### compile\_model()[¶](https://knowledge.dataiku.com/latest/kb/analytics-ml/deep-learning-code/index.html#compile-model "Permalink to this headline")

The `compile\_model()`` function is where you specify the loss function to be optimized and the method for performing the optimization. While the Keras `compile()`` method includes metrics for evaluating the model, you don’t need to specify them here, because Dataiku DSS will pick them up from the common Metrics panel under the Basics model settings.

### Deep Learning Training[¶](https://knowledge.dataiku.com/latest/kb/analytics-ml/deep-learning-code/index.html#deep-learning-training "Permalink to this headline")

In the Deep Learning Training panel, you specify the parameters that will be used in the call to the Keras model `fit()`` method. If you need greater control over training, you can click Advanced mode and supply the code specifications you want.

## Model Results[¶](https://knowledge.dataiku.com/latest/kb/analytics-ml/deep-learning-code/index.html#model-results "Permalink to this headline")

Click **Train** and, when complete, Dataiku DSS shows the results as it would for any other machine learning model.

## Deploy the Model to the Flow[¶](https://knowledge.dataiku.com/latest/kb/analytics-ml/deep-learning-code/index.html#deploy-the-model-to-the-flow "Permalink to this headline")

As with any other model managed by Dataiku, you can deploy it to the flow, then add the deployed model to a dashboard, find it in the catalog, create an API service endpoint using it, and so on.

## What’s next[¶](https://knowledge.dataiku.com/latest/kb/analytics-ml/deep-learning-code/index.html#what-s-next "Permalink to this headline")

* See a completed version of this project on the Dataiku gallery.

* See the Dataiku DSS reference documentation on deep learning.
