学习目录

1.tensorflow相关函数理解

(1)tf.nn.conv2d

(2)tf.nn.relu

(3)tf.nn.max_pool

(4)tf.nn.dropout

(5)tf.nn.sigmoid_cross_entropy_with_logits

(6)tf.nn.truncated_normal

(7)tf.nn.constant

(8)tf.nn.placeholder

(9)tf.nn.reduce_mean

(10)tf.nn.squared_difference

(1)tf.nn.square

2.tensorflow相关类的理解

(1)tf.Variavle


tf.nn.conv2d(tf.nn.conv2d是TensorFlow里面实现卷积的函数)

参数列表

  1. conv2d(
  2. input,
  3. filter,
  4. strides,
  5. padding,
  6. use_cudnn_on_gpu=True,
  7. data_format='NHWC',
  8. name=None
  9. )
参数名 必选 类型 说明
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 运算名称

以下的动画是我们以一张彩色的图片为例子,彩色图的通道数为3(黑白照的通道数为1),所以每张彩色的图片就需要3个filter,每个filter中的权重都不同,最后输出的值为各自所对应相乘相加即可。

实例:

  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.]]]]

tf.nn.relu(Tensorflow中常用的激活函数  ReLu=max(0,x)  )

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]

tf.nn.max_pool(CNN当中的最大值池化操作,其实用法和卷积很类似)

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.]]]]

tf.nn.dropout(tf.nn.dropout是TensorFlow里面为了防止或减轻过拟合而使用的函数,它一般用在全连接层)

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. ]]

tf.nn.sigmoid_cross_entropy_with_logits(对于给定的logits计算sigmoid的交叉熵。)

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

参数列表:

参数名 必选 类型 说明
_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]

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

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

参数列表

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))

执行结果:

  1. [[ 0.06492835 0.03914397 0.32634252]
  2. [ 0.22949421 0.21335489 0.6010958 ]
  3. [-0.11964546 -0.16878787 0.12951735]]

tf.constant(根据 value 的值生成一个 shape 维度的常量张量)

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

参数列表:

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

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

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

参数列表:

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

实例:

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

执行结果:

输出一个 3x3 的张量

tf.reduce_mean(计算张量 input_tensor 平均值)

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

参数列表:

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

实例:

  1. initial = [[1.,1.],[2.,2.]]
  2. x = tf.Variable(initial,dtype=tf.float32)
  3. init_op = tf.global_variables_initializer()
  4. with tf.Session() as sess:
  5. sess.run(init_op)
  6. print(sess.run(tf.reduce_mean(x)))
  7. print(sess.run(tf.reduce_mean(x,0))) #Column
  8. print(sess.run(tf.reduce_mean(x,1))) #row

运行结果:

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

tf.squared_difference(计算张量 x、y 对应元素差平方)

squared_difference(
x,
y,
name=None
)

参数列表:

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

实例:

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

执行结果:

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

tf.square(计算张量对应元素平方)

square(
x,
name=None
)

参数列表:

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

实例:

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

执行结果:

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


 

 tf.Variavle(TensorFlow中的图变量)

tf.Variable.init(initial_value, trainable=True, collections=None, validate_shape=True, name=None)

参数名称 参数类型 含义

initial_value:所有可以转换为Tensor的类型 变量的初始值
trainable bool:如果为True,会把它加入到GraphKeys.TRAINABLE_VARIABLES,才能对它使用Optimizer
collections list:指定该图变量的类型、默认为[GraphKeys.GLOBAL_VARIABLES]
validate_shape:bool 如果为False,则不进行类型和维度检查
name string:变量的名称,如果没有指定则系统会自动分配一个唯一的值

 实例:

  1. In [1]: import tensorflow as tf
  2. In [2]: v = tf.Variable(3, name='v')
  3. In [3]: v2 = v.assign(5)
  4. In [4]: sess = tf.InteractiveSession()
  5. In [5]: sess.run(v.initializer)
  6. In [6]: sess.run(v)
  7. Out[6]: 3
  8. In [7]: sess.run(v2)
  9. Out[7]: 5

内容参考与腾讯云开发者实验室

https://cloud.tencent.com/developer/labs/lab/10000

