Layer that concatenates a list of inputs. It takes as input a list of tensors, all of the same shape except for the concatenation axis, and returns a single tensor, the concatenation of all inputs. Arguments: axis: Axis along which to concatenate. **kwargs: standard layer keywo
| 354 | |
| 355 | @keras_export('keras.layers.Concatenate') |
| 356 | class Concatenate(_Merge): |
| 357 | """Layer that concatenates a list of inputs. |
| 358 | |
| 359 | It takes as input a list of tensors, |
| 360 | all of the same shape except for the concatenation axis, |
| 361 | and returns a single tensor, the concatenation of all inputs. |
| 362 | |
| 363 | Arguments: |
| 364 | axis: Axis along which to concatenate. |
| 365 | **kwargs: standard layer keyword arguments. |
| 366 | """ |
| 367 | |
| 368 | def __init__(self, axis=-1, **kwargs): |
| 369 | super(Concatenate, self).__init__(**kwargs) |
| 370 | self.axis = axis |
| 371 | self.supports_masking = True |
| 372 | self._reshape_required = False |
| 373 | |
| 374 | @tf_utils.shape_type_conversion |
| 375 | def build(self, input_shape): |
| 376 | # Used purely for shape validation. |
| 377 | if not isinstance(input_shape, list) or len(input_shape) < 2: |
| 378 | raise ValueError('A `Concatenate` layer should be called ' |
| 379 | 'on a list of at least 2 inputs') |
| 380 | if all(shape is None for shape in input_shape): |
| 381 | return |
| 382 | reduced_inputs_shapes = [list(shape) for shape in input_shape] |
| 383 | shape_set = set() |
| 384 | for i in range(len(reduced_inputs_shapes)): |
| 385 | del reduced_inputs_shapes[i][self.axis] |
| 386 | shape_set.add(tuple(reduced_inputs_shapes[i])) |
| 387 | if len(shape_set) > 1: |
| 388 | raise ValueError('A `Concatenate` layer requires ' |
| 389 | 'inputs with matching shapes ' |
| 390 | 'except for the concat axis. ' |
| 391 | 'Got inputs shapes: %s' % (input_shape)) |
| 392 | |
| 393 | def _merge_function(self, inputs): |
| 394 | return K.concatenate(inputs, axis=self.axis) |
| 395 | |
| 396 | @tf_utils.shape_type_conversion |
| 397 | def compute_output_shape(self, input_shape): |
| 398 | if not isinstance(input_shape, list): |
| 399 | raise ValueError('A `Concatenate` layer should be called ' |
| 400 | 'on a list of inputs.') |
| 401 | input_shapes = input_shape |
| 402 | output_shape = list(input_shapes[0]) |
| 403 | for shape in input_shapes[1:]: |
| 404 | if output_shape[self.axis] is None or shape[self.axis] is None: |
| 405 | output_shape[self.axis] = None |
| 406 | break |
| 407 | output_shape[self.axis] += shape[self.axis] |
| 408 | return tuple(output_shape) |
| 409 | |
| 410 | def compute_mask(self, inputs, mask=None): |
| 411 | if mask is None: |
| 412 | return None |
| 413 | if not isinstance(mask, list): |