1、函数及参数:tf.nn.conv2d

conv2d(
input,
filter,
strides,
padding,
use_cudnn_on_gpu=True,
data_format='NHWC',
name=None
)

卷积的原理可参考 A guide to convolution arithmetic for deep learning

参数列表:

参数名 必选 类型 说明
input tensor 是一个 4 维的 tensor,即 [ batch, in_height, in_width, in_channels ](若 input 是图像,[ 训练时一个 batch 的图片数量, 图片高度, 图片宽度, 图像通道数 ])
filter tensor 是一个 4 维的 tensor,即 [ filter_height, filter_width, in_channels, out_channels ](若 input 是图像,[ 卷积核的高度,卷积核的宽度,图像通道数,卷积核个数 ]),filter 的 in_channels 必须和 input 的 in_channels 相等
strides 列表 长度为 4 的 list,卷积时候在 input 上每一维的步长,一般 strides[0] = strides[3] = 1
padding string 只能为 " VALID "," SAME " 中之一,这个值决定了不同的卷积方式。VALID 丢弃方式;SAME:补全方式
use_cudnn_on_gpu bool 是否使用 cudnn 加速,默认为 true
data_format string 只能是 " NHWC ", " NCHW ",默认 " NHWC "
name string 运算名称
实例:

  1. import tensorflow as tf
  2.  
  3. a = tf.constant([1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,1,0,0,1,1,0,0],dtype=tf.float32,shape=[1,5,5,1])
  4. b = tf.constant([1,0,1,0,1,0,1,0,1],dtype=tf.float32,shape=[3,3,1,1])
  5. c = tf.nn.conv2d(a,b,strides=[1, 2, 2, 1],padding='VALID')
  6. d = tf.nn.conv2d(a,b,strides=[1, 2, 2, 1],padding='SAME')
  7. with tf.Session() as sess:
  8. print ("c shape:")
  9. print (c.shape)
  10. print ("c value:")
  11. print (sess.run(c))
  12. print ("d shape:")
  13. print (d.shape)
  14. print ("d value:")
  15. print (sess.run(d))

结果:

  1. c shape:
  2. (1, 3, 3, 1)
  3. c value:
  4. [[[[ 4.]
  5. [ 3.]
  6. [ 4.]]
  7.  
  8. [[ 2.]
  9. [ 4.]
  10. [ 3.]]
  11.  
  12. [[ 2.]
  13. [ 3.]
  14. [ 4.]]]]
  15. d shape:
  16. (1, 5, 5, 1)
  17. d value:
  18. [[[[ 2.]
  19. [ 2.]
  20. [ 3.]
  21. [ 1.]
  22. [ 1.]]
  23.  
  24. [[ 1.]
  25. [ 4.]
  26. [ 3.]
  27. [ 4.]
  28. [ 1.]]
  29.  
  30. [[ 1.]
  31. [ 2.]
  32. [ 4.]
  33. [ 3.]
  34. [ 3.]]
  35.  
  36. [[ 1.]
  37. [ 2.]
  38. [ 3.]
  39. [ 4.]
  40. [ 1.]]
  41.  
  42. [[ 0.]
  43. [ 2.]
  44. [ 2.]
  45. [ 1.]
  46. [ 1.]]]]

 参考:https://blog.csdn.net/zuolixiangfisher/article/details/80528989

1.1、tf.layers.conv1d

一维卷积一般用于处理文本,所以输入一般是一段长文本,就是词的列表

函数定义如下:

  1. tf.layers.conv1d(
  2. inputs,
  3. filters,
  4. kernel_size,
  5. strides=1,
  6. padding='valid',
  7. data_format='channels_last',
  8. dilation_rate=1,
  9. activation=None,
  10. use_bias=True,
  11. kernel_initializer=None,
  12. bias_initializer=tf.zeros_initializer(),
  13. kernel_regularizer=None,
  14. bias_regularizer=None,
  15. activity_regularizer=None,
  16. kernel_constraint=None,
  17. bias_constraint=None,
  18. trainable=True,
  19. name=None,
  20. reuse=None
  21. )

比较重要的几个参数是inputs, filters, kernel_size,下面分别说明

inputs :  输入tensor, 维度(None,  a, b) 是一个三维的tensor

None  :  一般是填充样本的个数,batch_size

a         :  句子中的词数或者字数

b          :    字或者词的向量维度

filters :  过滤器的个数

kernel_size : 卷积核的大小,卷积核其实应该是一个二维的,这里只需要指定一维,是因为卷积核的第二维与输入的词向量维度是一致的,因为对于句子而言,卷积的移动方向只能是沿着词的方向,即只能在列维度移动

