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 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
|
<!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/layers/core/">
<link rel="shortcut icon" href="../../img/favicon.ico">
<title>Core Layers - 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 = "Core Layers";
var mkdocs_page_input_path = "layers/core.md";
var mkdocs_page_url = "/layers/core/";
</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>
<li class="toctree-l1"><a class="reference internal" href="../../getting-started/sequential-model-guide/">Guide to the Sequential model</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../getting-started/functional-api-guide/">Guide to the Functional API</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../getting-started/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 class="current">
<li class="toctree-l1"><a class="reference internal" href="../about-keras-layers/">About Keras layers</a>
</li>
<li class="toctree-l1 current"><a class="reference internal current" href="./">Core Layers</a>
<ul class="current">
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../convolutional/">Convolutional Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../pooling/">Pooling Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../local/">Locally-connected Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../recurrent/">Recurrent Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../embeddings/">Embedding Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../merge/">Merge Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../advanced-activations/">Advanced Activations Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../normalization/">Normalization Layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../noise/">Noise layers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../wrappers/">Layer wrappers</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../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>Layers »</li>
<li>Core Layers</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">
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L796">[source]</a></span></p>
<h3 id="dense">Dense</h3>
<pre><code class="python">keras.layers.Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)
</code></pre>
<p>Just your regular densely-connected NN layer.</p>
<p><code>Dense</code> implements the operation:
<code>output = activation(dot(input, kernel) + bias)</code>
where <code>activation</code> is the element-wise activation function
passed as the <code>activation</code> argument, <code>kernel</code> is a weights matrix
created by the layer, and <code>bias</code> is a bias vector created by the layer
(only applicable if <code>use_bias</code> is <code>True</code>).</p>
<p>Note: if the input to the layer has a rank greater than 2, then
it is flattened prior to the initial dot product with <code>kernel</code>.</p>
<p><strong>Example</strong></p>
<pre><code class="python"># as first layer in a sequential model:
model = Sequential()
model.add(Dense(32, input_shape=(16,)))
# now the model will take as input arrays of shape (*, 16)
# and output arrays of shape (*, 32)
# after the first layer, you don't need to specify
# the size of the input anymore:
model.add(Dense(32))
</code></pre>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>units</strong>: Positive integer, dimensionality of the output space.</li>
<li><strong>activation</strong>: Activation function to use
(see <a href="../../activations/">activations</a>).
If you don't specify anything, no activation is applied
(ie. "linear" activation: <code>a(x) = x</code>).</li>
<li><strong>use_bias</strong>: Boolean, whether the layer uses a bias vector.</li>
<li><strong>kernel_initializer</strong>: Initializer for the <code>kernel</code> weights matrix
(see <a href="../../initializers/">initializers</a>).</li>
<li><strong>bias_initializer</strong>: Initializer for the bias vector
(see <a href="../../initializers/">initializers</a>).</li>
<li><strong>kernel_regularizer</strong>: Regularizer function applied to
the <code>kernel</code> weights matrix
(see <a href="../../regularizers/">regularizer</a>).</li>
<li><strong>bias_regularizer</strong>: Regularizer function applied to the bias vector
(see <a href="../../regularizers/">regularizer</a>).</li>
<li><strong>activity_regularizer</strong>: Regularizer function applied to
the output of the layer (its "activation").
(see <a href="../../regularizers/">regularizer</a>).</li>
<li><strong>kernel_constraint</strong>: Constraint function applied to
the <code>kernel</code> weights matrix
(see <a href="../../constraints/">constraints</a>).</li>
<li><strong>bias_constraint</strong>: Constraint function applied to the bias vector
(see <a href="../../constraints/">constraints</a>).</li>
</ul>
<p><strong>Input shape</strong></p>
<p>nD tensor with shape: <code>(batch_size, ..., input_dim)</code>.
The most common situation would be
a 2D input with shape <code>(batch_size, input_dim)</code>.</p>
<p><strong>Output shape</strong></p>
<p>nD tensor with shape: <code>(batch_size, ..., units)</code>.
For instance, for a 2D input with shape <code>(batch_size, input_dim)</code>,
the output would have shape <code>(batch_size, units)</code>.</p>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L277">[source]</a></span></p>
<h3 id="activation">Activation</h3>
<pre><code class="python">keras.layers.Activation(activation)
</code></pre>
<p>Applies an activation function to an output.</p>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>activation</strong>: name of activation function to use
(see: <a href="../../activations/">activations</a>),
or alternatively, a Theano or TensorFlow operation.</li>
</ul>
<p><strong>Input shape</strong></p>
<p>Arbitrary. Use the keyword argument <code>input_shape</code>
(tuple of integers, does not include the samples axis)
when using this layer as the first layer in a model.</p>
<p><strong>Output shape</strong></p>
<p>Same shape as input.</p>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L81">[source]</a></span></p>
<h3 id="dropout">Dropout</h3>
<pre><code class="python">keras.layers.Dropout(rate, noise_shape=None, seed=None)
</code></pre>
<p>Applies Dropout to the input.</p>
<p>Dropout consists in randomly setting
a fraction <code>rate</code> of input units to 0 at each update during training time,
which helps prevent overfitting.</p>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>rate</strong>: float between 0 and 1. Fraction of the input units to drop.</li>
<li><strong>noise_shape</strong>: 1D integer tensor representing the shape of the
binary dropout mask that will be multiplied with the input.
For instance, if your inputs have shape
<code>(batch_size, timesteps, features)</code> and
you want the dropout mask to be the same for all timesteps,
you can use <code>noise_shape=(batch_size, 1, features)</code>.</li>
<li><strong>seed</strong>: A Python integer to use as random seed.</li>
</ul>
<p><strong>References</strong></p>
<ul>
<li><a href="http://www.jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf">Dropout: A Simple Way to Prevent Neural Networks from Overfitting</a></li>
</ul>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L462">[source]</a></span></p>
<h3 id="flatten">Flatten</h3>
<pre><code class="python">keras.layers.Flatten(data_format=None)
</code></pre>
<p>Flattens the input. Does not affect the batch size.</p>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>data_format</strong>: A string,
one of <code>channels_last</code> (default) or <code>channels_first</code>.
The ordering of the dimensions in the inputs.
The purpose of this argument is to preserve weight
ordering when switching a model from one data format
to another.
<code>channels_last</code> corresponds to inputs with shape
<code>(batch, ..., channels)</code> while <code>channels_first</code> corresponds to
inputs with shape <code>(batch, channels, ...)</code>.
It defaults to the <code>image_data_format</code> value found in your
Keras config file at <code>~/.keras/keras.json</code>.
If you never set it, then it will be "channels_last".</li>
</ul>
<p><strong>Example</strong></p>
<pre><code class="python">model = Sequential()
model.add(Conv2D(64, (3, 3),
input_shape=(3, 32, 32), padding='same',))
# now: model.output_shape == (None, 64, 32, 32)
model.add(Flatten())
# now: model.output_shape == (None, 65536)
</code></pre>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/engine/input_layer.py#L114">[source]</a></span></p>
<h3 id="input">Input</h3>
<pre><code class="python">keras.engine.input_layer.Input()
</code></pre>
<p><code>Input()</code> is used to instantiate a Keras tensor.</p>
<p>A Keras tensor is a tensor object from the underlying backend
(Theano, TensorFlow or CNTK), which we augment with certain
attributes that allow us to build a Keras model
just by knowing the inputs and outputs of the model.</p>
<p>For instance, if a, b and c are Keras tensors,
it becomes possible to do:
<code>model = Model(input=[a, b], output=c)</code></p>
<p>The added Keras attributes are:
<code>_keras_shape</code>: Integer shape tuple propagated
via Keras-side shape inference.
<code>_keras_history</code>: Last layer applied to the tensor.
the entire layer graph is retrievable from that layer,
recursively.</p>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>shape</strong>: A shape tuple (integer), not including the batch size.
For instance, <code>shape=(32,)</code> indicates that the expected input
will be batches of 32-dimensional vectors.</li>
<li><strong>batch_shape</strong>: A shape tuple (integer), including the batch size.
For instance, <code>batch_shape=(10, 32)</code> indicates that
the expected input will be batches of 10 32-dimensional vectors.
<code>batch_shape=(None, 32)</code> indicates batches of an arbitrary number
of 32-dimensional vectors.</li>
<li><strong>name</strong>: An optional name string for the layer.
Should be unique in a model (do not reuse the same name twice).
It will be autogenerated if it isn't provided.</li>
<li><strong>dtype</strong>: The data type expected by the input, as a string
(<code>float32</code>, <code>float64</code>, <code>int32</code>...)</li>
<li><strong>sparse</strong>: A boolean specifying whether the placeholder
to be created is sparse.</li>
<li><strong>tensor</strong>: Optional existing tensor to wrap into the <code>Input</code> layer.
If set, the layer will not create a placeholder tensor.</li>
</ul>
<p><strong>Returns</strong></p>
<p>A tensor.</p>
<p><strong>Example</strong></p>
<pre><code class="python"># this is a logistic regression in Keras
x = Input(shape=(32,))
y = Dense(16, activation='softmax')(x)
model = Model(x, y)
</code></pre>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L311">[source]</a></span></p>
<h3 id="reshape">Reshape</h3>
<pre><code class="python">keras.layers.Reshape(target_shape)
</code></pre>
<p>Reshapes an output to a certain shape.</p>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>target_shape</strong>: target shape. Tuple of integers.
Does not include the batch axis.</li>
</ul>
<p><strong>Input shape</strong></p>
<p>Arbitrary, although all dimensions in the input shaped must be fixed.
Use the keyword argument <code>input_shape</code>
(tuple of integers, does not include the batch axis)
when using this layer as the first layer in a model.</p>
<p><strong>Output shape</strong></p>
<p><code>(batch_size,) + target_shape</code></p>
<p><strong>Example</strong></p>
<pre><code class="python"># as first layer in a Sequential model
model = Sequential()
model.add(Reshape((3, 4), input_shape=(12,)))
# now: model.output_shape == (None, 3, 4)
# note: `None` is the batch dimension
# as intermediate layer in a Sequential model
model.add(Reshape((6, 2)))
# now: model.output_shape == (None, 6, 2)
# also supports shape inference using `-1` as dimension
model.add(Reshape((-1, 2, 2)))
# now: model.output_shape == (None, 3, 2, 2)
</code></pre>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L410">[source]</a></span></p>
<h3 id="permute">Permute</h3>
<pre><code class="python">keras.layers.Permute(dims)
</code></pre>
<p>Permutes the dimensions of the input according to a given pattern.</p>
<p>Useful for e.g. connecting RNNs and convnets together.</p>
<p><strong>Example</strong></p>
<pre><code class="python">model = Sequential()
model.add(Permute((2, 1), input_shape=(10, 64)))
# now: model.output_shape == (None, 64, 10)
# note: `None` is the batch dimension
</code></pre>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>dims</strong>: Tuple of integers. Permutation pattern, does not include the
samples dimension. Indexing starts at 1.
For instance, <code>(2, 1)</code> permutes the first and second dimension
of the input.</li>
</ul>
<p><strong>Input shape</strong></p>
<p>Arbitrary. Use the keyword argument <code>input_shape</code>
(tuple of integers, does not include the samples axis)
when using this layer as the first layer in a model.</p>
<p><strong>Output shape</strong></p>
<p>Same as the input shape, but with the dimensions re-ordered according
to the specified pattern.</p>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L524">[source]</a></span></p>
<h3 id="repeatvector">RepeatVector</h3>
<pre><code class="python">keras.layers.RepeatVector(n)
</code></pre>
<p>Repeats the input n times.</p>
<p><strong>Example</strong></p>
<pre><code class="python">model = Sequential()
model.add(Dense(32, input_dim=32))
# now: model.output_shape == (None, 32)
# note: `None` is the batch dimension
model.add(RepeatVector(3))
# now: model.output_shape == (None, 3, 32)
</code></pre>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>n</strong>: integer, repetition factor.</li>
</ul>
<p><strong>Input shape</strong></p>
<p>2D tensor of shape <code>(num_samples, features)</code>.</p>
<p><strong>Output shape</strong></p>
<p>3D tensor of shape <code>(num_samples, n, features)</code>.</p>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L566">[source]</a></span></p>
<h3 id="lambda">Lambda</h3>
<pre><code class="python">keras.layers.Lambda(function, output_shape=None, mask=None, arguments=None)
</code></pre>
<p>Wraps arbitrary expression as a <code>Layer</code> object.</p>
<p><strong>Examples</strong></p>
<pre><code class="python"># add a x -> x^2 layer
model.add(Lambda(lambda x: x ** 2))
</code></pre>
<pre><code class="python"># add a layer that returns the concatenation
# of the positive part of the input and
# the opposite of the negative part
def antirectifier(x):
x -= K.mean(x, axis=1, keepdims=True)
x = K.l2_normalize(x, axis=1)
pos = K.relu(x)
neg = K.relu(-x)
return K.concatenate([pos, neg], axis=1)
def antirectifier_output_shape(input_shape):
shape = list(input_shape)
assert len(shape) == 2 # only valid for 2D tensors
shape[-1] *= 2
return tuple(shape)
model.add(Lambda(antirectifier,
output_shape=antirectifier_output_shape))
</code></pre>
<pre><code class="python"># add a layer that returns the hadamard product
# and sum of it from two input tensors
def hadamard_product_sum(tensors):
out1 = tensors[0] * tensors[1]
out2 = K.sum(out1, axis=-1)
return [out1, out2]
def hadamard_product_sum_output_shape(input_shapes):
shape1 = list(input_shapes[0])
shape2 = list(input_shapes[1])
assert shape1 == shape2 # else hadamard product isn't possible
return [tuple(shape1), tuple(shape2[:-1])]
x1 = Dense(32)(input_1)
x2 = Dense(32)(input_2)
layer = Lambda(hadamard_product_sum, hadamard_product_sum_output_shape)
x_hadamard, x_sum = layer([x1, x2])
</code></pre>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>function</strong>: The function to be evaluated.
Takes input tensor or list of tensors as first argument.</li>
<li><strong>output_shape</strong>: Expected output shape from function.
Only relevant when using Theano.
Can be a tuple or function.
If a tuple, it only specifies the first dimension onward;
sample dimension is assumed either the same as the input:
<code>output_shape = (input_shape[0], ) + output_shape</code>
or, the input is <code>None</code> and
the sample dimension is also <code>None</code>:
<code>output_shape = (None, ) + output_shape</code>
If a function, it specifies the entire shape as a function of the
input shape: <code>output_shape = f(input_shape)</code></li>
<li><strong>mask</strong>: Either None (indicating no masking) or a Tensor indicating the
input mask for Embedding.</li>
<li><strong>arguments</strong>: optional dictionary of keyword arguments to be passed
to the function.</li>
</ul>
<p><strong>Input shape</strong></p>
<p>Arbitrary. Use the keyword argument input_shape
(tuple of integers, does not include the samples axis)
when using this layer as the first layer in a model.</p>
<p><strong>Output shape</strong></p>
<p>Specified by <code>output_shape</code> argument
(or auto-inferred when using TensorFlow or CNTK).</p>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L940">[source]</a></span></p>
<h3 id="activityregularization">ActivityRegularization</h3>
<pre><code class="python">keras.layers.ActivityRegularization(l1=0.0, l2=0.0)
</code></pre>
<p>Layer that applies an update to the cost function based input activity.</p>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>l1</strong>: L1 regularization factor (positive float).</li>
<li><strong>l2</strong>: L2 regularization factor (positive float).</li>
</ul>
<p><strong>Input shape</strong></p>
<p>Arbitrary. Use the keyword argument <code>input_shape</code>
(tuple of integers, does not include the samples axis)
when using this layer as the first layer in a model.</p>
<p><strong>Output shape</strong></p>
<p>Same shape as input.</p>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L28">[source]</a></span></p>
<h3 id="masking">Masking</h3>
<pre><code class="python">keras.layers.Masking(mask_value=0.0)
</code></pre>
<p>Masks a sequence by using a mask value to skip timesteps.</p>
<p>If all features for a given sample timestep are equal to <code>mask_value</code>,
then the sample timestep will be masked (skipped) in all downstream layers
(as long as they support masking).</p>
<p>If any downstream layer does not support masking yet receives such
an input mask, an exception will be raised.</p>
<p><strong>Example</strong></p>
<p>Consider a Numpy data array <code>x</code> of shape <code>(samples, timesteps, features)</code>,
to be fed to an LSTM layer.
You want to mask sample #0 at timestep #3, and sample #2 at timestep #5,
because you lack features for these sample timesteps. You can do:</p>
<ul>
<li>set <code>x[0, 3, :] = 0.</code> and <code>x[2, 5, :] = 0.</code></li>
<li>insert a <code>Masking</code> layer with <code>mask_value=0.</code> before the LSTM layer:</li>
</ul>
<pre><code class="python">model = Sequential()
model.add(Masking(mask_value=0., input_shape=(timesteps, features)))
model.add(LSTM(32))
</code></pre>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>mask_value</strong>: Either None or mask value to skip</li>
</ul>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L141">[source]</a></span></p>
<h3 id="spatialdropout1d">SpatialDropout1D</h3>
<pre><code class="python">keras.layers.SpatialDropout1D(rate)
</code></pre>
<p>Spatial 1D version of Dropout.</p>
<p>This version performs the same function as Dropout, however it drops
entire 1D feature maps instead of individual elements. If adjacent frames
within feature maps are strongly correlated (as is normally the case in
early convolution layers) then regular dropout will not regularize the
activations and will otherwise just result in an effective learning rate
decrease. In this case, SpatialDropout1D will help promote independence
between feature maps and should be used instead.</p>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>rate</strong>: float between 0 and 1. Fraction of the input units to drop.</li>
</ul>
<p><strong>Input shape</strong></p>
<p>3D tensor with shape:
<code>(samples, timesteps, channels)</code></p>
<p><strong>Output shape</strong></p>
<p>Same as input</p>
<p><strong>References</strong></p>
<ul>
<li><a href="https://arxiv.org/abs/1411.4280">Efficient Object Localization Using Convolutional Networks</a></li>
</ul>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L178">[source]</a></span></p>
<h3 id="spatialdropout2d">SpatialDropout2D</h3>
<pre><code class="python">keras.layers.SpatialDropout2D(rate, data_format=None)
</code></pre>
<p>Spatial 2D version of Dropout.</p>
<p>This version performs the same function as Dropout, however it drops
entire 2D feature maps instead of individual elements. If adjacent pixels
within feature maps are strongly correlated (as is normally the case in
early convolution layers) then regular dropout will not regularize the
activations and will otherwise just result in an effective learning rate
decrease. In this case, SpatialDropout2D will help promote independence
between feature maps and should be used instead.</p>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>rate</strong>: float between 0 and 1. Fraction of the input units to drop.</li>
<li><strong>data_format</strong>: 'channels_first' or 'channels_last'.
In 'channels_first' mode, the channels dimension
(the depth) is at index 1,
in 'channels_last' mode is it at index 3.
It defaults to the <code>image_data_format</code> value found in your
Keras config file at <code>~/.keras/keras.json</code>.
If you never set it, then it will be "channels_last".</li>
</ul>
<p><strong>Input shape</strong></p>
<p>4D tensor with shape:
<code>(samples, channels, rows, cols)</code> if data_format='channels_first'
or 4D tensor with shape:
<code>(samples, rows, cols, channels)</code> if data_format='channels_last'.</p>
<p><strong>Output shape</strong></p>
<p>Same as input</p>
<p><strong>References</strong></p>
<ul>
<li><a href="https://arxiv.org/abs/1411.4280">Efficient Object Localization Using Convolutional Networks</a></li>
</ul>
<hr />
<p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L228">[source]</a></span></p>
<h3 id="spatialdropout3d">SpatialDropout3D</h3>
<pre><code class="python">keras.layers.SpatialDropout3D(rate, data_format=None)
</code></pre>
<p>Spatial 3D version of Dropout.</p>
<p>This version performs the same function as Dropout, however it drops
entire 3D feature maps instead of individual elements. If adjacent voxels
within feature maps are strongly correlated (as is normally the case in
early convolution layers) then regular dropout will not regularize the
activations and will otherwise just result in an effective learning rate
decrease. In this case, SpatialDropout3D will help promote independence
between feature maps and should be used instead.</p>
<p><strong>Arguments</strong></p>
<ul>
<li><strong>rate</strong>: float between 0 and 1. Fraction of the input units to drop.</li>
<li><strong>data_format</strong>: 'channels_first' or 'channels_last'.
In 'channels_first' mode, the channels dimension (the depth)
is at index 1, in 'channels_last' mode is it at index 4.
It defaults to the <code>image_data_format</code> value found in your
Keras config file at <code>~/.keras/keras.json</code>.
If you never set it, then it will be "channels_last".</li>
</ul>
<p><strong>Input shape</strong></p>
<p>5D tensor with shape:
<code>(samples, channels, dim1, dim2, dim3)</code> if data_format='channels_first'
or 5D tensor with shape:
<code>(samples, dim1, dim2, dim3, channels)</code> if data_format='channels_last'.</p>
<p><strong>Output shape</strong></p>
<p>Same as input</p>
<p><strong>References</strong></p>
<ul>
<li><a href="https://arxiv.org/abs/1411.4280">Efficient Object Localization Using Convolutional Networks</a></li>
</ul>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="../convolutional/" class="btn btn-neutral float-right" title="Convolutional Layers">Next <span class="icon icon-circle-arrow-right"></span></a>
<a href="../about-keras-layers/" class="btn btn-neutral" title="About Keras layers"><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="../about-keras-layers/" style="color: #fcfcfc;">« Previous</a></span>
<span style="margin-left: 15px"><a href="../convolutional/" 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>
|