tf.repeat(), Tensorflow 2.1.0 and above

tags: tensorflow

Today, the code used tf.repeat()

I checked the information and no one specifically explained this. Let me write it, hoping to help future generations.


Official documents:

https://www.tensorflow.org/api_docs/python/tf/repeat?hl=ca


Call the method:

tf.repeat(input, repeats, axis=None, name=None)

parameter:
1) input: a tensor

2) repeats: the number of repetitions
Note: len(repeats) must equal input.shape[axis] if axis is not None

3) axis: dimension
Let’s see an example to understand
Description: An int. The axis along which to repeat values. By default (axis=None), use the flattened input array, and return a flat output array.
If the axis has no parameters, the array will be flattened first to become one-dimensional and then repeated

Example 3:

tf.repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=1)
                                    -Input the 0th element repeated 2 times
                                                                         -Input the first element to repeat 3 times
[
 [1, 1, 2, 2, 2],
 [3, 3, 4, 4, 4]
]

tf.repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=0)
                                                                         -Input the 0th element repeated 2 times
                                                                         -Input the first element to repeat 3 times
[
 [1, 2],
 [1, 2],
 [3, 4],
 [3, 4],
 [3, 4]
]

For simple memory, axis = 1 increases horizontally, axis = 0 increases vertically


Example 1:
First look at the simplest one-dimensional

temp = tf.constant([1,2,3])
tf.Tensor: shape=(1, 3), dtype=int32, numpy=array([[1, 2, 3]], dtype=int32)

tf.repeat(input = temp, repeats=3, axis = 0)
 Output:
[1, 1, 1, 2, 2, 2, 3, 3, 3]
 Each number repeated 3 times

When the input is not one-dimensional, but no axis is assigned, the input will be flattened into one-dimensional

temp1 = tf.constant([[1,2],[3,4]])
tf.repeat(input = temp1, repeats = 2)
 Output:
[1,1,2,2,3,3,4,4]

Example 2:

temp3 = tf.const([[1],[2],[3]])
temp3.shape ---> (3,1)

As mentioned above, len(repeats) = input.shape[axis]

So in this example it must satisfy:
temp3.shape[axis = 0] = 4 = len(repeats)
temp3.shape[axis = 1] = 1 = len(repeats)

 1) When axis = 0
tf.repeat(temp3, repeats = [1,2,3], axis = 0)
 Output:
[
[1],
[2],
[2],
[3],
[3],
[3],
]

 2) When axis = 1
tf.repeat(temp3, repeats=2, axis=1)
 Output:
[
 [1, 1],
 [2, 2],
 [3, 3],
 [4, 4]
]

Intelligent Recommendation

Compile tensorflow 2.1.0 from source

Previously, because the cpu did not support avx instructions, I tried source code compilation but ended in failure. Yesterday, I went back to my old business and wanted to stand up... After two days o...

[Tensorflow] TF.Repeat Realizes a single element to extend a two-dimensional image with repeating elements

TF.Repeat Realizes a single element extension 2D image for its repeating element Target: Extend one dimensional data [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] into 9 two dimensional images (the element is 0 in t...

Anaconda+GTX1650+Python 3.7+Tensorflow 2.1.0

Special: 1. the blog post includes all white mounting posts Tensorflow 2.1.0 of the reference, does not provide specific installation steps. If you are also white, I can refer to the link provided, th...

For newbies, is it better to install tensorflow 2.1.0 or install 2.0.0?

I have been tossing back by tensorflow in the past two days and want to give up, but it is not enough to give up in case of difficulties I just started installing tensorflow2.1.0, when creating consta...

Tensorflow 2.1.0 Keras implements MNIST classification learning

MNIST is a collection of handwritten digital pictures Code Calculation results  ...

More Recommendation

TensorFlow 2.1.0 uses TFRecord to export and read images

Preface When an NLP player encountered a CV image classification task, to be honest, I was a little dazed. . . The task goal is to train an image classification model with a small number of layers and...

TensorFlow-cpu 2.1.0 installed in win10 pit

1. First make sure your python is64Bit 2. Install "Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019" Choose one to download and install according to your computer d...

(Novice Xiaobai) How to install tensorflow 2.1.0 with Anaconda

(Novice Xiaobai) How to install tensorflow 2.1.0 with Anaconda I'm a little white, because I'm exposed to the deep learning course this semester, I need to install tensorflow. After several days of co...

tensorflow 2.1.0-how to build a network (2)

1. The second way to build a network The network is constructed through the input layer and output layer. The network contains a hidden layer between the input and output, which returns a Model object...

CUDA10.1+cudnn7.6.5+tensorflow-gpu-2.1.0 installation process

Write a custom catalog title here Install anaconda Configure pip and conda domestic mirror source Create a virtual environment Install CUDA and cuDNN Check the current graphics card driver version Ins...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top