一个例子:

inputs = tf.placeholder('float', shape=[None, 6, 8])

out = tf.layers.conv1d(inputs, 5, 3)

说明: 对于一个样本而言,句子长度为6个字,字向量的维度为8

filters=5, kernel_size=3, 所以卷积核的维度为3*8

那么输入6*8经过3*8的卷积核卷积后得到的是4*1的一个向量(4=6-3+1)

又因为有5个过滤器,所以是得到5个4*1的向量

参考:https://blog.csdn.net/u011734144/article/details/84066928

2、函数及参数:tf.nn.relu

relu(
features,
name=None
)

功能说明:

参数列表:

参数名 必选 类型 说明
features tensor 是以下类型float32, float64, int32, int64, uint8, int16, int8, uint16, half
name string 运算名称

实例:

  1. import tensorflow as tf
  2.  
  3. a = tf.constant([1,-2,0,4,-5,6])
  4. b = tf.nn.relu(a)
  5. with tf.Session() as sess:
  6. print (sess.run(b))

执行结果:

  1. [1 0 0 4 0 6]

3、函数及参数:tf.nn.max_pool

max_pool(
value,
ksize,
strides,
padding,
data_format='NHWC',
name=None
)

功能说明:

参数列表:

参数名 必选 类型 说明
value tensor 4 维的张量,即 [ batch, height, width, channels ],数据类型为 tf.float32
ksize 列表 池化窗口的大小,长度为 4 的 list,一般是 [1, height, width, 1],因为不在 batch 和 channels 上做池化,所以第一个和最后一个维度为 1
strides 列表 池化窗口在每一个维度上的步长,一般 strides[0] = strides[3] = 1
padding string 只能为 " VALID "," SAME " 中之一,这个值决定了不同的池化方式。VALID 丢弃方式;SAME:补全方式
data_format string 只能是 " NHWC ", " NCHW ",默认" NHWC "
name string 运算名称

实例:

  1. import tensorflow as tf
  2.  
  3. a = tf.constant([1,3,2,1,2,9,1,1,1,3,2,3,5,6,1,2],dtype=tf.float32,shape=[1,4,4,1])
  4. b = tf.nn.max_pool(a,ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1],padding='VALID')
  5. c = tf.nn.max_pool(a,ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1],padding='SAME')
  6. with tf.Session() as sess:
  7. print ("b shape:")
  8. print (b.shape)
  9. print ("b value:")
  10. print (sess.run(b))
  11. print ("c shape:")
  12. print (c.shape)
  13. print ("c value:")
  14. print (sess.run(c))

执行结果:

  1. b shape:
  2. (1, 2, 2, 1)
  3. b value:
  4. [[[[ 9.]
  5. [ 2.]]
  6.  
  7. [[ 6.]
  8. [ 3.]]]]
  9. c shape:
  10. (1, 2, 2, 1)
  11. c value:
  12. [[[[ 9.]
  13. [ 2.]]
  14.  
  15. [[ 6.]
  16. [ 3.]]]]

池化的意义

4、函数及参数:tf.nn.dropout

dropout(
x,
keep_prob,
noise_shape=None,
seed=None,
name=None
)

功能说明:

参数列表:

参数名 必选 类型 说明
x tensor 输出元素是 x 中的元素以 keep_prob 概率除以 keep_prob,否则为 0
keep_prob scalar Tensor dropout 的概率,一般是占位符
noise_shape tensor 默认情况下,每个元素是否 dropout 是相互独立。如果指定 noise_shape,若 noise_shape[i] == shape(x)[i],该维度的元素是否 dropout 是相互独立,若 noise_shape[i] != shape(x)[i] 该维度元素是否 dropout 不相互独立,要么一起 dropout 要么一起保留
seed 数值 如果指定该值,每次 dropout 结果相同
name string 运算名称

防止过拟合

实例:

  1. import tensorflow as tf
  2.  
  3. a = tf.constant([1,2,3,4,5,6],shape=[2,3],dtype=tf.float32)
  4. b = tf.placeholder(tf.float32)
  5. c = tf.nn.dropout(a,b,[2,1],1)
  6. with tf.Session() as sess:
  7. sess.run(tf.global_variables_initializer())
  8. print (sess.run(c,feed_dict={b:0.75}))

执行结果:

  1. [[ 0. 0. 0. ]
  2. [ 5.33333349 6.66666651 8. ]]

5、函数及参数:tf.nn.sigmoid_cross_entropy_with_logits

