Home | History | Annotate | Download | only in layers
      1 # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 # =============================================================================
     15 
     16 # pylint: disable=unused-import,g-bad-import-order
     17 """Contains the convolutional layer classes and their functional aliases.
     18 """
     19 from __future__ import absolute_import
     20 from __future__ import division
     21 from __future__ import print_function
     22 
     23 from tensorflow.python.eager import context
     24 from tensorflow.python.framework import ops
     25 from tensorflow.python.framework import tensor_shape
     26 from tensorflow.python.layers import base
     27 from tensorflow.python.layers import utils
     28 from tensorflow.python.ops import array_ops
     29 from tensorflow.python.ops import init_ops
     30 from tensorflow.python.ops import nn
     31 from tensorflow.python.ops import nn_ops
     32 from tensorflow.python.util.tf_export import tf_export
     33 
     34 
     35 class _Conv(base.Layer):
     36   """Abstract nD convolution layer (private, used as implementation base).
     37 
     38   This layer creates a convolution kernel that is convolved
     39   (actually cross-correlated) with the layer input to produce a tensor of
     40   outputs. If `use_bias` is True (and a `bias_initializer` is provided),
     41   a bias vector is created and added to the outputs. Finally, if
     42   `activation` is not `None`, it is applied to the outputs as well.
     43 
     44   Arguments:
     45     rank: An integer, the rank of the convolution, e.g. "2" for 2D convolution.
     46     filters: Integer, the dimensionality of the output space (i.e. the number
     47       of filters in the convolution).
     48     kernel_size: An integer or tuple/list of n integers, specifying the
     49       length of the convolution window.
     50     strides: An integer or tuple/list of n integers,
     51       specifying the stride length of the convolution.
     52       Specifying any stride value != 1 is incompatible with specifying
     53       any `dilation_rate` value != 1.
     54     padding: One of `"valid"` or `"same"` (case-insensitive).
     55     data_format: A string, one of `channels_last` (default) or `channels_first`.
     56       The ordering of the dimensions in the inputs.
     57       `channels_last` corresponds to inputs with shape
     58       `(batch, ..., channels)` while `channels_first` corresponds to
     59       inputs with shape `(batch, channels, ...)`.
     60     dilation_rate: An integer or tuple/list of n integers, specifying
     61       the dilation rate to use for dilated convolution.
     62       Currently, specifying any `dilation_rate` value != 1 is
     63       incompatible with specifying any `strides` value != 1.
     64     activation: Activation function. Set it to None to maintain a
     65       linear activation.
     66     use_bias: Boolean, whether the layer uses a bias.
     67     kernel_initializer: An initializer for the convolution kernel.
     68     bias_initializer: An initializer for the bias vector. If None, the default
     69       initializer will be used.
     70     kernel_regularizer: Optional regularizer for the convolution kernel.
     71     bias_regularizer: Optional regularizer for the bias vector.
     72     activity_regularizer: Optional regularizer function for the output.
     73     kernel_constraint: Optional projection function to be applied to the
     74         kernel after being updated by an `Optimizer` (e.g. used to implement
     75         norm constraints or value constraints for layer weights). The function
     76         must take as input the unprojected variable and must return the
     77         projected variable (which must have the same shape). Constraints are
     78         not safe to use when doing asynchronous distributed training.
     79     bias_constraint: Optional projection function to be applied to the
     80         bias after being updated by an `Optimizer`.
     81     trainable: Boolean, if `True` also add variables to the graph collection
     82       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
     83     name: A string, the name of the layer.
     84   """
     85 
     86   def __init__(self, rank,
     87                filters,
     88                kernel_size,
     89                strides=1,
     90                padding='valid',
     91                data_format='channels_last',
     92                dilation_rate=1,
     93                activation=None,
     94                use_bias=True,
     95                kernel_initializer=None,
     96                bias_initializer=init_ops.zeros_initializer(),
     97                kernel_regularizer=None,
     98                bias_regularizer=None,
     99                activity_regularizer=None,
    100                kernel_constraint=None,
    101                bias_constraint=None,
    102                trainable=True,
    103                name=None,
    104                **kwargs):
    105     super(_Conv, self).__init__(trainable=trainable, name=name,
    106                                 activity_regularizer=activity_regularizer,
    107                                 **kwargs)
    108     self.rank = rank
    109     self.filters = filters
    110     self.kernel_size = utils.normalize_tuple(kernel_size, rank, 'kernel_size')
    111     self.strides = utils.normalize_tuple(strides, rank, 'strides')
    112     self.padding = utils.normalize_padding(padding)
    113     self.data_format = utils.normalize_data_format(data_format)
    114     self.dilation_rate = utils.normalize_tuple(
    115         dilation_rate, rank, 'dilation_rate')
    116     self.activation = activation
    117     self.use_bias = use_bias
    118     self.kernel_initializer = kernel_initializer
    119     self.bias_initializer = bias_initializer
    120     self.kernel_regularizer = kernel_regularizer
    121     self.bias_regularizer = bias_regularizer
    122     self.kernel_constraint = kernel_constraint
    123     self.bias_constraint = bias_constraint
    124     self.input_spec = base.InputSpec(ndim=self.rank + 2)
    125 
    126   def build(self, input_shape):
    127     input_shape = tensor_shape.TensorShape(input_shape)
    128     if self.data_format == 'channels_first':
    129       channel_axis = 1
    130     else:
    131       channel_axis = -1
    132     if input_shape[channel_axis].value is None:
    133       raise ValueError('The channel dimension of the inputs '
    134                        'should be defined. Found `None`.')
    135     input_dim = input_shape[channel_axis].value
    136     kernel_shape = self.kernel_size + (input_dim, self.filters)
    137 
    138     self.kernel = self.add_variable(name='kernel',
    139                                     shape=kernel_shape,
    140                                     initializer=self.kernel_initializer,
    141                                     regularizer=self.kernel_regularizer,
    142                                     constraint=self.kernel_constraint,
    143                                     trainable=True,
    144                                     dtype=self.dtype)
    145     if self.use_bias:
    146       self.bias = self.add_variable(name='bias',
    147                                     shape=(self.filters,),
    148                                     initializer=self.bias_initializer,
    149                                     regularizer=self.bias_regularizer,
    150                                     constraint=self.bias_constraint,
    151                                     trainable=True,
    152                                     dtype=self.dtype)
    153     else:
    154       self.bias = None
    155     self.input_spec = base.InputSpec(ndim=self.rank + 2,
    156                                      axes={channel_axis: input_dim})
    157     self._convolution_op = nn_ops.Convolution(
    158         input_shape,
    159         filter_shape=self.kernel.get_shape(),
    160         dilation_rate=self.dilation_rate,
    161         strides=self.strides,
    162         padding=self.padding.upper(),
    163         data_format=utils.convert_data_format(self.data_format,
    164                                               self.rank + 2))
    165     self.built = True
    166 
    167   def call(self, inputs):
    168     outputs = self._convolution_op(inputs, self.kernel)
    169 
    170     if self.use_bias:
    171       if self.data_format == 'channels_first':
    172         if self.rank == 1:
    173           # nn.bias_add does not accept a 1D input tensor.
    174           bias = array_ops.reshape(self.bias, (1, self.filters, 1))
    175           outputs += bias
    176         if self.rank == 2:
    177           outputs = nn.bias_add(outputs, self.bias, data_format='NCHW')
    178         if self.rank == 3:
    179           # As of Mar 2017, direct addition is significantly slower than
    180           # bias_add when computing gradients. To use bias_add, we collapse Z
    181           # and Y into a single dimension to obtain a 4D input tensor.
    182           outputs_shape = outputs.shape.as_list()
    183           outputs_4d = array_ops.reshape(outputs,
    184                                          [outputs_shape[0], outputs_shape[1],
    185                                           outputs_shape[2] * outputs_shape[3],
    186                                           outputs_shape[4]])
    187           outputs_4d = nn.bias_add(outputs_4d, self.bias, data_format='NCHW')
    188           outputs = array_ops.reshape(outputs_4d, outputs_shape)
    189       else:
    190         outputs = nn.bias_add(outputs, self.bias, data_format='NHWC')
    191 
    192     if self.activation is not None:
    193       return self.activation(outputs)
    194     return outputs
    195 
    196   def compute_output_shape(self, input_shape):
    197     input_shape = tensor_shape.TensorShape(input_shape).as_list()
    198     if self.data_format == 'channels_last':
    199       space = input_shape[1:-1]
    200       new_space = []
    201       for i in range(len(space)):
    202         new_dim = utils.conv_output_length(
    203             space[i],
    204             self.kernel_size[i],
    205             padding=self.padding,
    206             stride=self.strides[i],
    207             dilation=self.dilation_rate[i])
    208         new_space.append(new_dim)
    209       return tensor_shape.TensorShape([input_shape[0]] + new_space +
    210                                       [self.filters])
    211     else:
    212       space = input_shape[2:]
    213       new_space = []
    214       for i in range(len(space)):
    215         new_dim = utils.conv_output_length(
    216             space[i],
    217             self.kernel_size[i],
    218             padding=self.padding,
    219             stride=self.strides[i],
    220             dilation=self.dilation_rate[i])
    221         new_space.append(new_dim)
    222       return tensor_shape.TensorShape([input_shape[0], self.filters] +
    223                                       new_space)
    224 
    225 
    226 @tf_export('layers.Conv1D')
    227 class Conv1D(_Conv):
    228   """1D convolution layer (e.g. temporal convolution).
    229 
    230   This layer creates a convolution kernel that is convolved
    231   (actually cross-correlated) with the layer input to produce a tensor of
    232   outputs. If `use_bias` is True (and a `bias_initializer` is provided),
    233   a bias vector is created and added to the outputs. Finally, if
    234   `activation` is not `None`, it is applied to the outputs as well.
    235 
    236   Arguments:
    237     filters: Integer, the dimensionality of the output space (i.e. the number
    238       of filters in the convolution).
    239     kernel_size: An integer or tuple/list of a single integer, specifying the
    240       length of the 1D convolution window.
    241     strides: An integer or tuple/list of a single integer,
    242       specifying the stride length of the convolution.
    243       Specifying any stride value != 1 is incompatible with specifying
    244       any `dilation_rate` value != 1.
    245     padding: One of `"valid"` or `"same"` (case-insensitive).
    246     data_format: A string, one of `channels_last` (default) or `channels_first`.
    247       The ordering of the dimensions in the inputs.
    248       `channels_last` corresponds to inputs with shape
    249       `(batch, length, channels)` while `channels_first` corresponds to
    250       inputs with shape `(batch, channels, length)`.
    251     dilation_rate: An integer or tuple/list of a single integer, specifying
    252       the dilation rate to use for dilated convolution.
    253       Currently, specifying any `dilation_rate` value != 1 is
    254       incompatible with specifying any `strides` value != 1.
    255     activation: Activation function. Set it to None to maintain a
    256       linear activation.
    257     use_bias: Boolean, whether the layer uses a bias.
    258     kernel_initializer: An initializer for the convolution kernel.
    259     bias_initializer: An initializer for the bias vector. If None, the default
    260       initializer will be used.
    261     kernel_regularizer: Optional regularizer for the convolution kernel.
    262     bias_regularizer: Optional regularizer for the bias vector.
    263     activity_regularizer: Optional regularizer function for the output.
    264     kernel_constraint: Optional projection function to be applied to the
    265         kernel after being updated by an `Optimizer` (e.g. used to implement
    266         norm constraints or value constraints for layer weights). The function
    267         must take as input the unprojected variable and must return the
    268         projected variable (which must have the same shape). Constraints are
    269         not safe to use when doing asynchronous distributed training.
    270     bias_constraint: Optional projection function to be applied to the
    271         bias after being updated by an `Optimizer`.
    272     trainable: Boolean, if `True` also add variables to the graph collection
    273       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
    274     name: A string, the name of the layer.
    275   """
    276 
    277   def __init__(self, filters,
    278                kernel_size,
    279                strides=1,
    280                padding='valid',
    281                data_format='channels_last',
    282                dilation_rate=1,
    283                activation=None,
    284                use_bias=True,
    285                kernel_initializer=None,
    286                bias_initializer=init_ops.zeros_initializer(),
    287                kernel_regularizer=None,
    288                bias_regularizer=None,
    289                activity_regularizer=None,
    290                kernel_constraint=None,
    291                bias_constraint=None,
    292                trainable=True,
    293                name=None,
    294                **kwargs):
    295     super(Convolution1D, self).__init__(
    296         rank=1,
    297         filters=filters,
    298         kernel_size=kernel_size,
    299         strides=strides,
    300         padding=padding,
    301         data_format=data_format,
    302         dilation_rate=dilation_rate,
    303         activation=activation,
    304         use_bias=use_bias,
    305         kernel_initializer=kernel_initializer,
    306         bias_initializer=bias_initializer,
    307         kernel_regularizer=kernel_regularizer,
    308         bias_regularizer=bias_regularizer,
    309         activity_regularizer=activity_regularizer,
    310         kernel_constraint=kernel_constraint,
    311         bias_constraint=bias_constraint,
    312         trainable=trainable,
    313         name=name, **kwargs)
    314 
    315 
    316 @tf_export('layers.conv1d')
    317 def conv1d(inputs,
    318            filters,
    319            kernel_size,
    320            strides=1,
    321            padding='valid',
    322            data_format='channels_last',
    323            dilation_rate=1,
    324            activation=None,
    325            use_bias=True,
    326            kernel_initializer=None,
    327            bias_initializer=init_ops.zeros_initializer(),
    328            kernel_regularizer=None,
    329            bias_regularizer=None,
    330            activity_regularizer=None,
    331            kernel_constraint=None,
    332            bias_constraint=None,
    333            trainable=True,
    334            name=None,
    335            reuse=None):
    336   """Functional interface for 1D convolution layer (e.g. temporal convolution).
    337 
    338   This layer creates a convolution kernel that is convolved
    339   (actually cross-correlated) with the layer input to produce a tensor of
    340   outputs. If `use_bias` is True (and a `bias_initializer` is provided),
    341   a bias vector is created and added to the outputs. Finally, if
    342   `activation` is not `None`, it is applied to the outputs as well.
    343 
    344   Arguments:
    345     inputs: Tensor input.
    346     filters: Integer, the dimensionality of the output space (i.e. the number
    347       of filters in the convolution).
    348     kernel_size: An integer or tuple/list of a single integer, specifying the
    349       length of the 1D convolution window.
    350     strides: An integer or tuple/list of a single integer,
    351       specifying the stride length of the convolution.
    352       Specifying any stride value != 1 is incompatible with specifying
    353       any `dilation_rate` value != 1.
    354     padding: One of `"valid"` or `"same"` (case-insensitive).
    355     data_format: A string, one of `channels_last` (default) or `channels_first`.
    356       The ordering of the dimensions in the inputs.
    357       `channels_last` corresponds to inputs with shape
    358       `(batch, length, channels)` while `channels_first` corresponds to
    359       inputs with shape `(batch, channels, length)`.
    360     dilation_rate: An integer or tuple/list of a single integer, specifying
    361       the dilation rate to use for dilated convolution.
    362       Currently, specifying any `dilation_rate` value != 1 is
    363       incompatible with specifying any `strides` value != 1.
    364     activation: Activation function. Set it to None to maintain a
    365       linear activation.
    366     use_bias: Boolean, whether the layer uses a bias.
    367     kernel_initializer: An initializer for the convolution kernel.
    368     bias_initializer: An initializer for the bias vector. If None, the default
    369       initializer will be used.
    370     kernel_regularizer: Optional regularizer for the convolution kernel.
    371     bias_regularizer: Optional regularizer for the bias vector.
    372     activity_regularizer: Optional regularizer function for the output.
    373     kernel_constraint: Optional projection function to be applied to the
    374         kernel after being updated by an `Optimizer` (e.g. used to implement
    375         norm constraints or value constraints for layer weights). The function
    376         must take as input the unprojected variable and must return the
    377         projected variable (which must have the same shape). Constraints are
    378         not safe to use when doing asynchronous distributed training.
    379     bias_constraint: Optional projection function to be applied to the
    380         bias after being updated by an `Optimizer`.
    381     trainable: Boolean, if `True` also add variables to the graph collection
    382       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
    383     name: A string, the name of the layer.
    384     reuse: Boolean, whether to reuse the weights of a previous layer
    385       by the same name.
    386 
    387   Returns:
    388     Output tensor.
    389 
    390   Raises:
    391     ValueError: if eager execution is enabled.
    392   """
    393   layer = Conv1D(
    394       filters=filters,
    395       kernel_size=kernel_size,
    396       strides=strides,
    397       padding=padding,
    398       data_format=data_format,
    399       dilation_rate=dilation_rate,
    400       activation=activation,
    401       use_bias=use_bias,
    402       kernel_initializer=kernel_initializer,
    403       bias_initializer=bias_initializer,
    404       kernel_regularizer=kernel_regularizer,
    405       bias_regularizer=bias_regularizer,
    406       activity_regularizer=activity_regularizer,
    407       kernel_constraint=kernel_constraint,
    408       bias_constraint=bias_constraint,
    409       trainable=trainable,
    410       name=name,
    411       dtype=inputs.dtype.base_dtype,
    412       _reuse=reuse,
    413       _scope=name)
    414   return layer.apply(inputs)
    415 
    416 
    417 @tf_export('layers.Conv2D')
    418 class Conv2D(_Conv):
    419   """2D convolution layer (e.g. spatial convolution over images).
    420 
    421   This layer creates a convolution kernel that is convolved
    422   (actually cross-correlated) with the layer input to produce a tensor of
    423   outputs. If `use_bias` is True (and a `bias_initializer` is provided),
    424   a bias vector is created and added to the outputs. Finally, if
    425   `activation` is not `None`, it is applied to the outputs as well.
    426 
    427   Arguments:
    428     filters: Integer, the dimensionality of the output space (i.e. the number
    429       of filters in the convolution).
    430     kernel_size: An integer or tuple/list of 2 integers, specifying the
    431       height and width of the 2D convolution window.
    432       Can be a single integer to specify the same value for
    433       all spatial dimensions.
    434     strides: An integer or tuple/list of 2 integers,
    435       specifying the strides of the convolution along the height and width.
    436       Can be a single integer to specify the same value for
    437       all spatial dimensions.
    438       Specifying any stride value != 1 is incompatible with specifying
    439       any `dilation_rate` value != 1.
    440     padding: One of `"valid"` or `"same"` (case-insensitive).
    441     data_format: A string, one of `channels_last` (default) or `channels_first`.
    442       The ordering of the dimensions in the inputs.
    443       `channels_last` corresponds to inputs with shape
    444       `(batch, height, width, channels)` while `channels_first` corresponds to
    445       inputs with shape `(batch, channels, height, width)`.
    446 
    447     dilation_rate: An integer or tuple/list of 2 integers, specifying
    448       the dilation rate to use for dilated convolution.
    449       Can be a single integer to specify the same value for
    450       all spatial dimensions.
    451       Currently, specifying any `dilation_rate` value != 1 is
    452       incompatible with specifying any stride value != 1.
    453     activation: Activation function. Set it to None to maintain a
    454       linear activation.
    455     use_bias: Boolean, whether the layer uses a bias.
    456     kernel_initializer: An initializer for the convolution kernel.
    457     bias_initializer: An initializer for the bias vector. If None, the default
    458       initializer will be used.
    459     kernel_regularizer: Optional regularizer for the convolution kernel.
    460     bias_regularizer: Optional regularizer for the bias vector.
    461     activity_regularizer: Optional regularizer function for the output.
    462     kernel_constraint: Optional projection function to be applied to the
    463         kernel after being updated by an `Optimizer` (e.g. used to implement
    464         norm constraints or value constraints for layer weights). The function
    465         must take as input the unprojected variable and must return the
    466         projected variable (which must have the same shape). Constraints are
    467         not safe to use when doing asynchronous distributed training.
    468     bias_constraint: Optional projection function to be applied to the
    469         bias after being updated by an `Optimizer`.
    470     trainable: Boolean, if `True` also add variables to the graph collection
    471       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
    472     name: A string, the name of the layer.
    473   """
    474 
    475   def __init__(self, filters,
    476                kernel_size,
    477                strides=(1, 1),
    478                padding='valid',
    479                data_format='channels_last',
    480                dilation_rate=(1, 1),
    481                activation=None,
    482                use_bias=True,
    483                kernel_initializer=None,
    484                bias_initializer=init_ops.zeros_initializer(),
    485                kernel_regularizer=None,
    486                bias_regularizer=None,
    487                activity_regularizer=None,
    488                kernel_constraint=None,
    489                bias_constraint=None,
    490                trainable=True,
    491                name=None,
    492                **kwargs):
    493     super(Conv2D, self).__init__(
    494         rank=2,
    495         filters=filters,
    496         kernel_size=kernel_size,
    497         strides=strides,
    498         padding=padding,
    499         data_format=data_format,
    500         dilation_rate=dilation_rate,
    501         activation=activation,
    502         use_bias=use_bias,
    503         kernel_initializer=kernel_initializer,
    504         bias_initializer=bias_initializer,
    505         kernel_regularizer=kernel_regularizer,
    506         bias_regularizer=bias_regularizer,
    507         activity_regularizer=activity_regularizer,
    508         kernel_constraint=kernel_constraint,
    509         bias_constraint=bias_constraint,
    510         trainable=trainable,
    511         name=name, **kwargs)
    512 
    513 
    514 @tf_export('layers.conv2d')
    515 def conv2d(inputs,
    516            filters,
    517            kernel_size,
    518            strides=(1, 1),
    519            padding='valid',
    520            data_format='channels_last',
    521            dilation_rate=(1, 1),
    522            activation=None,
    523            use_bias=True,
    524            kernel_initializer=None,
    525            bias_initializer=init_ops.zeros_initializer(),
    526            kernel_regularizer=None,
    527            bias_regularizer=None,
    528            activity_regularizer=None,
    529            kernel_constraint=None,
    530            bias_constraint=None,
    531            trainable=True,
    532            name=None,
    533            reuse=None):
    534   """Functional interface for the 2D convolution layer.
    535 
    536   This layer creates a convolution kernel that is convolved
    537   (actually cross-correlated) with the layer input to produce a tensor of
    538   outputs. If `use_bias` is True (and a `bias_initializer` is provided),
    539   a bias vector is created and added to the outputs. Finally, if
    540   `activation` is not `None`, it is applied to the outputs as well.
    541 
    542   Arguments:
    543     inputs: Tensor input.
    544     filters: Integer, the dimensionality of the output space (i.e. the number
    545       of filters in the convolution).
    546     kernel_size: An integer or tuple/list of 2 integers, specifying the
    547       height and width of the 2D convolution window.
    548       Can be a single integer to specify the same value for
    549       all spatial dimensions.
    550     strides: An integer or tuple/list of 2 integers,
    551       specifying the strides of the convolution along the height and width.
    552       Can be a single integer to specify the same value for
    553       all spatial dimensions.
    554       Specifying any stride value != 1 is incompatible with specifying
    555       any `dilation_rate` value != 1.
    556     padding: One of `"valid"` or `"same"` (case-insensitive).
    557     data_format: A string, one of `channels_last` (default) or `channels_first`.
    558       The ordering of the dimensions in the inputs.
    559       `channels_last` corresponds to inputs with shape
    560       `(batch, height, width, channels)` while `channels_first` corresponds to
    561       inputs with shape `(batch, channels, height, width)`.
    562 
    563     dilation_rate: An integer or tuple/list of 2 integers, specifying
    564       the dilation rate to use for dilated convolution.
    565       Can be a single integer to specify the same value for
    566       all spatial dimensions.
    567       Currently, specifying any `dilation_rate` value != 1 is
    568       incompatible with specifying any stride value != 1.
    569     activation: Activation function. Set it to None to maintain a
    570       linear activation.
    571     use_bias: Boolean, whether the layer uses a bias.
    572     kernel_initializer: An initializer for the convolution kernel.
    573     bias_initializer: An initializer for the bias vector. If None, the default
    574       initializer will be used.
    575     kernel_regularizer: Optional regularizer for the convolution kernel.
    576     bias_regularizer: Optional regularizer for the bias vector.
    577     activity_regularizer: Optional regularizer function for the output.
    578     kernel_constraint: Optional projection function to be applied to the
    579         kernel after being updated by an `Optimizer` (e.g. used to implement
    580         norm constraints or value constraints for layer weights). The function
    581         must take as input the unprojected variable and must return the
    582         projected variable (which must have the same shape). Constraints are
    583         not safe to use when doing asynchronous distributed training.
    584     bias_constraint: Optional projection function to be applied to the
    585         bias after being updated by an `Optimizer`.
    586     trainable: Boolean, if `True` also add variables to the graph collection
    587       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
    588     name: A string, the name of the layer.
    589     reuse: Boolean, whether to reuse the weights of a previous layer
    590       by the same name.
    591 
    592   Returns:
    593     Output tensor.
    594 
    595   Raises:
    596     ValueError: if eager execution is enabled.
    597   """
    598   layer = Conv2D(
    599       filters=filters,
    600       kernel_size=kernel_size,
    601       strides=strides,
    602       padding=padding,
    603       data_format=data_format,
    604       dilation_rate=dilation_rate,
    605       activation=activation,
    606       use_bias=use_bias,
    607       kernel_initializer=kernel_initializer,
    608       bias_initializer=bias_initializer,
    609       kernel_regularizer=kernel_regularizer,
    610       bias_regularizer=bias_regularizer,
    611       activity_regularizer=activity_regularizer,
    612       kernel_constraint=kernel_constraint,
    613       bias_constraint=bias_constraint,
    614       trainable=trainable,
    615       name=name,
    616       dtype=inputs.dtype.base_dtype,
    617       _reuse=reuse,
    618       _scope=name)
    619   return layer.apply(inputs)
    620 
    621 
    622 @tf_export('layers.Conv3D')
    623 class Conv3D(_Conv):
    624   """3D convolution layer (e.g. spatial convolution over volumes).
    625 
    626   This layer creates a convolution kernel that is convolved
    627   (actually cross-correlated) with the layer input to produce a tensor of
    628   outputs. If `use_bias` is True (and a `bias_initializer` is provided),
    629   a bias vector is created and added to the outputs. Finally, if
    630   `activation` is not `None`, it is applied to the outputs as well.
    631 
    632   Arguments:
    633     filters: Integer, the dimensionality of the output space (i.e. the number
    634       of filters in the convolution).
    635     kernel_size: An integer or tuple/list of 3 integers, specifying the
    636       depth, height and width of the 3D convolution window.
    637       Can be a single integer to specify the same value for
    638       all spatial dimensions.
    639     strides: An integer or tuple/list of 3 integers,
    640       specifying the strides of the convolution along the depth,
    641       height and width.
    642       Can be a single integer to specify the same value for
    643       all spatial dimensions.
    644       Specifying any stride value != 1 is incompatible with specifying
    645       any `dilation_rate` value != 1.
    646     padding: One of `"valid"` or `"same"` (case-insensitive).
    647     data_format: A string, one of `channels_last` (default) or `channels_first`.
    648       The ordering of the dimensions in the inputs.
    649       `channels_last` corresponds to inputs with shape
    650       `(batch, depth, height, width, channels)` while `channels_first`
    651       corresponds to inputs with shape
    652       `(batch, channels, depth, height, width)`.
    653     dilation_rate: An integer or tuple/list of 3 integers, specifying
    654       the dilation rate to use for dilated convolution.
    655       Can be a single integer to specify the same value for
    656       all spatial dimensions.
    657       Currently, specifying any `dilation_rate` value != 1 is
    658       incompatible with specifying any stride value != 1.
    659     activation: Activation function. Set it to None to maintain a
    660       linear activation.
    661     use_bias: Boolean, whether the layer uses a bias.
    662     kernel_initializer: An initializer for the convolution kernel.
    663     bias_initializer: An initializer for the bias vector. If None, the default
    664       initializer will be used.
    665     kernel_regularizer: Optional regularizer for the convolution kernel.
    666     bias_regularizer: Optional regularizer for the bias vector.
    667     activity_regularizer: Optional regularizer function for the output.
    668     kernel_constraint: Optional projection function to be applied to the
    669         kernel after being updated by an `Optimizer` (e.g. used to implement
    670         norm constraints or value constraints for layer weights). The function
    671         must take as input the unprojected variable and must return the
    672         projected variable (which must have the same shape). Constraints are
    673         not safe to use when doing asynchronous distributed training.
    674     bias_constraint: Optional projection function to be applied to the
    675         bias after being updated by an `Optimizer`.
    676     trainable: Boolean, if `True` also add variables to the graph collection
    677       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
    678     name: A string, the name of the layer.
    679   """
    680 
    681   def __init__(self, filters,
    682                kernel_size,
    683                strides=(1, 1, 1),
    684                padding='valid',
    685                data_format='channels_last',
    686                dilation_rate=(1, 1, 1),
    687                activation=None,
    688                use_bias=True,
    689                kernel_initializer=None,
    690                bias_initializer=init_ops.zeros_initializer(),
    691                kernel_regularizer=None,
    692                bias_regularizer=None,
    693                activity_regularizer=None,
    694                kernel_constraint=None,
    695                bias_constraint=None,
    696                trainable=True,
    697                name=None,
    698                **kwargs):
    699     super(Conv3D, self).__init__(
    700         rank=3,
    701         filters=filters,
    702         kernel_size=kernel_size,
    703         strides=strides,
    704         padding=padding,
    705         data_format=data_format,
    706         dilation_rate=dilation_rate,
    707         activation=activation,
    708         use_bias=use_bias,
    709         kernel_initializer=kernel_initializer,
    710         bias_initializer=bias_initializer,
    711         kernel_regularizer=kernel_regularizer,
    712         bias_regularizer=bias_regularizer,
    713         activity_regularizer=activity_regularizer,
    714         kernel_constraint=kernel_constraint,
    715         bias_constraint=bias_constraint,
    716         trainable=trainable,
    717         name=name, **kwargs)
    718 
    719 
    720 @tf_export('layers.conv3d')
    721 def conv3d(inputs,
    722            filters,
    723            kernel_size,
    724            strides=(1, 1, 1),
    725            padding='valid',
    726            data_format='channels_last',
    727            dilation_rate=(1, 1, 1),
    728            activation=None,
    729            use_bias=True,
    730            kernel_initializer=None,
    731            bias_initializer=init_ops.zeros_initializer(),
    732            kernel_regularizer=None,
    733            bias_regularizer=None,
    734            activity_regularizer=None,
    735            kernel_constraint=None,
    736            bias_constraint=None,
    737            trainable=True,
    738            name=None,
    739            reuse=None):
    740   """Functional interface for the 3D convolution layer.
    741 
    742   This layer creates a convolution kernel that is convolved
    743   (actually cross-correlated) with the layer input to produce a tensor of
    744   outputs. If `use_bias` is True (and a `bias_initializer` is provided),
    745   a bias vector is created and added to the outputs. Finally, if
    746   `activation` is not `None`, it is applied to the outputs as well.
    747 
    748   Arguments:
    749     inputs: Tensor input.
    750     filters: Integer, the dimensionality of the output space (i.e. the number
    751       of filters in the convolution).
    752     kernel_size: An integer or tuple/list of 3 integers, specifying the
    753       depth, height and width of the 3D convolution window.
    754       Can be a single integer to specify the same value for
    755       all spatial dimensions.
    756     strides: An integer or tuple/list of 3 integers,
    757       specifying the strides of the convolution along the depth,
    758       height and width.
    759       Can be a single integer to specify the same value for
    760       all spatial dimensions.
    761       Specifying any stride value != 1 is incompatible with specifying
    762       any `dilation_rate` value != 1.
    763     padding: One of `"valid"` or `"same"` (case-insensitive).
    764     data_format: A string, one of `channels_last` (default) or `channels_first`.
    765       The ordering of the dimensions in the inputs.
    766       `channels_last` corresponds to inputs with shape
    767       `(batch, depth, height, width, channels)` while `channels_first`
    768       corresponds to inputs with shape
    769       `(batch, channels, depth, height, width)`.
    770     dilation_rate: An integer or tuple/list of 3 integers, specifying
    771       the dilation rate to use for dilated convolution.
    772       Can be a single integer to specify the same value for
    773       all spatial dimensions.
    774       Currently, specifying any `dilation_rate` value != 1 is
    775       incompatible with specifying any stride value != 1.
    776     activation: Activation function. Set it to None to maintain a
    777       linear activation.
    778     use_bias: Boolean, whether the layer uses a bias.
    779     kernel_initializer: An initializer for the convolution kernel.
    780     bias_initializer: An initializer for the bias vector. If None, the default
    781       initializer will be used.
    782     kernel_regularizer: Optional regularizer for the convolution kernel.
    783     bias_regularizer: Optional regularizer for the bias vector.
    784     activity_regularizer: Optional regularizer function for the output.
    785     kernel_constraint: Optional projection function to be applied to the
    786         kernel after being updated by an `Optimizer` (e.g. used to implement
    787         norm constraints or value constraints for layer weights). The function
    788         must take as input the unprojected variable and must return the
    789         projected variable (which must have the same shape). Constraints are
    790         not safe to use when doing asynchronous distributed training.
    791     bias_constraint: Optional projection function to be applied to the
    792         bias after being updated by an `Optimizer`.
    793     trainable: Boolean, if `True` also add variables to the graph collection
    794       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
    795     name: A string, the name of the layer.
    796     reuse: Boolean, whether to reuse the weights of a previous layer
    797       by the same name.
    798 
    799   Returns:
    800     Output tensor.
    801 
    802   Raises:
    803     ValueError: if eager execution is enabled.
    804   """
    805   layer = Conv3D(
    806       filters=filters,
    807       kernel_size=kernel_size,
    808       strides=strides,
    809       padding=padding,
    810       data_format=data_format,
    811       dilation_rate=dilation_rate,
    812       activation=activation,
    813       use_bias=use_bias,
    814       kernel_initializer=kernel_initializer,
    815       bias_initializer=bias_initializer,
    816       kernel_regularizer=kernel_regularizer,
    817       bias_regularizer=bias_regularizer,
    818       activity_regularizer=activity_regularizer,
    819       kernel_constraint=kernel_constraint,
    820       bias_constraint=bias_constraint,
    821       trainable=trainable,
    822       name=name,
    823       dtype=inputs.dtype.base_dtype,
    824       _reuse=reuse,
    825       _scope=name)
    826   return layer.apply(inputs)
    827 
    828 
    829 class _SeparableConv(_Conv):
    830   """Abstract base layer for separable nD convolution.
    831 
    832   This layer performs a depthwise convolution that acts separately on
    833   channels, followed by a pointwise convolution that mixes channels.
    834   If `use_bias` is True and a bias initializer is provided,
    835   it adds a bias vector to the output.
    836   It then optionally applies an activation function to produce the final output.
    837 
    838   Arguments:
    839     rank: An integer, the rank of the convolution, e.g. "2" for 2D convolution.
    840     filters: Integer, the dimensionality of the output space (i.e. the number
    841       of filters in the convolution).
    842     kernel_size: A tuple or list of integers specifying the spatial
    843       dimensions of the filters. Can be a single integer to specify the same
    844       value for all spatial dimensions.
    845     strides: A tuple or list of integers specifying the strides
    846       of the convolution. Can be a single integer to specify the same value for
    847       all spatial dimensions.
    848       Specifying any `stride` value != 1 is incompatible with specifying
    849       any `dilation_rate` value != 1.
    850     padding: One of `"valid"` or `"same"` (case-insensitive).
    851     data_format: A string, one of `channels_last` (default) or `channels_first`.
    852       The ordering of the dimensions in the inputs.
    853       `channels_last` corresponds to inputs with shape
    854       `(batch, ..., channels)` while `channels_first` corresponds to
    855       inputs with shape `(batch, channels, ...)`.
    856     dilation_rate: An integer or tuple/list of 2 integers, specifying
    857       the dilation rate to use for dilated convolution.
    858       Can be a single integer to specify the same value for
    859       all spatial dimensions.
    860       Currently, specifying any `dilation_rate` value != 1 is
    861       incompatible with specifying any stride value != 1.
    862     depth_multiplier: The number of depthwise convolution output channels for
    863       each input channel. The total number of depthwise convolution output
    864       channels will be equal to `num_filters_in * depth_multiplier`.
    865     activation: Activation function. Set it to None to maintain a
    866       linear activation.
    867     use_bias: Boolean, whether the layer uses a bias.
    868     depthwise_initializer: An initializer for the depthwise convolution kernel.
    869     pointwise_initializer: An initializer for the pointwise convolution kernel.
    870     bias_initializer: An initializer for the bias vector. If None, the default
    871       initializer will be used.
    872     depthwise_regularizer: Optional regularizer for the depthwise
    873       convolution kernel.
    874     pointwise_regularizer: Optional regularizer for the pointwise
    875       convolution kernel.
    876     bias_regularizer: Optional regularizer for the bias vector.
    877     activity_regularizer: Optional regularizer function for the output.
    878     depthwise_constraint: Optional projection function to be applied to the
    879         depthwise kernel after being updated by an `Optimizer` (e.g. used for
    880         norm constraints or value constraints for layer weights). The function
    881         must take as input the unprojected variable and must return the
    882         projected variable (which must have the same shape). Constraints are
    883         not safe to use when doing asynchronous distributed training.
    884     pointwise_constraint: Optional projection function to be applied to the
    885         pointwise kernel after being updated by an `Optimizer`.
    886     bias_constraint: Optional projection function to be applied to the
    887         bias after being updated by an `Optimizer`.
    888     trainable: Boolean, if `True` also add variables to the graph collection
    889       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
    890     name: A string, the name of the layer.
    891   """
    892 
    893   def __init__(self,
    894                rank,
    895                filters,
    896                kernel_size,
    897                strides=1,
    898                padding='valid',
    899                data_format='channels_last',
    900                dilation_rate=1,
    901                depth_multiplier=1,
    902                activation=None,
    903                use_bias=True,
    904                depthwise_initializer=None,
    905                pointwise_initializer=None,
    906                bias_initializer=init_ops.zeros_initializer(),
    907                depthwise_regularizer=None,
    908                pointwise_regularizer=None,
    909                bias_regularizer=None,
    910                activity_regularizer=None,
    911                depthwise_constraint=None,
    912                pointwise_constraint=None,
    913                bias_constraint=None,
    914                trainable=True,
    915                name=None,
    916                **kwargs):
    917     super(_SeparableConv, self).__init__(
    918         rank=rank,
    919         filters=filters,
    920         kernel_size=kernel_size,
    921         strides=strides,
    922         padding=padding,
    923         data_format=data_format,
    924         dilation_rate=dilation_rate,
    925         activation=activation,
    926         use_bias=use_bias,
    927         bias_regularizer=bias_regularizer,
    928         activity_regularizer=activity_regularizer,
    929         bias_constraint=bias_constraint,
    930         trainable=trainable,
    931         name=name,
    932         **kwargs)
    933     self.depth_multiplier = depth_multiplier
    934     self.depthwise_initializer = depthwise_initializer
    935     self.pointwise_initializer = pointwise_initializer
    936     self.depthwise_regularizer = depthwise_regularizer
    937     self.pointwise_regularizer = pointwise_regularizer
    938     self.depthwise_constraint = depthwise_constraint
    939     self.pointwise_constraint = pointwise_constraint
    940 
    941   def build(self, input_shape):
    942     input_shape = tensor_shape.TensorShape(input_shape)
    943     if self.data_format == 'channels_first':
    944       channel_axis = 1
    945     else:
    946       channel_axis = -1
    947     if input_shape[channel_axis].value is None:
    948       raise ValueError('The channel dimension of the inputs '
    949                        'should be defined. Found `None`.')
    950     input_dim = input_shape[channel_axis].value
    951     self.input_spec = base.InputSpec(ndim=self.rank + 2,
    952                                      axes={channel_axis: input_dim})
    953     depthwise_kernel_shape = self.kernel_size + (input_dim,
    954                                                  self.depth_multiplier)
    955     pointwise_kernel_shape = (
    956         1,) * self.rank + (self.depth_multiplier * input_dim, self.filters)
    957 
    958     self.depthwise_kernel = self.add_variable(
    959         name='depthwise_kernel',
    960         shape=depthwise_kernel_shape,
    961         initializer=self.depthwise_initializer,
    962         regularizer=self.depthwise_regularizer,
    963         constraint=self.depthwise_constraint,
    964         trainable=True,
    965         dtype=self.dtype)
    966     self.pointwise_kernel = self.add_variable(
    967         name='pointwise_kernel',
    968         shape=pointwise_kernel_shape,
    969         initializer=self.pointwise_initializer,
    970         regularizer=self.pointwise_regularizer,
    971         constraint=self.pointwise_constraint,
    972         trainable=True,
    973         dtype=self.dtype)
    974     if self.use_bias:
    975       self.bias = self.add_variable(name='bias',
    976                                     shape=(self.filters,),
    977                                     initializer=self.bias_initializer,
    978                                     regularizer=self.bias_regularizer,
    979                                     constraint=self.bias_constraint,
    980                                     trainable=True,
    981                                     dtype=self.dtype)
    982     else:
    983       self.bias = None
    984     self.built = True
    985 
    986   def call(self, inputs):
    987     raise NotImplementedError
    988 
    989 
    990 @tf_export('layers.SeparableConv1D')
    991 class SeparableConv1D(_SeparableConv):
    992   """Depthwise separable 1D convolution.
    993 
    994   This layer performs a depthwise convolution that acts separately on
    995   channels, followed by a pointwise convolution that mixes channels.
    996   If `use_bias` is True and a bias initializer is provided,
    997   it adds a bias vector to the output.
    998   It then optionally applies an activation function to produce the final output.
    999 
   1000   Arguments:
   1001     filters: Integer, the dimensionality of the output space (i.e. the number
   1002       of filters in the convolution).
   1003     kernel_size: A single integer specifying the spatial
   1004       dimensions of the filters.
   1005     strides: A single integer specifying the strides
   1006       of the convolution.
   1007       Specifying any `stride` value != 1 is incompatible with specifying
   1008       any `dilation_rate` value != 1.
   1009     padding: One of `"valid"` or `"same"` (case-insensitive).
   1010     data_format: A string, one of `channels_last` (default) or `channels_first`.
   1011       The ordering of the dimensions in the inputs.
   1012       `channels_last` corresponds to inputs with shape
   1013       `(batch, length, channels)` while `channels_first` corresponds to
   1014       inputs with shape `(batch, channels, length)`.
   1015     dilation_rate: A single integer, specifying
   1016       the dilation rate to use for dilated convolution.
   1017       Currently, specifying any `dilation_rate` value != 1 is
   1018       incompatible with specifying any stride value != 1.
   1019     depth_multiplier: The number of depthwise convolution output channels for
   1020       each input channel. The total number of depthwise convolution output
   1021       channels will be equal to `num_filters_in * depth_multiplier`.
   1022     activation: Activation function. Set it to None to maintain a
   1023       linear activation.
   1024     use_bias: Boolean, whether the layer uses a bias.
   1025     depthwise_initializer: An initializer for the depthwise convolution kernel.
   1026     pointwise_initializer: An initializer for the pointwise convolution kernel.
   1027     bias_initializer: An initializer for the bias vector. If None, the default
   1028       initializer will be used.
   1029     depthwise_regularizer: Optional regularizer for the depthwise
   1030       convolution kernel.
   1031     pointwise_regularizer: Optional regularizer for the pointwise
   1032       convolution kernel.
   1033     bias_regularizer: Optional regularizer for the bias vector.
   1034     activity_regularizer: Optional regularizer function for the output.
   1035     depthwise_constraint: Optional projection function to be applied to the
   1036         depthwise kernel after being updated by an `Optimizer` (e.g. used for
   1037         norm constraints or value constraints for layer weights). The function
   1038         must take as input the unprojected variable and must return the
   1039         projected variable (which must have the same shape). Constraints are
   1040         not safe to use when doing asynchronous distributed training.
   1041     pointwise_constraint: Optional projection function to be applied to the
   1042         pointwise kernel after being updated by an `Optimizer`.
   1043     bias_constraint: Optional projection function to be applied to the
   1044         bias after being updated by an `Optimizer`.
   1045     trainable: Boolean, if `True` also add variables to the graph collection
   1046       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
   1047     name: A string, the name of the layer.
   1048   """
   1049 
   1050   def __init__(self, filters,
   1051                kernel_size,
   1052                strides=1,
   1053                padding='valid',
   1054                data_format='channels_last',
   1055                dilation_rate=1,
   1056                depth_multiplier=1,
   1057                activation=None,
   1058                use_bias=True,
   1059                depthwise_initializer=None,
   1060                pointwise_initializer=None,
   1061                bias_initializer=init_ops.zeros_initializer(),
   1062                depthwise_regularizer=None,
   1063                pointwise_regularizer=None,
   1064                bias_regularizer=None,
   1065                activity_regularizer=None,
   1066                depthwise_constraint=None,
   1067                pointwise_constraint=None,
   1068                bias_constraint=None,
   1069                trainable=True,
   1070                name=None,
   1071                **kwargs):
   1072     super(SeparableConv1D, self).__init__(
   1073         rank=1,
   1074         filters=filters,
   1075         kernel_size=kernel_size,
   1076         strides=strides,
   1077         padding=padding,
   1078         data_format=data_format,
   1079         dilation_rate=dilation_rate,
   1080         depth_multiplier=depth_multiplier,
   1081         activation=activation,
   1082         use_bias=use_bias,
   1083         depthwise_initializer=depthwise_initializer,
   1084         pointwise_initializer=pointwise_initializer,
   1085         bias_initializer=bias_initializer,
   1086         depthwise_regularizer=depthwise_regularizer,
   1087         pointwise_regularizer=pointwise_regularizer,
   1088         bias_regularizer=bias_regularizer,
   1089         activity_regularizer=activity_regularizer,
   1090         depthwise_constraint=depthwise_constraint,
   1091         pointwise_constraint=pointwise_constraint,
   1092         bias_constraint=bias_constraint,
   1093         trainable=trainable,
   1094         name=name,
   1095         **kwargs)
   1096 
   1097   def call(self, inputs):
   1098     if self.data_format == 'channels_last':
   1099       strides = (1,) + self.strides * 2 + (1,)
   1100       spatial_start_dim = 1
   1101     else:
   1102       strides = (1, 1) + self.strides * 2
   1103       spatial_start_dim = 2
   1104 
   1105     # Explicitly broadcast inputs and kernels to 4D.
   1106     # TODO(fchollet): refactor when a native separable_conv1d op is available.
   1107     inputs = array_ops.expand_dims(inputs, spatial_start_dim)
   1108     depthwise_kernel = array_ops.expand_dims(self.depthwise_kernel, 0)
   1109     pointwise_kernel = array_ops.expand_dims(self.pointwise_kernel, 0)
   1110     dilation_rate = (1,) + self.dilation_rate
   1111 
   1112     outputs = nn.separable_conv2d(
   1113         inputs,
   1114         depthwise_kernel,
   1115         pointwise_kernel,
   1116         strides=strides,
   1117         padding=self.padding.upper(),
   1118         rate=dilation_rate,
   1119         data_format=utils.convert_data_format(self.data_format, ndim=4))
   1120 
   1121     if self.use_bias:
   1122       outputs = nn.bias_add(
   1123           outputs,
   1124           self.bias,
   1125           data_format=utils.convert_data_format(self.data_format, ndim=4))
   1126 
   1127     outputs = array_ops.squeeze(outputs, [spatial_start_dim])
   1128 
   1129     if self.activation is not None:
   1130       return self.activation(outputs)
   1131     return outputs
   1132 
   1133 
   1134 @tf_export('layers.SeparableConv2D')
   1135 class SeparableConv2D(_SeparableConv):
   1136   """Depthwise separable 2D convolution.
   1137 
   1138   This layer performs a depthwise convolution that acts separately on
   1139   channels, followed by a pointwise convolution that mixes channels.
   1140   If `use_bias` is True and a bias initializer is provided,
   1141   it adds a bias vector to the output.
   1142   It then optionally applies an activation function to produce the final output.
   1143 
   1144   Arguments:
   1145     filters: Integer, the dimensionality of the output space (i.e. the number
   1146       of filters in the convolution).
   1147     kernel_size: A tuple or list of 2 integers specifying the spatial
   1148       dimensions of the filters. Can be a single integer to specify the same
   1149       value for all spatial dimensions.
   1150     strides: A tuple or list of 2 positive integers specifying the strides
   1151       of the convolution. Can be a single integer to specify the same value for
   1152       all spatial dimensions.
   1153       Specifying any `stride` value != 1 is incompatible with specifying
   1154       any `dilation_rate` value != 1.
   1155     padding: One of `"valid"` or `"same"` (case-insensitive).
   1156     data_format: A string, one of `channels_last` (default) or `channels_first`.
   1157       The ordering of the dimensions in the inputs.
   1158       `channels_last` corresponds to inputs with shape
   1159       `(batch, height, width, channels)` while `channels_first` corresponds to
   1160       inputs with shape `(batch, channels, height, width)`.
   1161 
   1162     dilation_rate: An integer or tuple/list of 2 integers, specifying
   1163       the dilation rate to use for dilated convolution.
   1164       Can be a single integer to specify the same value for
   1165       all spatial dimensions.
   1166       Currently, specifying any `dilation_rate` value != 1 is
   1167       incompatible with specifying any stride value != 1.
   1168     depth_multiplier: The number of depthwise convolution output channels for
   1169       each input channel. The total number of depthwise convolution output
   1170       channels will be equal to `num_filters_in * depth_multiplier`.
   1171     activation: Activation function. Set it to None to maintain a
   1172       linear activation.
   1173     use_bias: Boolean, whether the layer uses a bias.
   1174     depthwise_initializer: An initializer for the depthwise convolution kernel.
   1175     pointwise_initializer: An initializer for the pointwise convolution kernel.
   1176     bias_initializer: An initializer for the bias vector. If None, the default
   1177       initializer will be used.
   1178     depthwise_regularizer: Optional regularizer for the depthwise
   1179       convolution kernel.
   1180     pointwise_regularizer: Optional regularizer for the pointwise
   1181       convolution kernel.
   1182     bias_regularizer: Optional regularizer for the bias vector.
   1183     activity_regularizer: Optional regularizer function for the output.
   1184     depthwise_constraint: Optional projection function to be applied to the
   1185         depthwise kernel after being updated by an `Optimizer` (e.g. used for
   1186         norm constraints or value constraints for layer weights). The function
   1187         must take as input the unprojected variable and must return the
   1188         projected variable (which must have the same shape). Constraints are
   1189         not safe to use when doing asynchronous distributed training.
   1190     pointwise_constraint: Optional projection function to be applied to the
   1191         pointwise kernel after being updated by an `Optimizer`.
   1192     bias_constraint: Optional projection function to be applied to the
   1193         bias after being updated by an `Optimizer`.
   1194     trainable: Boolean, if `True` also add variables to the graph collection
   1195       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
   1196     name: A string, the name of the layer.
   1197   """
   1198 
   1199   def __init__(self, filters,
   1200                kernel_size,
   1201                strides=(1, 1),
   1202                padding='valid',
   1203                data_format='channels_last',
   1204                dilation_rate=(1, 1),
   1205                depth_multiplier=1,
   1206                activation=None,
   1207                use_bias=True,
   1208                depthwise_initializer=None,
   1209                pointwise_initializer=None,
   1210                bias_initializer=init_ops.zeros_initializer(),
   1211                depthwise_regularizer=None,
   1212                pointwise_regularizer=None,
   1213                bias_regularizer=None,
   1214                activity_regularizer=None,
   1215                depthwise_constraint=None,
   1216                pointwise_constraint=None,
   1217                bias_constraint=None,
   1218                trainable=True,
   1219                name=None,
   1220                **kwargs):
   1221     super(SeparableConv2D, self).__init__(
   1222         rank=2,
   1223         filters=filters,
   1224         kernel_size=kernel_size,
   1225         strides=strides,
   1226         padding=padding,
   1227         data_format=data_format,
   1228         dilation_rate=dilation_rate,
   1229         depth_multiplier=depth_multiplier,
   1230         activation=activation,
   1231         use_bias=use_bias,
   1232         depthwise_initializer=depthwise_initializer,
   1233         pointwise_initializer=pointwise_initializer,
   1234         bias_initializer=bias_initializer,
   1235         depthwise_regularizer=depthwise_regularizer,
   1236         pointwise_regularizer=pointwise_regularizer,
   1237         bias_regularizer=bias_regularizer,
   1238         activity_regularizer=activity_regularizer,
   1239         depthwise_constraint=depthwise_constraint,
   1240         pointwise_constraint=pointwise_constraint,
   1241         bias_constraint=bias_constraint,
   1242         trainable=trainable,
   1243         name=name,
   1244         **kwargs)
   1245 
   1246   def call(self, inputs):
   1247     # Apply the actual ops.
   1248     if self.data_format == 'channels_last':
   1249       strides = (1,) + self.strides + (1,)
   1250     else:
   1251       strides = (1, 1) + self.strides
   1252     outputs = nn.separable_conv2d(
   1253         inputs,
   1254         self.depthwise_kernel,
   1255         self.pointwise_kernel,
   1256         strides=strides,
   1257         padding=self.padding.upper(),
   1258         rate=self.dilation_rate,
   1259         data_format=utils.convert_data_format(self.data_format, ndim=4))
   1260 
   1261     if self.use_bias:
   1262       outputs = nn.bias_add(
   1263           outputs,
   1264           self.bias,
   1265           data_format=utils.convert_data_format(self.data_format, ndim=4))
   1266 
   1267     if self.activation is not None:
   1268       return self.activation(outputs)
   1269     return outputs
   1270 
   1271 
   1272 @tf_export('layers.separable_conv1d')
   1273 def separable_conv1d(inputs,
   1274                      filters,
   1275                      kernel_size,
   1276                      strides=1,
   1277                      padding='valid',
   1278                      data_format='channels_last',
   1279                      dilation_rate=1,
   1280                      depth_multiplier=1,
   1281                      activation=None,
   1282                      use_bias=True,
   1283                      depthwise_initializer=None,
   1284                      pointwise_initializer=None,
   1285                      bias_initializer=init_ops.zeros_initializer(),
   1286                      depthwise_regularizer=None,
   1287                      pointwise_regularizer=None,
   1288                      bias_regularizer=None,
   1289                      activity_regularizer=None,
   1290                      depthwise_constraint=None,
   1291                      pointwise_constraint=None,
   1292                      bias_constraint=None,
   1293                      trainable=True,
   1294                      name=None,
   1295                      reuse=None):
   1296   """Functional interface for the depthwise separable 1D convolution layer.
   1297 
   1298   This layer performs a depthwise convolution that acts separately on
   1299   channels, followed by a pointwise convolution that mixes channels.
   1300   If `use_bias` is True and a bias initializer is provided,
   1301   it adds a bias vector to the output.
   1302   It then optionally applies an activation function to produce the final output.
   1303 
   1304   Arguments:
   1305     inputs: Input tensor.
   1306     filters: Integer, the dimensionality of the output space (i.e. the number
   1307       of filters in the convolution).
   1308     kernel_size: A single integer specifying the spatial
   1309       dimensions of the filters.
   1310     strides: A single integer specifying the strides
   1311       of the convolution.
   1312       Specifying any `stride` value != 1 is incompatible with specifying
   1313       any `dilation_rate` value != 1.
   1314     padding: One of `"valid"` or `"same"` (case-insensitive).
   1315     data_format: A string, one of `channels_last` (default) or `channels_first`.
   1316       The ordering of the dimensions in the inputs.
   1317       `channels_last` corresponds to inputs with shape
   1318       `(batch, length, channels)` while `channels_first` corresponds to
   1319       inputs with shape `(batch, channels, length)`.
   1320     dilation_rate: A single integer, specifying
   1321       the dilation rate to use for dilated convolution.
   1322       Currently, specifying any `dilation_rate` value != 1 is
   1323       incompatible with specifying any stride value != 1.
   1324     depth_multiplier: The number of depthwise convolution output channels for
   1325       each input channel. The total number of depthwise convolution output
   1326       channels will be equal to `num_filters_in * depth_multiplier`.
   1327     activation: Activation function. Set it to None to maintain a
   1328       linear activation.
   1329     use_bias: Boolean, whether the layer uses a bias.
   1330     depthwise_initializer: An initializer for the depthwise convolution kernel.
   1331     pointwise_initializer: An initializer for the pointwise convolution kernel.
   1332     bias_initializer: An initializer for the bias vector. If None, the default
   1333       initializer will be used.
   1334     depthwise_regularizer: Optional regularizer for the depthwise
   1335       convolution kernel.
   1336     pointwise_regularizer: Optional regularizer for the pointwise
   1337       convolution kernel.
   1338     bias_regularizer: Optional regularizer for the bias vector.
   1339     activity_regularizer: Optional regularizer function for the output.
   1340     depthwise_constraint: Optional projection function to be applied to the
   1341         depthwise kernel after being updated by an `Optimizer` (e.g. used for
   1342         norm constraints or value constraints for layer weights). The function
   1343         must take as input the unprojected variable and must return the
   1344         projected variable (which must have the same shape). Constraints are
   1345         not safe to use when doing asynchronous distributed training.
   1346     pointwise_constraint: Optional projection function to be applied to the
   1347         pointwise kernel after being updated by an `Optimizer`.
   1348     bias_constraint: Optional projection function to be applied to the
   1349         bias after being updated by an `Optimizer`.
   1350     trainable: Boolean, if `True` also add variables to the graph collection
   1351       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
   1352     name: A string, the name of the layer.
   1353     reuse: Boolean, whether to reuse the weights of a previous layer
   1354       by the same name.
   1355 
   1356   Returns:
   1357     Output tensor.
   1358 
   1359   Raises:
   1360     ValueError: if eager execution is enabled.
   1361   """
   1362   layer = SeparableConv1D(
   1363       filters=filters,
   1364       kernel_size=kernel_size,
   1365       strides=strides,
   1366       padding=padding,
   1367       data_format=data_format,
   1368       dilation_rate=dilation_rate,
   1369       depth_multiplier=depth_multiplier,
   1370       activation=activation,
   1371       use_bias=use_bias,
   1372       depthwise_initializer=depthwise_initializer,
   1373       pointwise_initializer=pointwise_initializer,
   1374       bias_initializer=bias_initializer,
   1375       depthwise_regularizer=depthwise_regularizer,
   1376       pointwise_regularizer=pointwise_regularizer,
   1377       bias_regularizer=bias_regularizer,
   1378       activity_regularizer=activity_regularizer,
   1379       depthwise_constraint=depthwise_constraint,
   1380       pointwise_constraint=pointwise_constraint,
   1381       bias_constraint=bias_constraint,
   1382       trainable=trainable,
   1383       name=name,
   1384       _reuse=reuse,
   1385       _scope=name)
   1386   return layer.apply(inputs)
   1387 
   1388 
   1389 @tf_export('layers.separable_conv2d')
   1390 def separable_conv2d(inputs,
   1391                      filters,
   1392                      kernel_size,
   1393                      strides=(1, 1),
   1394                      padding='valid',
   1395                      data_format='channels_last',
   1396                      dilation_rate=(1, 1),
   1397                      depth_multiplier=1,
   1398                      activation=None,
   1399                      use_bias=True,
   1400                      depthwise_initializer=None,
   1401                      pointwise_initializer=None,
   1402                      bias_initializer=init_ops.zeros_initializer(),
   1403                      depthwise_regularizer=None,
   1404                      pointwise_regularizer=None,
   1405                      bias_regularizer=None,
   1406                      activity_regularizer=None,
   1407                      depthwise_constraint=None,
   1408                      pointwise_constraint=None,
   1409                      bias_constraint=None,
   1410                      trainable=True,
   1411                      name=None,
   1412                      reuse=None):
   1413   """Functional interface for the depthwise separable 2D convolution layer.
   1414 
   1415   This layer performs a depthwise convolution that acts separately on
   1416   channels, followed by a pointwise convolution that mixes channels.
   1417   If `use_bias` is True and a bias initializer is provided,
   1418   it adds a bias vector to the output.
   1419   It then optionally applies an activation function to produce the final output.
   1420 
   1421   Arguments:
   1422     inputs: Input tensor.
   1423     filters: Integer, the dimensionality of the output space (i.e. the number
   1424       of filters in the convolution).
   1425     kernel_size: A tuple or list of 2 integers specifying the spatial
   1426       dimensions of the filters. Can be a single integer to specify the same
   1427       value for all spatial dimensions.
   1428     strides: A tuple or list of 2 positive integers specifying the strides
   1429       of the convolution. Can be a single integer to specify the same value for
   1430       all spatial dimensions.
   1431       Specifying any `stride` value != 1 is incompatible with specifying
   1432       any `dilation_rate` value != 1.
   1433     padding: One of `"valid"` or `"same"` (case-insensitive).
   1434     data_format: A string, one of `channels_last` (default) or `channels_first`.
   1435       The ordering of the dimensions in the inputs.
   1436       `channels_last` corresponds to inputs with shape
   1437       `(batch, height, width, channels)` while `channels_first` corresponds to
   1438       inputs with shape `(batch, channels, height, width)`.
   1439 
   1440     dilation_rate: An integer or tuple/list of 2 integers, specifying
   1441       the dilation rate to use for dilated convolution.
   1442       Can be a single integer to specify the same value for
   1443       all spatial dimensions.
   1444       Currently, specifying any `dilation_rate` value != 1 is
   1445       incompatible with specifying any stride value != 1.
   1446     depth_multiplier: The number of depthwise convolution output channels for
   1447       each input channel. The total number of depthwise convolution output
   1448       channels will be equal to `num_filters_in * depth_multiplier`.
   1449     activation: Activation function. Set it to None to maintain a
   1450       linear activation.
   1451     use_bias: Boolean, whether the layer uses a bias.
   1452     depthwise_initializer: An initializer for the depthwise convolution kernel.
   1453     pointwise_initializer: An initializer for the pointwise convolution kernel.
   1454     bias_initializer: An initializer for the bias vector. If None, the default
   1455       initializer will be used.
   1456     depthwise_regularizer: Optional regularizer for the depthwise
   1457       convolution kernel.
   1458     pointwise_regularizer: Optional regularizer for the pointwise
   1459       convolution kernel.
   1460     bias_regularizer: Optional regularizer for the bias vector.
   1461     activity_regularizer: Optional regularizer function for the output.
   1462     depthwise_constraint: Optional projection function to be applied to the
   1463         depthwise kernel after being updated by an `Optimizer` (e.g. used for
   1464         norm constraints or value constraints for layer weights). The function
   1465         must take as input the unprojected variable and must return the
   1466         projected variable (which must have the same shape). Constraints are
   1467         not safe to use when doing asynchronous distributed training.
   1468     pointwise_constraint: Optional projection function to be applied to the
   1469         pointwise kernel after being updated by an `Optimizer`.
   1470     bias_constraint: Optional projection function to be applied to the
   1471         bias after being updated by an `Optimizer`.
   1472     trainable: Boolean, if `True` also add variables to the graph collection
   1473       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
   1474     name: A string, the name of the layer.
   1475     reuse: Boolean, whether to reuse the weights of a previous layer
   1476       by the same name.
   1477 
   1478   Returns:
   1479     Output tensor.
   1480 
   1481   Raises:
   1482     ValueError: if eager execution is enabled.
   1483   """
   1484   layer = SeparableConv2D(
   1485       filters=filters,
   1486       kernel_size=kernel_size,
   1487       strides=strides,
   1488       padding=padding,
   1489       data_format=data_format,
   1490       dilation_rate=dilation_rate,
   1491       depth_multiplier=depth_multiplier,
   1492       activation=activation,
   1493       use_bias=use_bias,
   1494       depthwise_initializer=depthwise_initializer,
   1495       pointwise_initializer=pointwise_initializer,
   1496       bias_initializer=bias_initializer,
   1497       depthwise_regularizer=depthwise_regularizer,
   1498       pointwise_regularizer=pointwise_regularizer,
   1499       bias_regularizer=bias_regularizer,
   1500       activity_regularizer=activity_regularizer,
   1501       depthwise_constraint=depthwise_constraint,
   1502       pointwise_constraint=pointwise_constraint,
   1503       bias_constraint=bias_constraint,
   1504       trainable=trainable,
   1505       name=name,
   1506       _reuse=reuse,
   1507       _scope=name)
   1508   return layer.apply(inputs)
   1509 
   1510 
   1511 @tf_export('layers.Conv2DTranspose')
   1512 class Conv2DTranspose(Conv2D):
   1513   """Transposed 2D convolution layer (sometimes called 2D Deconvolution).
   1514 
   1515   The need for transposed convolutions generally arises
   1516   from the desire to use a transformation going in the opposite direction
   1517   of a normal convolution, i.e., from something that has the shape of the
   1518   output of some convolution to something that has the shape of its input
   1519   while maintaining a connectivity pattern that is compatible with
   1520   said convolution.
   1521 
   1522   Arguments:
   1523     filters: Integer, the dimensionality of the output space (i.e. the number
   1524       of filters in the convolution).
   1525     kernel_size: A tuple or list of 2 positive integers specifying the spatial
   1526       dimensions of the filters. Can be a single integer to specify the same
   1527       value for all spatial dimensions.
   1528     strides: A tuple or list of 2 positive integers specifying the strides
   1529       of the convolution. Can be a single integer to specify the same value for
   1530       all spatial dimensions.
   1531     padding: one of `"valid"` or `"same"` (case-insensitive).
   1532     data_format: A string, one of `channels_last` (default) or `channels_first`.
   1533       The ordering of the dimensions in the inputs.
   1534       `channels_last` corresponds to inputs with shape
   1535       `(batch, height, width, channels)` while `channels_first` corresponds to
   1536       inputs with shape `(batch, channels, height, width)`.
   1537     activation: Activation function. Set it to None to maintain a
   1538       linear activation.
   1539     use_bias: Boolean, whether the layer uses a bias.
   1540     kernel_initializer: An initializer for the convolution kernel.
   1541     bias_initializer: An initializer for the bias vector. If None, the default
   1542       initializer will be used.
   1543     kernel_regularizer: Optional regularizer for the convolution kernel.
   1544     bias_regularizer: Optional regularizer for the bias vector.
   1545     activity_regularizer: Optional regularizer function for the output.
   1546     kernel_constraint: Optional projection function to be applied to the
   1547         kernel after being updated by an `Optimizer` (e.g. used to implement
   1548         norm constraints or value constraints for layer weights). The function
   1549         must take as input the unprojected variable and must return the
   1550         projected variable (which must have the same shape). Constraints are
   1551         not safe to use when doing asynchronous distributed training.
   1552     bias_constraint: Optional projection function to be applied to the
   1553         bias after being updated by an `Optimizer`.
   1554     trainable: Boolean, if `True` also add variables to the graph collection
   1555       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
   1556     name: A string, the name of the layer.
   1557   """
   1558 
   1559   def __init__(self, filters,
   1560                kernel_size,
   1561                strides=(1, 1),
   1562                padding='valid',
   1563                data_format='channels_last',
   1564                activation=None,
   1565                use_bias=True,
   1566                kernel_initializer=None,
   1567                bias_initializer=init_ops.zeros_initializer(),
   1568                kernel_regularizer=None,
   1569                bias_regularizer=None,
   1570                activity_regularizer=None,
   1571                kernel_constraint=None,
   1572                bias_constraint=None,
   1573                trainable=True,
   1574                name=None,
   1575                **kwargs):
   1576     super(Conv2DTranspose, self).__init__(
   1577         filters,
   1578         kernel_size,
   1579         strides=strides,
   1580         padding=padding,
   1581         data_format=data_format,
   1582         activation=activation,
   1583         use_bias=use_bias,
   1584         kernel_initializer=kernel_initializer,
   1585         bias_initializer=bias_initializer,
   1586         kernel_regularizer=kernel_regularizer,
   1587         bias_regularizer=bias_regularizer,
   1588         activity_regularizer=activity_regularizer,
   1589         kernel_constraint=kernel_constraint,
   1590         bias_constraint=bias_constraint,
   1591         trainable=trainable,
   1592         name=name,
   1593         **kwargs)
   1594     self.input_spec = base.InputSpec(ndim=4)
   1595 
   1596   def build(self, input_shape):
   1597     if len(input_shape) != 4:
   1598       raise ValueError('Inputs should have rank 4. Received input shape: ' +
   1599                        str(input_shape))
   1600     if self.data_format == 'channels_first':
   1601       channel_axis = 1
   1602     else:
   1603       channel_axis = -1
   1604     if input_shape[channel_axis] is None:
   1605       raise ValueError('The channel dimension of the inputs '
   1606                        'should be defined. Found `None`.')
   1607     input_dim = input_shape[channel_axis]
   1608     self.input_spec = base.InputSpec(ndim=4, axes={channel_axis: input_dim})
   1609     kernel_shape = self.kernel_size + (self.filters, input_dim)
   1610 
   1611     self.kernel = self.add_variable(name='kernel',
   1612                                     shape=kernel_shape,
   1613                                     initializer=self.kernel_initializer,
   1614                                     regularizer=self.kernel_regularizer,
   1615                                     constraint=self.kernel_constraint,
   1616                                     trainable=True,
   1617                                     dtype=self.dtype)
   1618     if self.use_bias:
   1619       self.bias = self.add_variable(name='bias',
   1620                                     shape=(self.filters,),
   1621                                     initializer=self.bias_initializer,
   1622                                     regularizer=self.bias_regularizer,
   1623                                     constraint=self.bias_constraint,
   1624                                     trainable=True,
   1625                                     dtype=self.dtype)
   1626     else:
   1627       self.bias = None
   1628     self.built = True
   1629 
   1630   def call(self, inputs):
   1631     inputs_shape = array_ops.shape(inputs)
   1632     batch_size = inputs_shape[0]
   1633     if self.data_format == 'channels_first':
   1634       c_axis, h_axis, w_axis = 1, 2, 3
   1635     else:
   1636       c_axis, h_axis, w_axis = 3, 1, 2
   1637 
   1638     height, width = inputs_shape[h_axis], inputs_shape[w_axis]
   1639     kernel_h, kernel_w = self.kernel_size
   1640     stride_h, stride_w = self.strides
   1641 
   1642     # Infer the dynamic output shape:
   1643     out_height = utils.deconv_output_length(height,
   1644                                             kernel_h,
   1645                                             self.padding,
   1646                                             stride_h)
   1647     out_width = utils.deconv_output_length(width,
   1648                                            kernel_w,
   1649                                            self.padding,
   1650                                            stride_w)
   1651     if self.data_format == 'channels_first':
   1652       output_shape = (batch_size, self.filters, out_height, out_width)
   1653       strides = (1, 1, stride_h, stride_w)
   1654     else:
   1655       output_shape = (batch_size, out_height, out_width, self.filters)
   1656       strides = (1, stride_h, stride_w, 1)
   1657 
   1658     output_shape_tensor = array_ops.stack(output_shape)
   1659     outputs = nn.conv2d_transpose(
   1660         inputs,
   1661         self.kernel,
   1662         output_shape_tensor,
   1663         strides,
   1664         padding=self.padding.upper(),
   1665         data_format=utils.convert_data_format(self.data_format, ndim=4))
   1666 
   1667     if context.in_graph_mode():
   1668       # Infer the static output shape:
   1669       out_shape = inputs.get_shape().as_list()
   1670       out_shape[c_axis] = self.filters
   1671       out_shape[h_axis] = utils.deconv_output_length(out_shape[h_axis],
   1672                                                      kernel_h,
   1673                                                      self.padding,
   1674                                                      stride_h)
   1675       out_shape[w_axis] = utils.deconv_output_length(out_shape[w_axis],
   1676                                                      kernel_w,
   1677                                                      self.padding,
   1678                                                      stride_w)
   1679       outputs.set_shape(out_shape)
   1680 
   1681     if self.use_bias:
   1682       outputs = nn.bias_add(
   1683           outputs,
   1684           self.bias,
   1685           data_format=utils.convert_data_format(self.data_format, ndim=4))
   1686 
   1687     if self.activation is not None:
   1688       return self.activation(outputs)
   1689     return outputs
   1690 
   1691   def compute_output_shape(self, input_shape):
   1692     input_shape = tensor_shape.TensorShape(input_shape).as_list()
   1693     output_shape = list(input_shape)
   1694     if self.data_format == 'channels_first':
   1695       c_axis, h_axis, w_axis = 1, 2, 3
   1696     else:
   1697       c_axis, h_axis, w_axis = 3, 1, 2
   1698 
   1699     kernel_h, kernel_w = self.kernel_size
   1700     stride_h, stride_w = self.strides
   1701 
   1702     output_shape[c_axis] = self.filters
   1703     output_shape[h_axis] = utils.deconv_output_length(
   1704         output_shape[h_axis], kernel_h, self.padding, stride_h)
   1705     output_shape[w_axis] = utils.deconv_output_length(
   1706         output_shape[w_axis], kernel_w, self.padding, stride_w)
   1707     return tensor_shape.TensorShape(output_shape)
   1708 
   1709 
   1710 @tf_export('layers.conv2d_transpose')
   1711 def conv2d_transpose(inputs,
   1712                      filters,
   1713                      kernel_size,
   1714                      strides=(1, 1),
   1715                      padding='valid',
   1716                      data_format='channels_last',
   1717                      activation=None,
   1718                      use_bias=True,
   1719                      kernel_initializer=None,
   1720                      bias_initializer=init_ops.zeros_initializer(),
   1721                      kernel_regularizer=None,
   1722                      bias_regularizer=None,
   1723                      activity_regularizer=None,
   1724                      kernel_constraint=None,
   1725                      bias_constraint=None,
   1726                      trainable=True,
   1727                      name=None,
   1728                      reuse=None):
   1729   """Functional interface for transposed 2D convolution layer.
   1730 
   1731   The need for transposed convolutions generally arises
   1732   from the desire to use a transformation going in the opposite direction
   1733   of a normal convolution, i.e., from something that has the shape of the
   1734   output of some convolution to something that has the shape of its input
   1735   while maintaining a connectivity pattern that is compatible with
   1736   said convolution.
   1737 
   1738   Arguments:
   1739     inputs: Input tensor.
   1740     filters: Integer, the dimensionality of the output space (i.e. the number
   1741       of filters in the convolution).
   1742     kernel_size: A tuple or list of 2 positive integers specifying the spatial
   1743       dimensions of the filters. Can be a single integer to specify the same
   1744       value for all spatial dimensions.
   1745     strides: A tuple or list of 2 positive integers specifying the strides
   1746       of the convolution. Can be a single integer to specify the same value for
   1747       all spatial dimensions.
   1748     padding: one of `"valid"` or `"same"` (case-insensitive).
   1749     data_format: A string, one of `channels_last` (default) or `channels_first`.
   1750       The ordering of the dimensions in the inputs.
   1751       `channels_last` corresponds to inputs with shape
   1752       `(batch, height, width, channels)` while `channels_first` corresponds to
   1753       inputs with shape `(batch, channels, height, width)`.
   1754     activation: Activation function. Set it to `None` to maintain a
   1755       linear activation.
   1756     use_bias: Boolean, whether the layer uses a bias.
   1757     kernel_initializer: An initializer for the convolution kernel.
   1758     bias_initializer: An initializer for the bias vector. If `None`, the default
   1759       initializer will be used.
   1760     kernel_regularizer: Optional regularizer for the convolution kernel.
   1761     bias_regularizer: Optional regularizer for the bias vector.
   1762     activity_regularizer: Optional regularizer function for the output.
   1763     kernel_constraint: Optional projection function to be applied to the
   1764         kernel after being updated by an `Optimizer` (e.g. used to implement
   1765         norm constraints or value constraints for layer weights). The function
   1766         must take as input the unprojected variable and must return the
   1767         projected variable (which must have the same shape). Constraints are
   1768         not safe to use when doing asynchronous distributed training.
   1769     bias_constraint: Optional projection function to be applied to the
   1770         bias after being updated by an `Optimizer`.
   1771     trainable: Boolean, if `True` also add variables to the graph collection
   1772       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
   1773     name: A string, the name of the layer.
   1774     reuse: Boolean, whether to reuse the weights of a previous layer
   1775       by the same name.
   1776 
   1777   Returns:
   1778     Output tensor.
   1779 
   1780   Raises:
   1781     ValueError: if eager execution is enabled.
   1782   """
   1783   layer = Conv2DTranspose(
   1784       filters=filters,
   1785       kernel_size=kernel_size,
   1786       strides=strides,
   1787       padding=padding,
   1788       data_format=data_format,
   1789       activation=activation,
   1790       use_bias=use_bias,
   1791       kernel_initializer=kernel_initializer,
   1792       bias_initializer=bias_initializer,
   1793       kernel_regularizer=kernel_regularizer,
   1794       bias_regularizer=bias_regularizer,
   1795       activity_regularizer=activity_regularizer,
   1796       kernel_constraint=kernel_constraint,
   1797       bias_constraint=bias_constraint,
   1798       trainable=trainable,
   1799       name=name,
   1800       dtype=inputs.dtype.base_dtype,
   1801       _reuse=reuse,
   1802       _scope=name)
   1803   return layer.apply(inputs)
   1804 
   1805 
   1806 @tf_export('layers.Conv3DTranspose')
   1807 class Conv3DTranspose(Conv3D):
   1808   """Transposed 3D convolution layer (sometimes called 3D Deconvolution).
   1809 
   1810   Arguments:
   1811     filters: Integer, the dimensionality of the output space (i.e. the number
   1812       of filters in the convolution).
   1813     kernel_size: An integer or tuple/list of 3 integers, specifying the
   1814       depth, height and width of the 3D convolution window.
   1815       Can be a single integer to specify the same value for all spatial
   1816       dimensions.
   1817     strides: An integer or tuple/list of 3 integers, specifying the strides
   1818       of the convolution along the depth, height and width.
   1819       Can be a single integer to specify the same value for all spatial
   1820       dimensions.
   1821     padding: One of `"valid"` or `"same"` (case-insensitive).
   1822     data_format: A string, one of `channels_last` (default) or `channels_first`.
   1823       The ordering of the dimensions in the inputs.
   1824       `channels_last` corresponds to inputs with shape
   1825       `(batch, depth, height, width, channels)` while `channels_first`
   1826       corresponds to inputs with shape
   1827       `(batch, channels, depth, height, width)`.
   1828     activation: Activation function. Set it to `None` to maintain a
   1829       linear activation.
   1830     use_bias: Boolean, whether the layer uses a bias.
   1831     kernel_initializer: An initializer for the convolution kernel.
   1832     bias_initializer: An initializer for the bias vector. If `None`, the default
   1833       initializer will be used.
   1834     kernel_regularizer: Optional regularizer for the convolution kernel.
   1835     bias_regularizer: Optional regularizer for the bias vector.
   1836     activity_regularizer: Optional regularizer function for the output.
   1837     kernel_constraint: Optional projection function to be applied to the
   1838         kernel after being updated by an `Optimizer` (e.g. used to implement
   1839         norm constraints or value constraints for layer weights). The function
   1840         must take as input the unprojected variable and must return the
   1841         projected variable (which must have the same shape). Constraints are
   1842         not safe to use when doing asynchronous distributed training.
   1843     bias_constraint: Optional projection function to be applied to the
   1844         bias after being updated by an `Optimizer`.
   1845     trainable: Boolean, if `True` also add variables to the graph collection
   1846       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
   1847     name: A string, the name of the layer.
   1848   """
   1849 
   1850   def __init__(self,
   1851                filters,
   1852                kernel_size,
   1853                strides=(1, 1, 1),
   1854                padding='valid',
   1855                data_format='channels_last',
   1856                activation=None,
   1857                use_bias=True,
   1858                kernel_initializer=None,
   1859                bias_initializer=init_ops.zeros_initializer(),
   1860                kernel_regularizer=None,
   1861                bias_regularizer=None,
   1862                activity_regularizer=None,
   1863                kernel_constraint=None,
   1864                bias_constraint=None,
   1865                trainable=True,
   1866                name=None,
   1867                **kwargs):
   1868     super(Conv3DTranspose, self).__init__(
   1869         filters=filters,
   1870         kernel_size=kernel_size,
   1871         strides=strides,
   1872         padding=padding,
   1873         data_format=data_format,
   1874         activation=activation,
   1875         use_bias=use_bias,
   1876         kernel_initializer=kernel_initializer,
   1877         bias_initializer=bias_initializer,
   1878         kernel_regularizer=kernel_regularizer,
   1879         bias_regularizer=bias_regularizer,
   1880         activity_regularizer=activity_regularizer,
   1881         kernel_constraint=kernel_constraint,
   1882         bias_constraint=bias_constraint,
   1883         trainable=trainable,
   1884         name=name,
   1885         **kwargs)
   1886     self.input_spec = base.InputSpec(ndim=5)
   1887 
   1888   def build(self, input_shape):
   1889     if len(input_shape) != 5:
   1890       raise ValueError('Inputs should have rank 5, received input shape:',
   1891                        str(input_shape))
   1892     if self.data_format == 'channels_first':
   1893       channel_axis = 1
   1894     else:
   1895       channel_axis = -1
   1896     if input_shape[channel_axis] is None:
   1897       raise ValueError('The channel dimension of the inputs '
   1898                        'should be defined, found None: ' + str(input_shape))
   1899     input_dim = input_shape[channel_axis]
   1900     kernel_shape = self.kernel_size + (self.filters, input_dim)
   1901 
   1902     self.kernel = self.add_variable(
   1903         'kernel',
   1904         shape=kernel_shape,
   1905         initializer=self.kernel_initializer,
   1906         regularizer=self.kernel_regularizer,
   1907         constraint=self.kernel_constraint,
   1908         trainable=True,
   1909         dtype=self.dtype)
   1910     if self.use_bias:
   1911       self.bias = self.add_variable(
   1912           'bias',
   1913           shape=(self.filters,),
   1914           initializer=self.bias_initializer,
   1915           regularizer=self.bias_regularizer,
   1916           constraint=self.bias_constraint,
   1917           trainable=True,
   1918           dtype=self.dtype)
   1919     else:
   1920       self.bias = None
   1921     self.built = True
   1922 
   1923   def call(self, inputs):
   1924     inputs_shape = array_ops.shape(inputs)
   1925     batch_size = inputs_shape[0]
   1926     if self.data_format == 'channels_first':
   1927       c_axis, d_axis, h_axis, w_axis = 1, 2, 3, 4
   1928     else:
   1929       c_axis, d_axis, h_axis, w_axis = 4, 1, 2, 3
   1930 
   1931     self.input_spec = base.InputSpec(ndim=5,
   1932                                      axes={c_axis: inputs_shape[c_axis]})
   1933 
   1934     depth = inputs_shape[d_axis]
   1935     height = inputs_shape[h_axis]
   1936     width = inputs_shape[w_axis]
   1937 
   1938     kernel_d, kernel_h, kernel_w = self.kernel_size
   1939     stride_d, stride_h, stride_w = self.strides
   1940 
   1941     # Infer the dynamic output shape:
   1942     out_depth = utils.deconv_output_length(depth,
   1943                                            kernel_d,
   1944                                            self.padding,
   1945                                            stride_d)
   1946     out_height = utils.deconv_output_length(height,
   1947                                             kernel_h,
   1948                                             self.padding,
   1949                                             stride_h)
   1950     out_width = utils.deconv_output_length(width,
   1951                                            kernel_w,
   1952                                            self.padding,
   1953                                            stride_w)
   1954     if self.data_format == 'channels_first':
   1955       output_shape = (batch_size, self.filters, out_depth, out_height,
   1956                       out_width)
   1957       strides = (1, 1, stride_d, stride_h, stride_w)
   1958     else:
   1959       output_shape = (batch_size, out_depth, out_height, out_width,
   1960                       self.filters)
   1961       strides = (1, stride_d, stride_h, stride_w, 1)
   1962 
   1963     output_shape_tensor = array_ops.stack(output_shape)
   1964     outputs = nn.conv3d_transpose(
   1965         inputs,
   1966         self.kernel,
   1967         output_shape_tensor,
   1968         strides,
   1969         data_format=utils.convert_data_format(self.data_format, ndim=5),
   1970         padding=self.padding.upper())
   1971 
   1972     if context.in_graph_mode():
   1973       # Infer the static output shape:
   1974       out_shape = inputs.get_shape().as_list()
   1975       out_shape[c_axis] = self.filters
   1976       out_shape[d_axis] = utils.deconv_output_length(out_shape[d_axis],
   1977                                                      kernel_d,
   1978                                                      self.padding,
   1979                                                      stride_d)
   1980       out_shape[h_axis] = utils.deconv_output_length(out_shape[h_axis],
   1981                                                      kernel_h,
   1982                                                      self.padding,
   1983                                                      stride_h)
   1984       out_shape[w_axis] = utils.deconv_output_length(out_shape[w_axis],
   1985                                                      kernel_w,
   1986                                                      self.padding,
   1987                                                      stride_w)
   1988       outputs.set_shape(out_shape)
   1989 
   1990     if self.use_bias:
   1991       outputs_shape = outputs.shape.as_list()
   1992       if outputs_shape[0] is None:
   1993         outputs_shape[0] = -1
   1994       if self.data_format == 'channels_first':
   1995         outputs_4d = array_ops.reshape(outputs, [
   1996             outputs_shape[0], outputs_shape[1],
   1997             outputs_shape[2] * outputs_shape[3], outputs_shape[4]
   1998         ])
   1999       else:
   2000         outputs_4d = array_ops.reshape(outputs, [
   2001             outputs_shape[0], outputs_shape[1] * outputs_shape[2],
   2002             outputs_shape[3], outputs_shape[4]
   2003         ])
   2004       outputs_4d = nn.bias_add(
   2005           outputs_4d,
   2006           self.bias,
   2007           data_format=utils.convert_data_format(self.data_format, ndim=4))
   2008       outputs = array_ops.reshape(outputs_4d, outputs_shape)
   2009 
   2010     if self.activation is not None:
   2011       return self.activation(outputs)
   2012     return outputs
   2013 
   2014   def compute_output_shape(self, input_shape):
   2015     input_shape = tensor_shape.TensorShape(input_shape).as_list()
   2016     output_shape = list(input_shape)
   2017     if self.data_format == 'channels_first':
   2018       c_axis, d_axis, h_axis, w_axis = 1, 2, 3, 4
   2019     else:
   2020       c_axis, d_axis, h_axis, w_axis = 4, 1, 2, 3
   2021 
   2022     kernel_d, kernel_h, kernel_w = self.kernel_size
   2023     stride_d, stride_h, stride_w = self.strides
   2024 
   2025     output_shape[c_axis] = self.filters
   2026     output_shape[d_axis] = utils.deconv_output_length(
   2027         output_shape[d_axis], kernel_d, self.padding, stride_d)
   2028     output_shape[h_axis] = utils.deconv_output_length(
   2029         output_shape[h_axis], kernel_h, self.padding, stride_h)
   2030     output_shape[w_axis] = utils.deconv_output_length(
   2031         output_shape[w_axis], kernel_w, self.padding, stride_w)
   2032     return tensor_shape.TensorShape(output_shape)
   2033 
   2034 
   2035 @tf_export('layers.conv3d_transpose')
   2036 def conv3d_transpose(inputs,
   2037                      filters,
   2038                      kernel_size,
   2039                      strides=(1, 1, 1),
   2040                      padding='valid',
   2041                      data_format='channels_last',
   2042                      activation=None,
   2043                      use_bias=True,
   2044                      kernel_initializer=None,
   2045                      bias_initializer=init_ops.zeros_initializer(),
   2046                      kernel_regularizer=None,
   2047                      bias_regularizer=None,
   2048                      activity_regularizer=None,
   2049                      kernel_constraint=None,
   2050                      bias_constraint=None,
   2051                      trainable=True,
   2052                      name=None,
   2053                      reuse=None):
   2054   """Functional interface for transposed 3D convolution layer.
   2055 
   2056   Arguments:
   2057     inputs: Input tensor.
   2058     filters: Integer, the dimensionality of the output space (i.e. the number
   2059       of filters in the convolution).
   2060     kernel_size: A tuple or list of 3 positive integers specifying the spatial
   2061       dimensions of the filters. Can be a single integer to specify the same
   2062       value for all spatial dimensions.
   2063     strides: A tuple or list of 3 positive integers specifying the strides
   2064       of the convolution. Can be a single integer to specify the same value for
   2065       all spatial dimensions.
   2066     padding: one of `"valid"` or `"same"` (case-insensitive).
   2067     data_format: A string, one of `channels_last` (default) or `channels_first`.
   2068       The ordering of the dimensions in the inputs.
   2069       `channels_last` corresponds to inputs with shape
   2070       `(batch, depth, height, width, channels)` while `channels_first`
   2071       corresponds to inputs with shape
   2072       `(batch, channels, depth, height, width)`.
   2073     activation: Activation function. Set it to None to maintain a
   2074       linear activation.
   2075     use_bias: Boolean, whether the layer uses a bias.
   2076     kernel_initializer: An initializer for the convolution kernel.
   2077     bias_initializer: An initializer for the bias vector. If None, the default
   2078       initializer will be used.
   2079     kernel_regularizer: Optional regularizer for the convolution kernel.
   2080     bias_regularizer: Optional regularizer for the bias vector.
   2081     activity_regularizer: Optional regularizer function for the output.
   2082     kernel_constraint: Optional projection function to be applied to the
   2083         kernel after being updated by an `Optimizer` (e.g. used to implement
   2084         norm constraints or value constraints for layer weights). The function
   2085         must take as input the unprojected variable and must return the
   2086         projected variable (which must have the same shape). Constraints are
   2087         not safe to use when doing asynchronous distributed training.
   2088     bias_constraint: Optional projection function to be applied to the
   2089         bias after being updated by an `Optimizer`.
   2090     trainable: Boolean, if `True` also add variables to the graph collection
   2091       `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
   2092     name: A string, the name of the layer.
   2093     reuse: Boolean, whether to reuse the weights of a previous layer
   2094       by the same name.
   2095 
   2096   Returns:
   2097     Output tensor.
   2098 
   2099   Raises:
   2100     ValueError: if eager execution is enabled.
   2101   """
   2102   layer = Conv3DTranspose(
   2103       filters=filters,
   2104       kernel_size=kernel_size,
   2105       strides=strides,
   2106       padding=padding,
   2107       data_format=data_format,
   2108       activation=activation,
   2109       use_bias=use_bias,
   2110       kernel_initializer=kernel_initializer,
   2111       bias_initializer=bias_initializer,
   2112       kernel_regularizer=kernel_regularizer,
   2113       bias_regularizer=bias_regularizer,
   2114       activity_regularizer=activity_regularizer,
   2115       kernel_constraint=kernel_constraint,
   2116       bias_constraint=bias_constraint,
   2117       trainable=trainable,
   2118       name=name,
   2119       dtype=inputs.dtype.base_dtype,
   2120       _reuse=reuse,
   2121       _scope=name)
   2122   return layer.apply(inputs)
   2123 
   2124 
   2125 # Aliases
   2126 
   2127 Convolution1D = Conv1D
   2128 Convolution2D = Conv2D
   2129 Convolution3D = Conv3D
   2130 SeparableConvolution2D = SeparableConv2D
   2131 Convolution2DTranspose = Deconvolution2D = Deconv2D = Conv2DTranspose
   2132 Convolution3DTranspose = Deconvolution3D = Deconv3D = Conv3DTranspose
   2133 convolution1d = conv1d
   2134 convolution2d = conv2d
   2135 convolution3d = conv3d
   2136 separable_convolution2d = separable_conv2d
   2137 convolution2d_transpose = deconvolution2d = deconv2d = conv2d_transpose
   2138 convolution3d_transpose = deconvolution3d = deconv3d = conv3d_transpose
   2139