Hướng dẫn dùng tensorflow concatenate python

  • Install
  • Learn
  • API
    • Overview
    • Python
    • C++
    • Java
    • More
  • Resources
  • Community
  • Why TensorFlow
  • GitHub

Stay organized with collections Save and categorize content based on your preferences.

Layer that concatenates a list of inputs.

Inherits From: Layer, Module

View aliases

Compat aliases for migration

See Migration guide for more details.

tf.compat.v1.keras.layers.Concatenate

tf.keras.layers.Concatenate(
    axis=-1, **kwargs
)

Used in the notebooks

Used in the guideUsed in the tutorials
  • Migrate `tf.feature_column`s to Keras preprocessing layers
  • Migrate from TPU embedding_columns to TPUEmbedding layer
  • Load CSV data
  • pix2pix: Image-to-image translation with a conditional GAN
  • Image segmentation
  • Networks
  • Using text and neural network features

It takes as input a list of tensors, all of the same shape except for the concatenation axis, and returns a single tensor that is the concatenation of all inputs.

x = np.arange(20).reshape(2, 2, 5)
print(x)
[[[ 0  1  2  3  4]
  [ 5  6  7  8  9]]
 [[10 11 12 13 14]
  [15 16 17 18 19]]]
y = np.arange(20, 30).reshape(2, 1, 5)
print(y)
[[[20 21 22 23 24]]
 [[25 26 27 28 29]]]
tf.keras.layers.Concatenate(axis=1)([x, y])

array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [20, 21, 22, 23, 24]],
       [[10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19],
        [25, 26, 27, 28, 29]]])>
x1 = tf.keras.layers.Dense(8)(np.arange(10).reshape(5, 2))
x2 = tf.keras.layers.Dense(8)(np.arange(10, 20).reshape(5, 2))
concatted = tf.keras.layers.Concatenate()([x1, x2])
concatted.shape
TensorShape([5, 16])

Args

axis Axis along which to concatenate.
**kwargs standard layer keyword arguments.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.

Last updated 2022-09-08 UTC.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]

  • Install
  • Learn
  • API
    • Overview
    • Python
    • C++
    • Java
    • More
  • Resources
  • Community
  • Why TensorFlow
  • GitHub

Stay organized with collections Save and categorize content based on your preferences.

Concatenates tensors along one dimension.

View aliases

Compat aliases for migration

See Migration guide for more details.

tf.compat.v1.concat

tf.concat(
    values, axis, name='concat'
)

Used in the notebooks

Used in the guideUsed in the tutorials
  • Better performance with the tf.data API
  • Ragged tensors
  • Customize what happens in Model.fit
  • Writing a training loop from scratch
  • TensorFlow basics
  • word2vec
  • Simple audio recognition: Recognizing keywords
  • Learned data compression
  • Integrated gradients
  • Load a pandas DataFrame

See also tf.tile, tf.stack, tf.repeat.

Concatenates the list of tensors values along dimension axis. If values[i].shape = [D0, D1, ... Daxis(i), ...Dn], the concatenated result has shape

[D0, D1, ... Raxis, ...Dn]

where

Raxis = sum(Daxis(i))

That is, the data from the input tensors is joined along the axis dimension.

The number of dimensions of the input tensors must match, and all dimensions except axis must be equal.

For example:

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 0)

array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]], dtype=int32)>
tf.concat([t1, t2], 1)

array([[ 1,  2,  3,  7,  8,  9],
       [ 4,  5,  6, 10, 11, 12]], dtype=int32)>

As in Python, the axis could also be negative numbers. Negative axis are interpreted as counting from the end of the rank, i.e., axis + rank(values)-th dimension.

For example:

t1 = [[[1, 2], [2, 3]], [[4, 4], [5, 3]]]
t2 = [[[7, 4], [8, 4]], [[2, 10], [15, 11]]]
tf.concat([t1, t2], -1)

  array([[[ 1,  2,  7,  4],
          [ 2,  3,  8,  4]],
         [[ 4,  4,  2, 10],
          [ 5,  3, 15, 11]]], dtype=int32)>
tf.concat([tf.expand_dims(t, axis) for t in tensors], axis)

can be rewritten as

tf.stack(tensors, axis=axis)

Args

values A list of Tensor objects or a single Tensor.
axis 0-D int32 Tensor. Dimension along which to concatenate. Must be in the range [-rank(values), rank(values)). As in Python, indexing for axis is 0-based. Positive axis in the rage of [0, rank(values)) refers to axis-th dimension. And negative axis refers to axis + rank(values)-th dimension.
name A name for the operation (optional).

Returns

A Tensor resulting from concatenation of the input tensors.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.

Last updated 2022-09-08 UTC.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]