sigmoid_cross_entropy_with_logits(
_sentinel=None,
labels=None,
logits=None,
name=None
)

功能说明:

先对 logits 通过 sigmoid 计算,再计算交叉熵,交叉熵代价函数可以参考 CS231n: Convolutional Neural Networks for Visual Recognition

参数列表:

参数名 必选 类型 说明
_sentinel None 没有使用的参数
labels Tensor type, shape 与 logits相同
logits Tensor type 是 float32 或者 float64
name string 运算名称

实例:

  1. import tensorflow as tf
  2. x = tf.constant([1,2,3,4,5,6,7],dtype=tf.float64)
  3. y = tf.constant([1,1,1,0,0,1,0],dtype=tf.float64)
  4. loss = tf.nn.sigmoid_cross_entropy_with_logits(labels = y,logits = x)
  5. with tf.Session() as sess:
  6. print (sess.run(loss))

执行结果:

  1. [ 3.13261688e-01 1.26928011e-01 4.85873516e-02 4.01814993e+00
  2. 5.00671535e+00 2.47568514e-03 7.00091147e+00]

相关1

 相关2

6、函数及参数:tf.truncated_normal

truncated_normal(
shape,
mean=0.0,
stddev=1.0,
dtype=tf.float32,
seed=None,
name=None
)

功能说明:

产生截断正态分布随机数,取值范围为 [ mean - 2 * stddev, mean + 2 * stddev ]

参数列表:

参数名 必选 类型 说明
shape 1 维整形张量或 array 输出张量的维度
mean 0 维张量或数值 均值
stddev 0 维张量或数值 标准差
dtype dtype 输出类型
seed 数值 随机种子,若 seed 赋值,每次产生相同随机数
name string 运算名称

实例:

  1. import tensorflow as tf
  2. initial = tf.truncated_normal(shape=[3,3], mean=0, stddev=1)
  3. print(tf.Session().run(initial))
执行结果:
  将得到一个取值范围 [ -2, 2 ] 的 3 * 3 矩阵

相关

7、函数及参数:tf.constant

constant(
value,
dtype=None,
shape=None,
name='Const',
verify_shape=False
)

功能说明:

根据 value 的值生成一个 shape 维度的常量张量

参数列表:

参数名 必选 类型 说明
value 常量数值或者 list 输出张量的值
dtype dtype 输出张量元素类型
shape 1 维整形张量或 array 输出张量的维度
name string 张量名称
verify_shape Boolean 检测 shape 是否和 value 的 shape 一致,若为 Fasle,不一致时,会用最后一个元素将 shape 补全

8、函数及参数:tf.placeholder

placeholder(
dtype,
shape=None,
name=None
)

功能说明:

是一种占位符,在执行时候需要为其提供数据

参数列表:

参数名 必选 类型 说明
dtype dtype 占位符数据类型
shape 1 维整形张量或 array 占位符维度
name string 占位符名称

实例:

  1. #!/usr/bin/python
  2.  
  3. import tensorflow as tf
  4. import numpy as np
  5.  
  6. x = tf.placeholder(tf.float32,[None,3])
  7. y = tf.matmul(x,x)
  8. with tf.Session() as sess:
  9. rand_array = np.random.rand(3,3)
  10. print(sess.run(y,feed_dict={x:rand_array}))

执行结果:

输出一个 3x3 的张量

constant、placeholder、variable介绍

9、函数及参数:tf.Variable

__init__(
initial_value=None,
trainable=True,
collections=None,
validate_shape=True,
caching_device=None,
name=None,
variable_def=None,
dtype=None,
expected_shape=None,
import_scope=None
)

功能说明:

维护图在执行过程中的状态信息,例如神经网络权重值的变化。

参数列表:

参数名 类型 说明
initial_value 张量 Variable 类的初始值,这个变量必须指定 shape 信息,否则后面 validate_shape 需设为 False
trainable Boolean 是否把变量添加到 collection GraphKeys.TRAINABLE_VARIABLES 中(collection 是一种全局存储,不受变量名生存空间影响,一处保存,到处可取)
collections Graph collections 全局存储,默认是 GraphKeys.GLOBAL_VARIABLES
validate_shape Boolean 是否允许被未知维度的 initial_value 初始化
caching_device string 指明哪个 device 用来缓存变量
name string 变量名
dtype dtype 如果被设置,初始化的值就会按照这个类型初始化
expected_shape TensorShape 要是设置了,那么初始的值会是这种维度

