- TensorFlow Machine Learning Cookbook
- Nick McClure
- 630字
- 2021-04-02 20:36:28
Declaring Tensors
Tensors are the primary data structure that TensorFlow uses to operate on the computational graph. We can declare these tensors as variables and or feed them in as placeholders. First we must know how to create tensors.
Getting ready
When we create a tensor and declare it to be a variable, TensorFlow creates several graph structures in our computation graph. It is also important to point out that just by creating a tensor, TensorFlow is not adding anything to the computational graph. TensorFlow does this only after creating available out of the tensor. See the next section on variables and placeholders for more information.
How to do it…
Here we will cover the main ways to create tensors in TensorFlow:
- Fixed tensors:
- Create a zero filled tensor. Use the following:
zero_tsr = tf.zeros([row_dim, col_dim])
- Create a one filled tensor. Use the following:
ones_tsr = tf.ones([row_dim, col_dim])
- Create a constant filled tensor. Use the following:
filled_tsr = tf.fill([row_dim, col_dim], 42)
- Create a tensor out of an existing constant. Use the following:
constant_tsr = tf.constant([1,2,3])
- Create a zero filled tensor. Use the following:
- Tensors of similar shape:
- We can also initialize variables based on the shape of other tensors, as follows:
zeros_similar = tf.zeros_like(constant_tsr) ones_similar = tf.ones_like(constant_tsr)
- We can also initialize variables based on the shape of other tensors, as follows:
- Sequence tensors:
- TensorFlow allows us to specify tensors that contain defined intervals. The following functions behave very similarly to the
range()
outputs and numpy'slinspace()
outputs. See the following function:linear_tsr = tf.linspace(start=0, stop=1, start=3)
- The resulting tensor is the sequence
[0.0, 0.5, 1.0]
. Note that this function includes the specified stop value. See the following function:integer_seq_tsr = tf.range(start=6, limit=15, delta=3)
- The result is the sequence [6, 9, 12]. Note that this function does not include the limit value.
- TensorFlow allows us to specify tensors that contain defined intervals. The following functions behave very similarly to the
- Random tensors:
- The following generated random numbers are from a uniform distribution:
randunif_tsr = tf.random_uniform([row_dim, col_dim], minval=0, maxval=1)
- Note that this random uniform distribution draws from the interval that includes the
minval
but not themaxval
(minval
<=x
<maxval
). - To get a tensor with random draws from a normal distribution, as follows:
randnorm_tsr = tf.random_normal([row_dim, col_dim], mean=0.0, stddev=1.0)
- There are also times when we wish to generate normal random values that are assured within certain bounds. The
truncated_normal()
function always picks normal values within two standard deviations of the specified mean. See the following:runcnorm_tsr = tf.truncated_normal([row_dim, col_dim], mean=0.0, stddev=1.0)
- We might also be interested in randomizing entries of arrays. To accomplish this, there are two functions that help us:
random_shuffle()
andrandom_crop()
. See the following:shuffled_output = tf.random_shuffle(input_tensor) cropped_output = tf.random_crop(input_tensor, crop_size)
- Later on in this book, we will be interested in randomly cropping an image of size (height, width, 3) where there are three color spectrums. To fix a dimension in the
cropped_output
, you must give it the maximum size in that dimension:cropped_image = tf.random_crop(my_image, [height/2, width/2, 3])
- The following generated random numbers are from a uniform distribution:
推薦閱讀
- 中國戰(zhàn)略性新興產(chǎn)業(yè)研究與發(fā)展·數(shù)據(jù)中心
- Altium Designer Summer 09電路設(shè)計(jì)與制作
- 電子技能與實(shí)訓(xùn)項(xiàng)目教程
- 高速數(shù)字電路設(shè)計(jì)入門
- 移動基站設(shè)備與維護(hù)(第2版)
- 索尼新型彩色電視機(jī)速修圖解
- 碳化硅功率器件:特性、測試和應(yīng)用技術(shù)
- iOS 7開發(fā)快速入門
- 現(xiàn)代交換原理與技術(shù)(第2版)
- 微信小程序商城開發(fā):界面設(shè)計(jì)實(shí)戰(zhàn)
- 移動增值業(yè)務(wù)網(wǎng)絡(luò)及其運(yùn)營
- 信息通信網(wǎng)絡(luò)建設(shè)安全管理概要
- 電子技術(shù)基礎(chǔ)
- 電子管聲頻放大器實(shí)用手冊
- APP UI 設(shè)計(jì)手冊