1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
|
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="http://keras.io/getting-started/functional-api-guide/">
<link rel="shortcut icon" href="../../img/favicon.ico">
<title>Guide to the Functional API - Keras Documentation</title>
<link href='https://fonts.googleapis.com/css?family=Lato:400,700|Source+Sans+Pro:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="../../css/theme.css" type="text/css" />
<link rel="stylesheet" href="../../css/theme_extra.css" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
<script>
// Current page data
var mkdocs_page_name = "Guide to the Functional API";
var mkdocs_page_input_path = "getting-started/functional-api-guide.md";
var mkdocs_page_url = "/getting-started/functional-api-guide/";
</script>
<script src="../../js/jquery-2.1.1.min.js" defer></script>
<script src="../../js/modernizr-2.8.3.min.js" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-61785484-1', 'keras.io');
ga('send', 'pageview');
</script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
<div class="wy-side-scroll">
<a href="">
<div class="keras-logo">
<img src="/img/keras-logo-small.jpg" class="keras-logo-img">
Keras Documentation
</div>
</a>
<div class="wy-side-nav-search">
<div role="search">
<form id ="rtd-search-form" class="wy-form" action="../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" title="Type search term here" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../..">Home</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../why-use-keras/">Why use Keras</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Getting started</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../sequential-model-guide/">Guide to the Sequential model</a>
</li>
<li class="toctree-l1 current"><a class="reference internal current" href="./">Guide to the Functional API</a>
<ul class="current">
<li class="toctree-l2"><a class="reference internal" href="#first-example-a-densely-connected-network">First example: a densely-connected network</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="#all-models-are-callable-just-like-layers">All models are callable, just like layers</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="#multi-input-and-multi-output-models">Multi-input and multi-output models</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="#shared-layers">Shared layers</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="#the-concept-of-layer-node">The concept of layer "node"</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="#more-examples">More examples</a>
<ul>
<li class="toctree-l3"><a class="reference internal" href="#inception-module">Inception module</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#residual-connection-on-a-convolution-layer">Residual connection on a convolution layer</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#shared-vision-model">Shared vision model</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#visual-question-answering-model">Visual question answering model</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#video-question-answering-model">Video question answering model</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../faq/">FAQ</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../models/about-keras-models/">About Keras models</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../models/sequential/">Sequential</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../models/model/">Model (functional API)</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Layers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../layers/about-keras-layers/">About Keras layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/core/">Core Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/convolutional/">Convolutional Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/pooling/">Pooling Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/local/">Locally-connected Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/recurrent/">Recurrent Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/embeddings/">Embedding Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/merge/">Merge Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/advanced-activations/">Advanced Activations Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/normalization/">Normalization Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/noise/">Noise layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/wrappers/">Layer wrappers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../layers/writing-your-own-keras-layers/">Writing your own Keras layers</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Preprocessing</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../preprocessing/sequence/">Sequence Preprocessing</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../preprocessing/text/">Text Preprocessing</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../preprocessing/image/">Image Preprocessing</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../losses/">Losses</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../metrics/">Metrics</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../optimizers/">Optimizers</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../activations/">Activations</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../callbacks/">Callbacks</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../datasets/">Datasets</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../applications/">Applications</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../backend/">Backend</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../initializers/">Initializers</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../regularizers/">Regularizers</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../constraints/">Constraints</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../visualization/">Visualization</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../scikit-learn-api/">Scikit-learn API</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../utils/">Utils</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../contributing/">Contributing</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Examples</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../examples/addition_rnn/">Addition RNN</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/antirectifier/">Custom layer - antirectifier</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/babi_rnn/">Baby RNN</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/babi_memnn/">Baby MemNN</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/cifar10_cnn/">CIFAR-10 CNN</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/cifar10_resnet/">CIFAR-10 ResNet</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/conv_filter_visualization/">Convolution filter visualization</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/conv_lstm/">Convolutional LSTM</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/deep_dream/">Deep Dream</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/image_ocr/">Image OCR</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/imdb_bidirectional_lstm/">Bidirectional LSTM</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/imdb_cnn/">1D CNN for text classification</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/imdb_cnn_lstm/">Sentiment classification CNN-LSTM</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/imdb_fasttext/">Fasttext for text classification</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/imdb_lstm/">Sentiment classification LSTM</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/lstm_seq2seq/">Sequence to sequence - training</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/lstm_seq2seq_restore/">Sequence to sequence - prediction</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/lstm_stateful/">Stateful LSTM</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/lstm_text_generation/">LSTM for text generation</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../examples/mnist_acgan/">Auxiliary Classifier GAN</a>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../..">Keras Documentation</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../..">Docs</a> »</li>
<li>Getting started »</li>
<li>Guide to the Functional API</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/keras-team/keras/tree/master/docs"
class="icon icon-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
</div>
<div role="main">
<div class="section">
<h1 id="getting-started-with-the-keras-functional-api">Getting started with the Keras functional API</h1>
<p>The Keras functional API is the way to go for defining complex models, such as multi-output models, directed acyclic graphs, or models with shared layers.</p>
<p>This guide assumes that you are already familiar with the <code>Sequential</code> model.</p>
<p>Let's start with something simple.</p>
<hr />
<h2 id="first-example-a-densely-connected-network">First example: a densely-connected network</h2>
<p>The <code>Sequential</code> model is probably a better choice to implement such a network, but it helps to start with something really simple.</p>
<ul>
<li>A layer instance is callable (on a tensor), and it returns a tensor</li>
<li>Input tensor(s) and output tensor(s) can then be used to define a <code>Model</code></li>
<li>Such a model can be trained just like Keras <code>Sequential</code> models.</li>
</ul>
<pre><code class="python">from keras.layers import Input, Dense
from keras.models import Model
# This returns a tensor
inputs = Input(shape=(784,))
# a layer instance is callable on a tensor, and returns a tensor
output_1 = Dense(64, activation='relu')(inputs)
output_2 = Dense(64, activation='relu')(output_1)
predictions = Dense(10, activation='softmax')(output_2)
# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(data, labels) # starts training
</code></pre>
<hr />
<h2 id="all-models-are-callable-just-like-layers">All models are callable, just like layers</h2>
<p>With the functional API, it is easy to reuse trained models: you can treat any model as if it were a layer, by calling it on a tensor. Note that by calling a model you aren't just reusing the <em>architecture</em> of the model, you are also reusing its weights.</p>
<pre><code class="python">x = Input(shape=(784,))
# This works, and returns the 10-way softmax we defined above.
y = model(x)
</code></pre>
<p>This can allow, for instance, to quickly create models that can process <em>sequences</em> of inputs. You could turn an image classification model into a video classification model, in just one line.</p>
<pre><code class="python">from keras.layers import TimeDistributed
# Input tensor for sequences of 20 timesteps,
# each containing a 784-dimensional vector
input_sequences = Input(shape=(20, 784))
# This applies our previous model to every timestep in the input sequences.
# the output of the previous model was a 10-way softmax,
# so the output of the layer below will be a sequence of 20 vectors of size 10.
processed_sequences = TimeDistributed(model)(input_sequences)
</code></pre>
<hr />
<h2 id="multi-input-and-multi-output-models">Multi-input and multi-output models</h2>
<p>Here's a good use case for the functional API: models with multiple inputs and outputs. The functional API makes it easy to manipulate a large number of intertwined datastreams.</p>
<p>Let's consider the following model. We seek to predict how many retweets and likes a news headline will receive on Twitter. The main input to the model will be the headline itself, as a sequence of words, but to spice things up, our model will also have an auxiliary input, receiving extra data such as the time of day when the headline was posted, etc.
The model will also be supervised via two loss functions. Using the main loss function earlier in a model is a good regularization mechanism for deep models.</p>
<p>Here's what our model looks like:</p>
<p><img src="https://s3.amazonaws.com/keras.io/img/multi-input-multi-output-graph.png" alt="multi-input-multi-output-graph" style="width: 400px;"/></p>
<p>Let's implement it with the functional API.</p>
<p>The main input will receive the headline, as a sequence of integers (each integer encodes a word).
The integers will be between 1 and 10,000 (a vocabulary of 10,000 words) and the sequences will be 100 words long.</p>
<pre><code class="python">from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model
import numpy as np
np.random.seed(0) # Set a random seed for reproducibility
# Headline input: meant to receive sequences of 100 integers, between 1 and 10000.
# Note that we can name any layer by passing it a "name" argument.
main_input = Input(shape=(100,), dtype='int32', name='main_input')
# This embedding layer will encode the input sequence
# into a sequence of dense 512-dimensional vectors.
x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input)
# A LSTM will transform the vector sequence into a single vector,
# containing information about the entire sequence
lstm_out = LSTM(32)(x)
</code></pre>
<p>Here we insert the auxiliary loss, allowing the LSTM and Embedding layer to be trained smoothly even though the main loss will be much higher in the model.</p>
<pre><code class="python">auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)
</code></pre>
<p>At this point, we feed into the model our auxiliary input data by concatenating it with the LSTM output:</p>
<pre><code class="python">auxiliary_input = Input(shape=(5,), name='aux_input')
x = keras.layers.concatenate([lstm_out, auxiliary_input])
# We stack a deep densely-connected network on top
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
# And finally we add the main logistic regression layer
main_output = Dense(1, activation='sigmoid', name='main_output')(x)
</code></pre>
<p>This defines a model with two inputs and two outputs:</p>
<pre><code class="python">model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])
</code></pre>
<p>We compile the model and assign a weight of 0.2 to the auxiliary loss.
To specify different <code>loss_weights</code> or <code>loss</code> for each different output, you can use a list or a dictionary.
Here we pass a single loss as the <code>loss</code> argument, so the same loss will be used on all outputs.</p>
<pre><code class="python">model.compile(optimizer='rmsprop', loss='binary_crossentropy',
loss_weights=[1., 0.2])
</code></pre>
<p>We can train the model by passing it lists of input arrays and target arrays:</p>
<pre><code class="python">headline_data = np.round(np.abs(np.random.rand(12, 100) * 100))
additional_data = np.random.randn(12, 5)
headline_labels = np.random.randn(12, 1)
additional_labels = np.random.randn(12, 1)
model.fit([headline_data, additional_data], [headline_labels, additional_labels],
epochs=50, batch_size=32)
</code></pre>
<p>Since our inputs and outputs are named (we passed them a "name" argument),
we could also have compiled the model via:</p>
<pre><code class="python">model.compile(optimizer='rmsprop',
loss={'main_output': 'binary_crossentropy', 'aux_output': 'binary_crossentropy'},
loss_weights={'main_output': 1., 'aux_output': 0.2})
# And trained it via:
model.fit({'main_input': headline_data, 'aux_input': additional_data},
{'main_output': headline_labels, 'aux_output': additional_labels},
epochs=50, batch_size=32)
</code></pre>
<p>To use the model for inferencing, use</p>
<pre><code class="python">model.predict({'main_input': headline_data, 'aux_input': additional_data})
</code></pre>
<p>or alternatively,</p>
<pre><code class="python">pred = model.predict([headline_data, additional_data])
</code></pre>
<hr />
<h2 id="shared-layers">Shared layers</h2>
<p>Another good use for the functional API are models that use shared layers. Let's take a look at shared layers.</p>
<p>Let's consider a dataset of tweets. We want to build a model that can tell whether two tweets are from the same person or not (this can allow us to compare users by the similarity of their tweets, for instance).</p>
<p>One way to achieve this is to build a model that encodes two tweets into two vectors, concatenates the vectors and then adds a logistic regression; this outputs a probability that the two tweets share the same author. The model would then be trained on positive tweet pairs and negative tweet pairs.</p>
<p>Because the problem is symmetric, the mechanism that encodes the first tweet should be reused (weights and all) to encode the second tweet. Here we use a shared LSTM layer to encode the tweets.</p>
<p>Let's build this with the functional API. We will take as input for a tweet a binary matrix of shape <code>(280, 256)</code>, i.e. a sequence of 280 vectors of size 256, where each dimension in the 256-dimensional vector encodes the presence/absence of a character (out of an alphabet of 256 frequent characters).</p>
<pre><code class="python">import keras
from keras.layers import Input, LSTM, Dense
from keras.models import Model
tweet_a = Input(shape=(280, 256))
tweet_b = Input(shape=(280, 256))
</code></pre>
<p>To share a layer across different inputs, simply instantiate the layer once, then call it on as many inputs as you want:</p>
<pre><code class="python"># This layer can take as input a matrix
# and will return a vector of size 64
shared_lstm = LSTM(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 = shared_lstm(tweet_a)
encoded_b = shared_lstm(tweet_b)
# We can then concatenate the two vectors:
merged_vector = keras.layers.concatenate([encoded_a, encoded_b], axis=-1)
# And add a logistic regression on top
predictions = Dense(1, activation='sigmoid')(merged_vector)
# We define a trainable model linking the
# tweet inputs to the predictions
model = Model(inputs=[tweet_a, tweet_b], outputs=predictions)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit([data_a, data_b], labels, epochs=10)
</code></pre>
<p>Let's pause to take a look at how to read the shared layer's output or output shape.</p>
<hr />
<h2 id="the-concept-of-layer-node">The concept of layer "node"</h2>
<p>Whenever you are calling a layer on some input, you are creating a new tensor (the output of the layer), and you are adding a "node" to the layer, linking the input tensor to the output tensor. When you are calling the same layer multiple times, that layer owns multiple nodes indexed as 0, 1, 2...</p>
<p>In previous versions of Keras, you could obtain the output tensor of a layer instance via <code>layer.get_output()</code>, or its output shape via <code>layer.output_shape</code>. You still can (except <code>get_output()</code> has been replaced by the property <code>output</code>). But what if a layer is connected to multiple inputs?</p>
<p>As long as a layer is only connected to one input, there is no confusion, and <code>.output</code> will return the one output of the layer:</p>
<pre><code class="python">a = Input(shape=(280, 256))
lstm = LSTM(32)
encoded_a = lstm(a)
assert lstm.output == encoded_a
</code></pre>
<p>Not so if the layer has multiple inputs:</p>
<pre><code class="python">a = Input(shape=(280, 256))
b = Input(shape=(280, 256))
lstm = LSTM(32)
encoded_a = lstm(a)
encoded_b = lstm(b)
lstm.output
</code></pre>
<pre><code>>> AttributeError: Layer lstm_1 has multiple inbound nodes,
hence the notion of "layer output" is ill-defined.
Use `get_output_at(node_index)` instead.
</code></pre>
<p>Okay then. The following works:</p>
<pre><code class="python">assert lstm.get_output_at(0) == encoded_a
assert lstm.get_output_at(1) == encoded_b
</code></pre>
<p>Simple enough, right?</p>
<p>The same is true for the properties <code>input_shape</code> and <code>output_shape</code>: as long as the layer has only one node, or as long as all nodes have the same input/output shape, then the notion of "layer output/input shape" is well defined, and that one shape will be returned by <code>layer.output_shape</code>/<code>layer.input_shape</code>. But if, for instance, you apply the same <code>Conv2D</code> layer to an input of shape <code>(32, 32, 3)</code>, and then to an input of shape <code>(64, 64, 3)</code>, the layer will have multiple input/output shapes, and you will have to fetch them by specifying the index of the node they belong to:</p>
<pre><code class="python">a = Input(shape=(32, 32, 3))
b = Input(shape=(64, 64, 3))
conv = Conv2D(16, (3, 3), padding='same')
conved_a = conv(a)
# Only one input so far, the following will work:
assert conv.input_shape == (None, 32, 32, 3)
conved_b = conv(b)
# now the `.input_shape` property wouldn't work, but this does:
assert conv.get_input_shape_at(0) == (None, 32, 32, 3)
assert conv.get_input_shape_at(1) == (None, 64, 64, 3)
</code></pre>
<hr />
<h2 id="more-examples">More examples</h2>
<p>Code examples are still the best way to get started, so here are a few more.</p>
<h3 id="inception-module">Inception module</h3>
<p>For more information about the Inception architecture, see <a href="http://arxiv.org/abs/1409.4842">Going Deeper with Convolutions</a>.</p>
<pre><code class="python">from keras.layers import Conv2D, MaxPooling2D, Input
input_img = Input(shape=(256, 256, 3))
tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)
tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)
tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)
output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)
</code></pre>
<h3 id="residual-connection-on-a-convolution-layer">Residual connection on a convolution layer</h3>
<p>For more information about residual networks, see <a href="http://arxiv.org/abs/1512.03385">Deep Residual Learning for Image Recognition</a>.</p>
<pre><code class="python">from keras.layers import Conv2D, Input
# input tensor for a 3-channel 256x256 image
x = Input(shape=(256, 256, 3))
# 3x3 conv with 3 output channels (same as input channels)
y = Conv2D(3, (3, 3), padding='same')(x)
# this returns x + y.
z = keras.layers.add([x, y])
</code></pre>
<h3 id="shared-vision-model">Shared vision model</h3>
<p>This model reuses the same image-processing module on two inputs, to classify whether two MNIST digits are the same digit or different digits.</p>
<pre><code class="python">from keras.layers import Conv2D, MaxPooling2D, Input, Dense, Flatten
from keras.models import Model
# First, define the vision modules
digit_input = Input(shape=(27, 27, 1))
x = Conv2D(64, (3, 3))(digit_input)
x = Conv2D(64, (3, 3))(x)
x = MaxPooling2D((2, 2))(x)
out = Flatten()(x)
vision_model = Model(digit_input, out)
# Then define the tell-digits-apart model
digit_a = Input(shape=(27, 27, 1))
digit_b = Input(shape=(27, 27, 1))
# The vision model will be shared, weights and all
out_a = vision_model(digit_a)
out_b = vision_model(digit_b)
concatenated = keras.layers.concatenate([out_a, out_b])
out = Dense(1, activation='sigmoid')(concatenated)
classification_model = Model([digit_a, digit_b], out)
</code></pre>
<h3 id="visual-question-answering-model">Visual question answering model</h3>
<p>This model can select the correct one-word answer when asked a natural-language question about a picture.</p>
<p>It works by encoding the question into a vector, encoding the image into a vector, concatenating the two, and training on top a logistic regression over some vocabulary of potential answers.</p>
<pre><code class="python">from keras.layers import Conv2D, MaxPooling2D, Flatten
from keras.layers import Input, LSTM, Embedding, Dense
from keras.models import Model, Sequential
# First, let's define a vision model using a Sequential model.
# This model will encode an image into a vector.
vision_model = Sequential()
vision_model.add(Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3)))
vision_model.add(Conv2D(64, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
vision_model.add(Conv2D(128, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
vision_model.add(Conv2D(256, (3, 3), activation='relu'))
vision_model.add(Conv2D(256, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Flatten())
# Now let's get a tensor with the output of our vision model:
image_input = Input(shape=(224, 224, 3))
encoded_image = vision_model(image_input)
# Next, let's define a language model to encode the question into a vector.
# Each question will be at most 100 words long,
# and we will index words as integers from 1 to 9999.
question_input = Input(shape=(100,), dtype='int32')
embedded_question = Embedding(input_dim=10000, output_dim=256, input_length=100)(question_input)
encoded_question = LSTM(256)(embedded_question)
# Let's concatenate the question vector and the image vector:
merged = keras.layers.concatenate([encoded_question, encoded_image])
# And let's train a logistic regression over 1000 words on top:
output = Dense(1000, activation='softmax')(merged)
# This is our final model:
vqa_model = Model(inputs=[image_input, question_input], outputs=output)
# The next stage would be training this model on actual data.
</code></pre>
<h3 id="video-question-answering-model">Video question answering model</h3>
<p>Now that we have trained our image QA model, we can quickly turn it into a video QA model. With appropriate training, you will be able to show it a short video (e.g. 100-frame human action) and ask a natural language question about the video (e.g. "what sport is the boy playing?" -> "football").</p>
<pre><code class="python">from keras.layers import TimeDistributed
video_input = Input(shape=(100, 224, 224, 3))
# This is our video encoded via the previously trained vision_model (weights are reused)
encoded_frame_sequence = TimeDistributed(vision_model)(video_input) # the output will be a sequence of vectors
encoded_video = LSTM(256)(encoded_frame_sequence) # the output will be a vector
# This is a model-level representation of the question encoder, reusing the same weights as before:
question_encoder = Model(inputs=question_input, outputs=encoded_question)
# Let's use it to encode the question:
video_question_input = Input(shape=(100,), dtype='int32')
encoded_video_question = question_encoder(video_question_input)
# And this is our video question answering model:
merged = keras.layers.concatenate([encoded_video, encoded_video_question])
output = Dense(1000, activation='softmax')(merged)
video_qa_model = Model(inputs=[video_input, video_question_input], outputs=output)
</code></pre>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="../faq/" class="btn btn-neutral float-right" title="FAQ">Next <span class="icon icon-circle-arrow-right"></span></a>
<a href="../sequential-model-guide/" class="btn btn-neutral" title="Guide to the Sequential model"><span class="icon icon-circle-arrow-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<!-- Copyright etc -->
</div>
Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<div class="rst-versions" role="note" aria-label="versions">
<span class="rst-current-version" data-toggle="rst-current-version">
<a href="http://github.com/keras-team/keras/" class="fa fa-github" style="float: left; color: #fcfcfc"> GitHub</a>
<span><a href="../sequential-model-guide/" style="color: #fcfcfc;">« Previous</a></span>
<span style="margin-left: 15px"><a href="../faq/" style="color: #fcfcfc">Next »</a></span>
</span>
</div>
<script>var base_url = '../..';</script>
<script src="../../js/theme.js" defer></script>
<script src="../../search/main.js" defer></script>
<script type="text/javascript" defer>
window.onload = function () {
SphinxRtdTheme.Navigation.enable(true);
};
</script>
</body>
</html>
|