实例:

  1. #!/usr/bin/python
  2.  
  3. import tensorflow as tf
  4. initial = tf.truncated_normal(shape=[10,10],mean=0,stddev=1)
  5. W=tf.Variable(initial)
  6. list = [[1.,1.],[2.,2.]]
  7. X = tf.Variable(list,dtype=tf.float32)
  8. init_op = tf.global_variables_initializer()
  9. with tf.Session() as sess:
  10. sess.run(init_op)
  11. print ("##################(1)################")
  12. print (sess.run(W))
  13. print ("##################(2)################")
  14. print (sess.run(W[:2,:2]))
  15. op = W[:2,:2].assign(22.*tf.ones((2,2)))
  16. print ("###################(3)###############")
  17. print (sess.run(op))
  18. print ("###################(4)###############")
  19. print (W.eval(sess)) #computes and returns the value of this variable
  20. print ("####################(5)##############")
  21. print (W.eval()) #Usage with the default session
  22. print ("#####################(6)#############")
  23. print (W.dtype)
  24. print (sess.run(W.initial_value))
  25. print (sess.run(W.op))
  26. print (W.shape)
  27. print ("###################(7)###############")
  28. print (sess.run(X))

执行结果:

  1. ##################(1)################
  2. [[ 0.9146753 0.6688656 -1.160011 0.47780406 -0.75987047 0.12145027
  3. 1.9253138 0.47339278 0.35785297 -1.3425105 ]
  4. [ 0.5472313 -1.1619761 0.5283075 -0.18622003 1.2028183 0.11885296
  5. -0.7281785 -0.7120525 1.2409171 -0.6293891 ]
  6. [-0.749 0.6319412 1.2232305 0.84037226 1.3894111 1.6266172
  7. -0.19630633 -0.60519034 -0.04114273 -0.26215535]
  8. [ 1.6056366 -0.34816736 1.8081874 0.616779 0.11021009 -1.8835621
  9. -0.7651011 -1.9168056 0.4641662 -0.49475962]
  10. [-0.19028567 0.5131004 0.6460657 -1.9266285 -0.8439824 0.9123347
  11. -0.06474591 -1.1662508 -0.26887077 -1.0815035 ]
  12. [-0.4873036 -1.286625 0.6506081 -0.6631708 0.14755192 0.6262951
  13. 0.26856226 -0.73765683 -0.92605734 -0.06623057]
  14. [-0.63469845 0.53975016 -0.30874643 -0.38573733 0.7492554 -1.0891181
  15. -0.31184393 -0.9791635 -1.5904634 0.42422688]
  16. [-0.25923353 0.3640418 -0.13705702 1.3891985 -0.04305851 0.7362603
  17. -0.04637048 1.5171311 -0.41555843 -1.7395841 ]
  18. [-0.45628768 0.26341474 -0.6413827 -0.03408551 -0.6371565 -0.2707571
  19. -1.0807754 -0.545007 -0.9005748 -0.44123358]
  20. [-0.03599161 -0.93177044 0.12982644 0.7088804 0.35801464 0.11168886
  21. 1.227298 0.4929586 1.5796568 0.39961207]]
  22. ##################(2)################
  23. [[ 0.9146753 0.6688656]
  24. [ 0.5472313 -1.1619761]]
  25. ###################(3)###############
  26. [[22. 22. -1.160011 0.47780406 -0.75987047 0.12145027
  27. 1.9253138 0.47339278 0.35785297 -1.3425105 ]
  28. [22. 22. 0.5283075 -0.18622003 1.2028183 0.11885296
  29. -0.7281785 -0.7120525 1.2409171 -0.6293891 ]
  30. [-0.749 0.6319412 1.2232305 0.84037226 1.3894111 1.6266172
  31. -0.19630633 -0.60519034 -0.04114273 -0.26215535]
  32. [ 1.6056366 -0.34816736 1.8081874 0.616779 0.11021009 -1.8835621
  33. -0.7651011 -1.9168056 0.4641662 -0.49475962]
  34. [-0.19028567 0.5131004 0.6460657 -1.9266285 -0.8439824 0.9123347
  35. -0.06474591 -1.1662508 -0.26887077 -1.0815035 ]
  36. [-0.4873036 -1.286625 0.6506081 -0.6631708 0.14755192 0.6262951
  37. 0.26856226 -0.73765683 -0.92605734 -0.06623057]
  38. [-0.63469845 0.53975016 -0.30874643 -0.38573733 0.7492554 -1.0891181
  39. -0.31184393 -0.9791635 -1.5904634 0.42422688]
  40. [-0.25923353 0.3640418 -0.13705702 1.3891985 -0.04305851 0.7362603
  41. -0.04637048 1.5171311 -0.41555843 -1.7395841 ]
  42. [-0.45628768 0.26341474 -0.6413827 -0.03408551 -0.6371565 -0.2707571
  43. -1.0807754 -0.545007 -0.9005748 -0.44123358]
  44. [-0.03599161 -0.93177044 0.12982644 0.7088804 0.35801464 0.11168886
  45. 1.227298 0.4929586 1.5796568 0.39961207]]
  46. ###################(4)###############
  47. [[22. 22. -1.160011 0.47780406 -0.75987047 0.12145027
  48. 1.9253138 0.47339278 0.35785297 -1.3425105 ]
  49. [22. 22. 0.5283075 -0.18622003 1.2028183 0.11885296
  50. -0.7281785 -0.7120525 1.2409171 -0.6293891 ]
  51. [-0.749 0.6319412 1.2232305 0.84037226 1.3894111 1.6266172
  52. -0.19630633 -0.60519034 -0.04114273 -0.26215535]
  53. [ 1.6056366 -0.34816736 1.8081874 0.616779 0.11021009 -1.8835621
  54. -0.7651011 -1.9168056 0.4641662 -0.49475962]
  55. [-0.19028567 0.5131004 0.6460657 -1.9266285 -0.8439824 0.9123347
  56. -0.06474591 -1.1662508 -0.26887077 -1.0815035 ]
  57. [-0.4873036 -1.286625 0.6506081 -0.6631708 0.14755192 0.6262951
  58. 0.26856226 -0.73765683 -0.92605734 -0.06623057]
  59. [-0.63469845 0.53975016 -0.30874643 -0.38573733 0.7492554 -1.0891181
  60. -0.31184393 -0.9791635 -1.5904634 0.42422688]
  61. [-0.25923353 0.3640418 -0.13705702 1.3891985 -0.04305851 0.7362603
  62. -0.04637048 1.5171311 -0.41555843 -1.7395841 ]
  63. [-0.45628768 0.26341474 -0.6413827 -0.03408551 -0.6371565 -0.2707571
  64. -1.0807754 -0.545007 -0.9005748 -0.44123358]
  65. [-0.03599161 -0.93177044 0.12982644 0.7088804 0.35801464 0.11168886
  66. 1.227298 0.4929586 1.5796568 0.39961207]]
  67. ####################(5)##############
  68. [[22. 22. -1.160011 0.47780406 -0.75987047 0.12145027
  69. 1.9253138 0.47339278 0.35785297 -1.3425105 ]
  70. [22. 22. 0.5283075 -0.18622003 1.2028183 0.11885296
  71. -0.7281785 -0.7120525 1.2409171 -0.6293891 ]
  72. [-0.749 0.6319412 1.2232305 0.84037226 1.3894111 1.6266172
  73. -0.19630633 -0.60519034 -0.04114273 -0.26215535]
  74. [ 1.6056366 -0.34816736 1.8081874 0.616779 0.11021009 -1.8835621
  75. -0.7651011 -1.9168056 0.4641662 -0.49475962]
  76. [-0.19028567 0.5131004 0.6460657 -1.9266285 -0.8439824 0.9123347
  77. -0.06474591 -1.1662508 -0.26887077 -1.0815035 ]
  78. [-0.4873036 -1.286625 0.6506081 -0.6631708 0.14755192 0.6262951
  79. 0.26856226 -0.73765683 -0.92605734 -0.06623057]
  80. [-0.63469845 0.53975016 -0.30874643 -0.38573733 0.7492554 -1.0891181
  81. -0.31184393 -0.9791635 -1.5904634 0.42422688]
  82. [-0.25923353 0.3640418 -0.13705702 1.3891985 -0.04305851 0.7362603
  83. -0.04637048 1.5171311 -0.41555843 -1.7395841 ]
  84. [-0.45628768 0.26341474 -0.6413827 -0.03408551 -0.6371565 -0.2707571
  85. -1.0807754 -0.545007 -0.9005748 -0.44123358]
  86. [-0.03599161 -0.93177044 0.12982644 0.7088804 0.35801464 0.11168886
  87. 1.227298 0.4929586 1.5796568 0.39961207]]
  88. #####################(6)#############
  89. <dtype: 'float32_ref'>
  90. [[-8.67323399e-01 4.41813141e-01 -3.52488965e-01 -1.76614091e-01
  91. 1.00177491e+00 2.64105648e-01 -5.67676127e-01 -5.66444278e-01
  92. -1.11713566e-01 -1.62478304e+00]
  93. [-4.01060373e-01 -1.53238133e-01 1.42307401e+00 1.83093756e-01
  94. -3.86358112e-01 -3.38347167e-01 -7.37435699e-01 4.85668957e-01
  95. -2.23827705e-01 -6.16371274e-01]
  96. [-1.74246800e+00 -1.50056124e+00 4.86318618e-01 2.23255545e-01
  97. -1.32530510e-01 4.32058543e-01 1.63234258e+00 4.27152753e-01
  98. -8.93448740e-02 -1.63452363e+00]
  99. [-5.66304445e-01 1.96294522e+00 -1.79052025e-01 -4.16035742e-01
  100. -1.98147106e+00 9.22261253e-02 1.61968184e+00 -8.48334093e-05
  101. -1.10359132e+00 -9.37306821e-01]
  102. [-1.29361153e+00 -2.19077677e-01 9.65968966e-01 -1.00457764e+00
  103. 1.03525579e+00 7.68449605e-01 -2.83788472e-01 -9.96825278e-01
  104. 9.89943027e-01 -8.15696597e-01]
  105. [ 1.05699182e+00 4.94321942e-01 5.56545183e-02 -6.78756416e-01
  106. -7.09120393e-01 9.43395719e-02 8.03394914e-02 -3.90621006e-01
  107. 1.11901140e+00 1.81483221e+00]
  108. [ 1.01025134e-01 7.93871880e-01 -9.95540798e-01 -1.59315515e+00
  109. 6.70278594e-02 -1.59100366e+00 5.90698659e-01 -1.91533327e-01
  110. -1.50902605e+00 1.18755126e+00]
  111. [-1.12480015e-01 -3.39124560e-01 -2.30515420e-01 -5.54720983e-02
  112. 1.67964801e-01 -6.07038677e-01 7.68552572e-02 -3.74692559e-01
  113. 2.46963039e-01 -3.22560370e-01]
  114. [-8.20123136e-01 -1.29625332e-02 1.89566636e+00 8.59677613e-01
  115. 7.04243034e-02 2.64431804e-01 -5.70574045e-01 1.15279579e+00
  116. -2.09608763e-01 1.06412935e+00]
  117. [-6.38129354e-01 -7.22046494e-01 -9.16518748e-01 1.95674682e+00
  118. 1.76543847e-01 -1.04135227e+00 -1.37964451e+00 5.35133541e-01
  119. -1.39267373e+00 -1.18439484e+00]]
  120. None
  121. (10, 10)
  122. ###################(7)###############
  123. [[1. 1.]
  124. [2. 2.]]

