How does tensorflow.pad work?

2024/10/11 2:23:39

There is the example of tensorflow.pad():

# 't' =  is [[1, 2, 3], [4, 5, 6]].
# 'paddings' is [[1, 1,], [2, 2]].
#  rank of 't' is 2.
' tf.pad(t, paddings, "CONSTANT")'
==> [[0, 0, 0, 0, 0, 0, 0],[0, 0, 1, 2, 3, 0, 0],[0, 0, 4, 5, 6, 0, 0],[0, 0, 0, 0, 0, 0, 0]]

my question is how to pad zeros in every dimention of input? And the shape of t is [2,3], why output after pad() is [4,x],how the '4' comes? Thanks for helping me!!!

Answer

'paddings' is [[1, 1,], [2, 2]]. Try to map this vale us as [[top,bottom],[left,right]]. i.e.

top = 1,      //Extra padding introduce on top
bottom = 1,   //Extra padding introduce on bottom
left = 2,     //Extra padding introduce on left 
right = 2.    //Extra padding introduce on right

Try another example where 'padding' is [[2, 1], [2, 3]]. Output will be:

[[0 0 0 0 0 0 0 0][0 0 0 0 0 0 0 0][0 0 1 2 3 0 0 0][0 0 4 5 6 0 0 0][0 0 0 0 0 0 0 0]]

Here top=2, bottom=1, left=2, right=3.

https://en.xdnf.cn/q/69825.html

Related Q&A

Installing PyPotrace on Windows 10

I would like to install Potrace on my Windows 10 Computer and I will be using it with python 2.7.5. I am following the installation instruction from this site.( https://pypi.python.org/pypi/pypotrace) …

dateutil 2.5.0 is the minimum required version

Im running the jupyter notebook (Enthought Canopy python distribution 2.7) on Mac OSX (v 10.13.6). When I try to import pandas (import pandas as pd), I am getting the complaint: ImportError: dateutil …

pytest fixture - get value and avoid error Fixture X called directly

I have updated pytest to 4.3.0 and now I need to rework test code since calling fixtures directly is deprecated. I have an issue with fixtures used in an unittest.TestCase, how do I get the value retur…

Django limit the number of requests per minute

Im trying to limit the number of requests from an IP in case I get too many requests from it. For example: if I will get more than 50 requests per minute I want to block that IP for 5 minutes. When I u…

How to add template variable in the filename of an EmailOperator task? (Airflow)

I cant seem to get this to work.I am trying to send daily a given file, whose name is like file_{{ds_nodash}}.csv.The problem is that I cant seem to add this name as the filename, since it seems it can…

Disadvantage of Python eggs?

Are there any disadvantages about using eggs through easy-install compared to the "traditional" packages/modules/libs?

Does Google App Engine Flex support Pipfile?

For App Engine Standard the explicitly state that they do not support Pipfiles and immediately block you from pushing your project if it contains a Pipfile. In searching the documentation, I dont see a…

Keras sees my GPU but doesnt use it when training a neural network

My GPU is not used by Keras/TensorFlow.To try to make my GPU working with tensorflow, I installed tensorflow-gpu via pip (I am using Anaconda on Windows)I have nvidia 1080tiprint(tf.test.is_gpu_availab…

Identify if there are two of the same character adjacent to eachother

Ive been asked to create a program that identifies if a password is valid or not. The one part I am struggling with is identifying whether there are two of the same character adjacent to each other. He…

Extract lined table from scanned document opencv python

I want to extract the information from a scanned table and store it a csv. Right now my table extraction algorithm does the following steps.Apply skew correction Apply a gaussian filter for denoising. …