---
title: "About Keras Models"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{About Keras Models}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
type: docs
repo: https://github.com/rstudio/keras
menu:
  main:
    name: "About Keras Models"
    identifier: "keras-about-keras-models"
    parent: "keras-advanced"
    weight: 10
---

```{r setup, include = FALSE}
library(keras)
knitr::opts_chunk$set(comment = NA, eval = FALSE)
```

## Overview

There are two types of built-in models available in Keras: [sequential models](sequential_model.html) and models created with the [functional API](functional_api.html). In addition, you can also create [custom models](custom_models.html) that define their own forward-pass logic.

### Sequential

Sequential models are created using the `keras_model_sequential()` function and are composed of a set of linear layers:

```{r}
model <- keras_model_sequential() 
model %>% 
  layer_dense(units = 32, input_shape = c(784)) %>% 
  layer_activation('relu') %>% 
  layer_dense(units = 10) %>% 
  layer_activation('softmax')
```

Note that Keras objects are [modified in place](faq.html#why-are-keras-objects-modified-in-place) which is why it's not necessary for `model` to be assigned back to after the layers are added.

Learn more by reading the [Guide to the Sequential Model](sequential_model.html).

### Functional

The functional API enables you to define more complex models, such as multi-output models, directed acyclic graphs, or models with shared layers. To create a model with the functional API compose a set of input and output layers then pass them to the `keras_model()` function: 

```{r}
tweet_a <- layer_input(shape = c(140, 256))
tweet_b <- layer_input(shape = c(140, 256))

# This layer can take as input a matrix and will return a vector of size 64
shared_lstm <- layer_lstm(units = 64)

# When we reuse the same layer instance multiple times, the weights of the layer are also
# being reused (it is effectively *the same* layer)
encoded_a <- tweet_a %>% shared_lstm
encoded_b <- tweet_b %>% shared_lstm

# We can then concatenate the two vectors and add a logistic regression on top
predictions <- layer_concatenate(c(encoded_a, encoded_b), axis=-1) %>% 
  layer_dense(units = 1, activation = 'sigmoid')

# We define a trainable model linking the tweet inputs to the predictions
model <- keras_model(inputs = c(tweet_a, tweet_b), outputs = predictions)
```

Learn more by reading the [Guide to the Functional API](functional_api.html).

### Custom

Custom models enable you to implement custom forward-pass logic (e.g. to encapsulate the logic associated with constructuing various types of models). See the article on [Writing Custom Keras Models](custom_models.html) for additional documentation, including an example that demonstrates creating a custom model that encapsulates a simple multi-layer-perceptron model with optional dropout and batch normalization layers.

## Properties

All models share the following properties:

- `model$layers` --- A flattened list of the layers comprising the model graph.

- `model$inputs` --- List of input tensors.

- `model$outputs` --- List of output tensors.


## Functions

These functions enable you to create, train, evaluate, persist, and generate predictions with models:

<table class="ref-index">

<colgroup>
<col class="alias" />
<col class="title" />
</colgroup>

<tbody>
<tr>
<!--  -->
<td>
`keras_model()`
</td>
<td><p>Keras Model</p></td>
</tr><tr>
<!--  -->
<td>
`keras_model_sequential()`
</td>
<td><p>Keras Model composed of a linear stack of layers</p></td>
</tr><tr>
<!--  -->
<td>
`compile()`
</td>
<td><p>Configure a Keras model for training</p></td>
</tr><tr>
<!--  -->
<td>
`fit()`
</td>
<td><p>Train a Keras model</p></td>
</tr><tr>
<!--  -->
<td>
`evaluate()`
</td>
<td><p>Evaluate a Keras model</p></td>
</tr><tr>
<!--  -->
<td>
`predict()`
</td>
<td><p>Predict Method for Keras Models</p></td>
</tr><tr>
<!--  -->
<td>
`summary()`
</td>
<td><p>Print a summary of a model</p></td>
</tr>
<tr>
<!--  -->
<td>
  `save_model_hdf5()` `load_model_hdf5()`
</td>
<td><p>Save/Load models using HDF5 files</p></td>
</tr><tr>
<!--  -->
<td>
  `get_layer()` 
</td>
<td><p>Retrieves a layer based on either its name (unique) or index.</p></td>
</tr><tr>
<!--  -->
<td>
  `pop_layer()` 
</td>
<td><p>Remove the last layer in a model</p></td>
</tr><tr>
<!--  -->
<td>
  `save_model_weights_hdf5()` `load_model_weights_hdf5()`
</td>
<td><p>Save/Load model weights using HDF5 files</p></td>
</tr><tr>
<!--  -->
<td>
  `get_weights()` `set_weights()`
</td>
<td><p>Layer/Model weights as R arrays</p></td>
</tr><tr>
<!--  -->
<td>
  `get_config()` `from_config()`
</td>
<td><p>Layer/Model configuration</p></td>
</tr><tr>
<!--  -->
<td>
  `model_to_json()` `model_from_json()`
</td>
<td><p>Model configuration as JSON</p></td>
</tr><tr>
<!--  -->
<td>
  `model_to_yaml()` `model_from_yaml()`
</td>
<td><p>Model configuration as YAML</p></td>
</tr>
</tbody>
</table>