10、函数及参数:tf.nn.bias_add

bias_add(
value,
bias,
data_format=None,
name=None
)

功能说明:

将偏差项 bias 加到 value 上面,可以看做是 tf.add 的一个特例,其中 bias 必须是一维的,并且维度和 value 的最后一维相同,数据类型必须和 value 相同。

参数列表:

参数名 必选 类型 说明
value 张量 数据类型为 float, double, int64, int32, uint8, int16, int8, complex64, or complex128
bias 1 维张量 维度必须和 value 最后一维维度相等
data_format string 数据格式,支持 ' NHWC ' 和 ' NCHW '
name string 运算名称

实例:

  1. #!/usr/bin/python
  2.  
  3. import tensorflow as tf
  4. import numpy as np
  5.  
  6. a = tf.constant([[1.0, 2.0],[1.0, 2.0],[1.0, 2.0]])
  7. b = tf.constant([2.0,1.0])
  8. c = tf.constant([1.0])
  9. sess = tf.Session()
  10. print (sess.run(tf.nn.bias_add(a, b)))
  11. #print (sess.run(tf.nn.bias_add(a,c))) error
  12. print ("##################################")
  13. print (sess.run(tf.add(a, b)))
  14. print ("##################################")
  15. print (sess.run(tf.add(a, c)))

