Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/convolutional/conv1d.py: 93%

14 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

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"""Keras 1D convolution layer.""" 

16 

17 

18from keras.src import activations 

19from keras.src import constraints 

20from keras.src import initializers 

21from keras.src import regularizers 

22from keras.src.dtensor import utils 

23from keras.src.layers.convolutional.base_conv import Conv 

24 

25# isort: off 

26from tensorflow.python.util.tf_export import keras_export 

27 

28 

29@keras_export("keras.layers.Conv1D", "keras.layers.Convolution1D") 

30class Conv1D(Conv): 

31 """1D convolution layer (e.g. temporal convolution). 

32 

33 This layer creates a convolution kernel that is convolved 

34 with the layer input over a single spatial (or temporal) dimension 

35 to produce a tensor of outputs. 

36 If `use_bias` is True, a bias vector is created and added to the outputs. 

37 Finally, if `activation` is not `None`, 

38 it is applied to the outputs as well. 

39 

40 When using this layer as the first layer in a model, 

41 provide an `input_shape` argument 

42 (tuple of integers or `None`, e.g. 

43 `(10, 128)` for sequences of 10 vectors of 128-dimensional vectors, 

44 or `(None, 128)` for variable-length sequences of 128-dimensional vectors. 

45 

46 Examples: 

47 

48 >>> # The inputs are 128-length vectors with 10 timesteps, and the 

49 >>> # batch size is 4. 

50 >>> input_shape = (4, 10, 128) 

51 >>> x = tf.random.normal(input_shape) 

52 >>> y = tf.keras.layers.Conv1D( 

53 ... 32, 3, activation='relu',input_shape=input_shape[1:])(x) 

54 >>> print(y.shape) 

55 (4, 8, 32) 

56 

57 >>> # With extended batch shape [4, 7] (e.g. weather data where batch 

58 >>> # dimensions correspond to spatial location and the third dimension 

59 >>> # corresponds to time.) 

60 >>> input_shape = (4, 7, 10, 128) 

61 >>> x = tf.random.normal(input_shape) 

62 >>> y = tf.keras.layers.Conv1D( 

63 ... 32, 3, activation='relu', input_shape=input_shape[2:])(x) 

64 >>> print(y.shape) 

65 (4, 7, 8, 32) 

66 

67 Args: 

68 filters: Integer, the dimensionality of the output space 

69 (i.e. the number of output filters in the convolution). 

70 kernel_size: An integer or tuple/list of a single integer, 

71 specifying the length of the 1D convolution window. 

72 strides: An integer or tuple/list of a single integer, 

73 specifying the stride length of the convolution. 

74 Specifying any stride value != 1 is incompatible with specifying 

75 any `dilation_rate` value != 1. 

76 padding: One of `"valid"`, `"same"` or `"causal"` (case-insensitive). 

77 `"valid"` means no padding. `"same"` results in padding with zeros 

78 evenly to the left/right or up/down of the input such that output has 

79 the same height/width dimension as the input. 

80 `"causal"` results in causal (dilated) convolutions, e.g. `output[t]` 

81 does not depend on `input[t+1:]`. Useful when modeling temporal data 

82 where the model should not violate the temporal order. 

83 See [WaveNet: A Generative Model for Raw Audio, section 

84 2.1](https://arxiv.org/abs/1609.03499). 

85 data_format: A string, one of `channels_last` (default) or 

86 `channels_first`. The ordering of the dimensions in the inputs. 

87 `channels_last` corresponds to inputs with shape `(batch_size, width, 

88 channels)` while `channels_first` corresponds to inputs with shape 

89 `(batch_size, channels, width)`. Note that the `channels_first` format 

90 is currently not supported by TensorFlow on CPU. 

91 dilation_rate: an integer or tuple/list of a single integer, specifying 

92 the dilation rate to use for dilated convolution. 

93 Currently, specifying any `dilation_rate` value != 1 is 

94 incompatible with specifying any `strides` value != 1. 

95 groups: A positive integer specifying the number of groups in which the 

96 input is split along the channel axis. Each group is convolved 

97 separately with `filters / groups` filters. The output is the 

98 concatenation of all the `groups` results along the channel axis. 

99 Input channels and `filters` must both be divisible by `groups`. 

100 activation: Activation function to use. 

101 If you don't specify anything, no activation is applied 

102 (see `keras.activations`). 

103 use_bias: Boolean, whether the layer uses a bias vector. 

104 kernel_initializer: Initializer for the `kernel` weights matrix 

105 (see `keras.initializers`). Defaults to 'glorot_uniform'. 

106 bias_initializer: Initializer for the bias vector 

107 (see `keras.initializers`). Defaults to 'zeros'. 

108 kernel_regularizer: Regularizer function applied to 

109 the `kernel` weights matrix (see `keras.regularizers`). 

110 bias_regularizer: Regularizer function applied to the bias vector 

111 (see `keras.regularizers`). 

112 activity_regularizer: Regularizer function applied to 

113 the output of the layer (its "activation") 

114 (see `keras.regularizers`). 

115 kernel_constraint: Constraint function applied to the kernel matrix 

116 (see `keras.constraints`). 

117 bias_constraint: Constraint function applied to the bias vector 

118 (see `keras.constraints`). 

119 

120 Input shape: 

121 3+D tensor with shape: `batch_shape + (steps, input_dim)` 

122 

123 Output shape: 

124 3+D tensor with shape: `batch_shape + (new_steps, filters)` 

125 `steps` value might have changed due to padding or strides. 

126 

127 Returns: 

128 A tensor of rank 3 representing 

129 `activation(conv1d(inputs, kernel) + bias)`. 

130 

131 Raises: 

132 ValueError: when both `strides > 1` and `dilation_rate > 1`. 

133 """ 

134 

135 @utils.allow_initializer_layout 

136 def __init__( 

137 self, 

138 filters, 

139 kernel_size, 

140 strides=1, 

141 padding="valid", 

142 data_format="channels_last", 

143 dilation_rate=1, 

144 groups=1, 

145 activation=None, 

146 use_bias=True, 

147 kernel_initializer="glorot_uniform", 

148 bias_initializer="zeros", 

149 kernel_regularizer=None, 

150 bias_regularizer=None, 

151 activity_regularizer=None, 

152 kernel_constraint=None, 

153 bias_constraint=None, 

154 **kwargs 

155 ): 

156 super().__init__( 

157 rank=1, 

158 filters=filters, 

159 kernel_size=kernel_size, 

160 strides=strides, 

161 padding=padding, 

162 data_format=data_format, 

163 dilation_rate=dilation_rate, 

164 groups=groups, 

165 activation=activations.get(activation), 

166 use_bias=use_bias, 

167 kernel_initializer=initializers.get(kernel_initializer), 

168 bias_initializer=initializers.get(bias_initializer), 

169 kernel_regularizer=regularizers.get(kernel_regularizer), 

170 bias_regularizer=regularizers.get(bias_regularizer), 

171 activity_regularizer=regularizers.get(activity_regularizer), 

172 kernel_constraint=constraints.get(kernel_constraint), 

173 bias_constraint=constraints.get(bias_constraint), 

174 **kwargs 

175 ) 

176 

177 

178# Alias 

179 

180Convolution1D = Conv1D 

181