tensorflow相关API的学习的更多相关文章

  1. TensorFlow - 相关 API

    来自:https://cloud.tencent.com/developer/labs/lab/10324 TensorFlow - 相关 API TensorFlow 相关函数理解 任务时间:时间未 ...

  2. TensorFlow — 相关 API

    TensorFlow — 相关 API TensorFlow 相关函数理解 任务时间:时间未知 tf.truncated_normal truncated_normal( shape, mean=0. ...

  3. 主要DL Optimizer原理与Tensorflow相关API

    V(t) = y*V(t-1) + learning_rate*G(x) x(t) = x(t-1) - V(t) 参考:https://arxiv.org/pdf/1609.04747.pdf DL ...

  4. [原创]java WEB学习笔记44:Filter 简介,模型,创建,工作原理,相关API,过滤器的部署及映射的方式,Demo

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  5. 深度学习Tensorflow相关书籍推荐和PDF下载

    深度学习Tensorflow相关书籍推荐和PDF下载 baihualinxin关注 32018.03.28 10:46:16字数 481阅读 22,673 1.机器学习入门经典<统计学习方法&g ...

  6. [Tensorflow实战Google深度学习框架]笔记4

    本系列为Tensorflow实战Google深度学习框架知识笔记,仅为博主看书过程中觉得较为重要的知识点,简单摘要下来,内容较为零散,请见谅. 2017-11-06 [第五章] MNIST数字识别问题 ...

  7. 基于TensorFlow Serving的深度学习在线预估

    一.前言 随着深度学习在图像.语言.广告点击率预估等各个领域不断发展,很多团队开始探索深度学习技术在业务层面的实践与应用.而在广告CTR预估方面,新模型也是层出不穷: Wide and Deep[1] ...

  8. 学习《TensorFlow实战Google深度学习框架 (第2版) 》中文PDF和代码

    TensorFlow是谷歌2015年开源的主流深度学习框架,目前已得到广泛应用.<TensorFlow:实战Google深度学习框架(第2版)>为TensorFlow入门参考书,帮助快速. ...

  9. ava如何实现系统监控、系统信息收集、sigar开源API的学习(转)

    ava如何实现系统监控.系统信息收集.sigar开源API的学习(转) 转自:http://liningjustsoso.iteye.com/blog/1254584 首先给大家介绍一个开源工具Sig ...

随机推荐

  1. python安装模块方法汇总

    方法一: 原文地址: http://blog.csdn.net/cighao/article/details/47860041 在 windows 系统下,只需要输入命令 pip install re ...

  2. Linux组管理和权限管理

    ⒈Linux组基本介绍 1)在Linux中的每个用户必须属于一个组,不能独立于组外. 2)Linux中每个文件都有所有者.所在组.其它组的概念 ①所有者 一般(默认)为文件的创建者,谁创建了该文件,就 ...

  3. 【黑客免杀攻防】读书笔记5 - PE格式讲解

    0x01 MS-DOS头 MS-DOS头部的字段重点关注e_magic与最后一个e_lfanew是需要关注的. 第一个e_magic字段的值为4D5A,作用是可以作为判断这个文件是否是PE文件. 最后 ...

  4. kafka系列八、kafka消息重复和丢失的场景及解决方案分析

    消息重复和丢失是kafka中很常见的问题,主要发生在以下三个阶段: 生产者阶段 broke阶段 消费者阶段 一.生产者阶段重复场景 1.根本原因 生产发送的消息没有收到正确的broke响应,导致pro ...

  5. HTMl学习笔记02-编辑器

    工欲善其事,必先利其器 使用专业HTML编辑器来编辑HTML,推荐使用Notepad++,中文界面. 在Notepad++安装完成后,点击文件>新建.语言>H中选择HTML 在新建的文件输 ...

  6. 转载:《RESTful API 设计指南》 阮一峰

    原文:http://www.ruanyifeng.com/blog/2014/05/restful_api 网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面 ...

  7. js动态添加未知新属性

    1 前言 由于项目需要,需要实现获取服务器不同网卡扫局域网中的某种类型设备,然而不同服务器的网卡个数是不相同的,从而需要动态增加未知属性来统计不同网卡扫到的设备个数. 2 代码 function ni ...

  8. javascript 搞不清原型链和constructor

    prototype.constructor仅仅可以用于识别对象是由哪个构造函数初始化的,仅此而已. var one=new Person(‘js’); 要解释这个结果就要仔细研究一下new这个操作符了 ...

  9. 转:10分钟了解JS堆、栈以及事件循环的概念

    https://juejin.im/post/5b1deac06fb9a01e643e2a95?utm_medium=fe&utm_source=weixinqun 前言 其实一开始对栈.堆的 ...

  10. VS2013 "当前不会命中断点.还没有为该文档家在任何符号" 解决办法

    参考:http://blog.csdn.net/u010797208/article/details/40452797 亲测可行