执行结果:

  3 个 3x2 维张量

11、函数及参数:tf.reduce_mean

reduce_mean(
input_tensor,
axis=None,
keep_dims=False,
name=None,
reduction_indices=None
)

功能说明:

计算张量 input_tensor 平均值

参数列表:

参数名 必选 类型 说明
input_tensor 张量 输入待求平均值的张量
axis None、0、1 None:全局求平均值;0:求每一列平均值;1:求每一行平均值
keep_dims Boolean 保留原来的维度(例如不会从二维矩阵降为一维向量)
name string 运算名称
reduction_indices None 和 axis 等价,被弃用

实例:

  1. #!/usr/bin/python
  2.  
  3. import tensorflow as tf
  4. import numpy as np
  5.  
  6. initial = [[1.,1.],[2.,2.]]
  7. x = tf.Variable(initial,dtype=tf.float32)
  8. init_op = tf.global_variables_initializer()
  9. with tf.Session() as sess:
  10. sess.run(init_op)
  11. print(sess.run(tf.reduce_mean(x)))
  12. print(sess.run(tf.reduce_mean(x,0))) #Column
  13. print(sess.run(tf.reduce_mean(x,1))) #row

执行结果:

  1. 1.5
  2. [ 1.5 1.5]
  3. [ 1. 2.]

12、函数及参数:tf.squared_difference

squared_difference(
x,
y,
name=None
)

功能说明:

计算张量 x、y 对应元素差平方

参数列表:

参数名 必选 类型 说明
x 张量 是 half, float32, float64, int32, int64, complex64, complex128 其中一种类型
y 张量 是 half, float32, float64, int32, int64, complex64, complex128 其中一种类型
name string 运算名称

实例:

  1. #!/usr/bin/python
  2.  
  3. import tensorflow as tf
  4. import numpy as np
  5.  
  6. initial_x = [[1.,1.],[2.,2.]]
  7. x = tf.Variable(initial_x,dtype=tf.float32)
  8. initial_y = [[3.,3.],[4.,4.]]
  9. y = tf.Variable(initial_y,dtype=tf.float32)
  10. diff = tf.squared_difference(x,y)
  11. init_op = tf.global_variables_initializer()
  12. with tf.Session() as sess:
  13. sess.run(init_op)
  14. print(sess.run(diff))

执行结果:

  1. [[ 4. 4.]
  2. [ 4. 4.]]

13、函数及参数:tf.square

square(
x,
name=None
)

功能说明:

计算张量对应元素平方

参数列表:

参数名 必选 类型 说明
x 张量 是 half, float32, float64, int32, int64, complex64, complex128 其中一种类型
name string 运算名称

实例:

  1. #!/usr/bin/python
  2. import tensorflow as tf
  3. import numpy as np
  4.  
  5. initial_x = [[1.,1.],[2.,2.]]
  6. x = tf.Variable(initial_x,dtype=tf.float32)
  7. x2 = tf.square(x)
  8. init_op = tf.global_variables_initializer()
  9. with tf.Session() as sess:
  10. sess.run(init_op)
  11. print(sess.run(x2))

执行结果:

  1. [[ 1. 1.]
  2. [ 4. 4.]]

 14、tf.layers.dense()

全连接层,和tf.fully connected layers功能相同,但推荐tf.layers.dense()

  1. tf.layers.dense(
  2. inputs,
  3. units,
  4. activation=None,
  5. use_bias=True,
  6. kernel_initializer=None,
  7. bias_initializer=tf.zeros_initializer(),
  8. kernel_regularizer=None,
  9. bias_regularizer=None,
  10. activity_regularizer=None,
  11. kernel_constraint=None,
  12. bias_constraint=None,
  13. trainable=True,
  14. name=None,
  15. reuse=None
  16. )
  • inputs:该层的输入。
  • units: 输出的大小(维数),整数或long。
  • activation: 使用什么激活函数(神经网络的非线性层),默认为None,不使用激活函数。
  • use_bias: 使用bias为True(默认使用),不用bias改成False即可。
  • kernel_initializer:权重矩阵的初始化函数。 如果为None(默认值),则使用tf.get_variable使用的默认初始化程序初始化权重。
  • bias_initializer:bias的初始化函数。
  • kernel_regularizer:权重矩阵的正则函数。
  • bias_regularizer:bias的的正则函数。
  • activity_regularizer:输出的的正则函数。
  • kernel_constraint:由优化器更新后应用于内核的可选投影函数(例如,用于实现层权重的范数约束或值约束)。 该函数必须将未投影的变量作为输入,并且必须返回投影变量(必须具有相同的形状)。 在进行异步分布式培训时,使用约束是不安全的。
  • bias_constraint:由优化器更新后应用于偏差的可选投影函数。
  • trainable:Boolean,如果为True,还将变量添加到图集collectionGraphKeys.TRAINABLE_VARIABLES(参见tf.Variable)。
  • name:名字
  • reuse:Boolean,是否以同一名称重用前一层的权重。

tensorFlow(一)相关重要函数理解的更多相关文章

  1. linux新定时器:timefd及相关操作函数

    timerfd是Linux为用户程序提供的一个定时器接口.这个接口基于文件描述符,通过文件描述符的可读事件进行超时通知,所以能够被用于select/poll的应用场景. 一,相关操作函数 #inclu ...

  2. (四) tensorflow笔记:常用函数说明

    tensorflow笔记系列: (一) tensorflow笔记:流程,概念和简单代码注释 (二) tensorflow笔记:多层CNN代码分析 (三) tensorflow笔记:多层LSTM代码分析 ...

  3. (二十四)linux新定时器:timefd及相关操作函数

    timerfd是Linux为用户程序提供的一个定时器接口.这个接口基于文件描述符,通过文件描述符的可读事件进行超时通知,所以能够被用于select/poll的应用场景. 一,相关操作函数 #inclu ...

  4. tensorflow dataloader 相关内容

    Tensorflow dataloader 相关调研:数据读取是训练的开始,是非常关键的一步:下面是调研时搜集到的一些相关链接: 十图详解tensorflow数据读取机制 https://zhuanl ...

  5. 一些PHP选项参数相关的函数

    关于 PHP 的配置,我们大多数情况下都是去查看 php.ini 文件或者通过命令行来查询某些信息,其实,PHP 的一些内置函数也可以帮助我们去查看或操作这些配置参数.比如之前我们学习过的 关于php ...

  6. Scala中Zip相关的函数

    在Scala中存在好几个Zip相关的函数,比如zip,zipAll,zipped 以及zipWithIndex等等.我们在代码中也经常看到这样的函数,这篇文章主要介绍一下这些函数的区别以及使用. 1. ...

  7. OpenGl的glMatrixMode()函数理解

    glMatrixMode()函数的参数,这个函数其实就是对接下来要做什么进行一下声明,也就是在要做下一步之前告诉计算机我要对“什么”进行操作了,这个“什么”在glMatrixMode的“()”里的选项 ...

  8. python字符串 列表 元组 字典相关操作函数总结

    1.字符串操作函数 find 在字符串中查找子串,找到首次出现的位置,返回下标,找不到返回-1 rfind 从右边查找 join 连接字符串数组 replace 用指定内容替换指定内容,可以指定次数 ...

  9. c/c++ 图相关的函数(二维数组法)

    c/c++ 图相关的函数(二维数组法) 遍历图 插入顶点 添加顶点间的线 删除顶点 删除顶点间的线 摧毁图 取得与v顶点有连线的第一个顶点 取得与v1顶点,v1顶点之后的v2顶点的之后的有连线的第一个 ...

随机推荐

  1. 如何通过代码审计挖掘REDos漏洞

    写这篇文章的目的一是由于目前网上关于java代码审计的资料实在是太少了,本人作为一个java代码审计的新手,深知学习java代码审计的难受之处,所以将自己学习过程中挖掘的一些漏洞写成博客发出来希望可以 ...

  2. puppeteer(二)操作实例——新Web自动化工具更轻巧更简单

    一.入门实例 了解puppeteer见上一篇文章: https://www.cnblogs.com/baihuitestsoftware/p/9957343.html 1)本例主要是启动浏览器 con ...

  3. SV class

    SV中的class通过new来创建实例,new函数可以传递多个参数: packet P1:  P1 = new:创建一个P1的对象: P2 = P1:P2也指向同一个对象: P2 = new P1:P ...

  4. 文件夹放到VS2015显示不出来

    在该项目中,点这个按钮 , 会显示没有包括进项目的文件 在要包括的文件上右键--包括在项目中即可 同理也可以从项目排除

  5. TCP/IP协议详解内容总结(怒喷一口老血)

    TCP/IP协议(本文源自外部链接) TCP/IP不是一个协议,而是一个协议族的统称.里面包括IP协议.IMCP协议.TCP协议. 这里有几个需要注意的知识点: 互联网地址:也就是IP地址,一般为网络 ...

  6. Hdu2041 超级楼梯 (斐波那契数列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2041 超级楼梯 Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. CSS 层级选择器

    CSS 层级选择器 注:使用空格分隔,塞选到最后一项应用. 标签选择器 下 标签选择器 父标签 --> 指定子标签添加css属性 <html> <head> <!- ...

  8. 【2.0】SpringBoot连接MySql 8.0的url设置

    jdbc:mysql://localhost:3306/enterprise?useUnicode=true&amp&useSSL=false&amp&characte ...

  9. 【Git】git error记录之 "unpacking the sent packfile failed on the remote"

    错误信息: error: cannot open .git/FETCH_HEAD: Permission denied unpacking the sent packfile failed on th ...

  10. 1_Linux概述

    linux就是一套操作系统 //系统调用与内核如果能够参考硬件的功能函数并修改你的操作系统程序代码,那经过改版后的操作系统就能够在另一个硬件平台上面运行了,这个操作通常被称为"软件移植&qu ...