TensorFlow — 相关 API
TensorFlow — 相关 API
TensorFlow 相关函数理解
任务时间:时间未知
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 | 运算名称 |
示例代码:
现在您可以在 /home/ubuntu 目录下创建源文件 truncated_normal.py:
示例代码:/home/ubuntu/truncated_normal.py
#!/usr/bin/python
import tensorflow as tf
initial = tf.truncated_normal(shape=[3,3], mean=0, stddev=1)
print tf.Session().run(initial)
然后执行:
python /home/ubuntu/truncated_normal.py
执行结果:
将得到一个取值范围 [-2, 2] 的 3 * 3 矩阵,您也可以尝试修改源代码看看输出结果有什么变化?
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 补全 |
示例代码:
现在您可以在 /home/ubuntu 目录下创建源文件 constant.py,内容可参考:
示例代码:/home/ubuntu/constant.py
#!/usr/bin/python
import tensorflow as tf
import numpy as np
a = tf.constant([1,2,3,4,5,6],shape=[2,3])
b = tf.constant(-1,shape=[3,2])
c = tf.matmul(a,b)
e = tf.constant(np.arange(1,13,dtype=np.int32),shape=[2,2,3])
f = tf.constant(np.arange(13,25,dtype=np.int32),shape=[2,3,2])
g = tf.matmul(e,f)
with tf.Session() as sess:
print sess.run(a)
print ("##################################")
print sess.run(b)
print ("##################################")
print sess.run(c)
print ("##################################")
print sess.run(e)
print ("##################################")
print sess.run(f)
print ("##################################")
print sess.run(g)
然后执行:
python /home/ubuntu/constant.py
执行结果:
- a: 2x3 维张量;
- b: 3x2 维张量;
- c: 2x2 维张量;
- e: 2x2x3 维张量;
- f: 2x3x2 维张量;
- g: 2x2x2 维张量。
您也可以尝试修改源代码看看输出结果有什么变化?
tf.placeholder
placeholder(
dtype,
shape=None,
name=None
)
功能说明:
是一种占位符,在执行时候需要为其提供数据
参数列表:
| 参数名 | 必选 | 类型 | 说明 |
|---|---|---|---|
| dtype | 是 | dtype | 占位符数据类型 |
| shape | 否 | 1 维整形张量或 array | 占位符维度 |
| name | 否 | string | 占位符名称 |
示例代码:
现在您可以在 /home/ubuntu 目录下创建源文件 placeholder.py,内容可参考:
示例代码:/home/ubuntu/placeholder.py
#!/usr/bin/python
import tensorflow as tf
import numpy as np
x = tf.placeholder(tf.float32,[None,10])
y = tf.matmul(x,x)
with tf.Session() as sess:
rand_array = np.random.rand(10,10)
print sess.run(y,feed_dict={x:rand_array})
然后执行:
python /home/ubuntu/placeholder.py
执行结果:
输出一个 10x10 维的张量。您也可以尝试修改源代码看看输出结果有什么变化?
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 | 运算名称 |
示例代码:
现在您可以在 /home/ubuntu 目录下创建源文件 bias_add.py,内容可参考:
示例代码:/home/ubuntu/bias_add.py
#!/usr/bin/python
import tensorflow as tf
import numpy as np
a = tf.constant([[1.0, 2.0],[1.0, 2.0],[1.0, 2.0]])
b = tf.constant([2.0,1.0])
c = tf.constant([1.0])
sess = tf.Session()
print sess.run(tf.nn.bias_add(a, b))
#print sess.run(tf.nn.bias_add(a,c)) error
print ("##################################")
print sess.run(tf.add(a, b))
print ("##################################")
print sess.run(tf.add(a, c))
然后执行:
python /home/ubuntu/bias_add.py
执行结果:
3 个 3x2 维张量。您也可以尝试修改源代码看看输出结果有什么变化?
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 | 保留原来的维度,降为 1 |
| name | 否 | string | 运算名称 |
| reduction_indices | 否 | None | 和axis等价,被弃用 |
示例代码:
现在您可以在 /home/ubuntu 目录下创建源文件 reduce_mean.py,内容可参考:
示例代码:/home/ubuntu/reduce_mean.py
#!/usr/bin/python
import tensorflow as tf
import numpy as np
initial = [[1.,1.],[2.,2.]]
x = tf.Variable(initial,dtype=tf.float32)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
print sess.run(tf.reduce_mean(x))
print sess.run(tf.reduce_mean(x,0)) #Column
print sess.run(tf.reduce_mean(x,1)) #row
然后执行:
python /home/ubuntu/reduce_mean.py
执行结果:
1.5
[ 1.5 1.5]
[ 1. 2.]
您也可以尝试修改源代码看看输出结果有什么变化?
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 | 运算名称 |
示例代码:
现在您可以在 /home/ubuntu 目录下创建源文件 squared_difference.py,内容可参考:
示例代码:/home/ubuntu/squared_difference.py
#!/usr/bin/python
import tensorflow as tf
import numpy as np
initial_x = [[1.,1.],[2.,2.]]
x = tf.Variable(initial_x,dtype=tf.float32)
initial_y = [[3.,3.],[4.,4.]]
y = tf.Variable(initial_y,dtype=tf.float32)
diff = tf.squared_difference(x,y)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
print sess.run(diff)
然后执行:
python /home/ubuntu/squared_difference.py
执行结果:
[[ 4. 4.]
[ 4. 4.]]
您也可以尝试修改源代码看看输出结果有什么变化?
tf.square
square(
x,
name=None
)
功能说明:
计算张量对应元素平方
参数列表:
| 参数名 | 必选 | 类型 | 说明 |
|---|---|---|---|
| x | 是 | 张量 | 是 half, float32, float64, int32, int64, complex64, complex128 其中一种类型 |
| name | 否 | string | 运算名称 |
示例代码:
现在您可以在 /home/ubuntu 目录下创建源文件 square.py,内容可参考:
示例代码:/home/ubuntu/square.py
#!/usr/bin/python
import tensorflow as tf
import numpy as np
initial_x = [[1.,1.],[2.,2.]]
x = tf.Variable(initial_x,dtype=tf.float32)
x2 = tf.square(x)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
print sess.run(x2)
然后执行:
python /home/ubuntu/square.py
执行结果:
[[ 1. 1.]
[ 4. 4.]]
您也可以尝试修改源代码看看输出结果有什么变化?
TensorFlow 相关类理解
任务时间:时间未知
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 | 要是设置了,那么初始的值会是这种维度 |
示例代码:
现在您可以在 /home/ubuntu 目录下创建源文件 Variable.py,内容可参考:
示例代码:/home/ubuntu/Variable.py
#!/usr/bin/python
import tensorflow as tf
initial = tf.truncated_normal(shape=[10,10],mean=0,stddev=1)
W=tf.Variable(initial)
list = [[1.,1.],[2.,2.]]
X = tf.Variable(list,dtype=tf.float32)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
print ("##################(1)################")
print sess.run(W)
print ("##################(2)################")
print sess.run(W[:2,:2])
op = W[:2,:2].assign(22.*tf.ones((2,2)))
print ("###################(3)###############")
print sess.run(op)
print ("###################(4)###############")
print (W.eval(sess)) #computes and returns the value of this variable
print ("####################(5)##############")
print (W.eval()) #Usage with the default session
print ("#####################(6)#############")
print W.dtype
print sess.run(W.initial_value)
print sess.run(W.op)
print W.shape
print ("###################(7)###############")
print sess.run(X)
然后执行:
python /home/ubuntu/Variable.py
完成
任务时间:时间未知
恭喜,您已完成本实验内容
您可进行更多 TensorFlow 的系列教程:
- TensorFlow - 线性回归
- TensorFlow - 基于 CNN 数字识别
关于 TensorFlow 的更多资料可参考 TensorFlow 官网 。
TensorFlow — 相关 API的更多相关文章
- TensorFlow - 相关 API
来自:https://cloud.tencent.com/developer/labs/lab/10324 TensorFlow - 相关 API TensorFlow 相关函数理解 任务时间:时间未 ...
- tensorflow相关API的学习
学习目录 1.tensorflow相关函数理解 (1)tf.nn.conv2d (2)tf.nn.relu (3)tf.nn.max_pool (4)tf.nn.dropout (5)tf.nn.si ...
- 主要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 ...
- 开源框架---通过Bazel编译使用tensorflow c++ API 记录
开源框架---通过Bazel编译使用tensorflow c++ API 记录 tensorflow python API,在python中借用pip安装tensorflow,真的很方便,几句指令就完 ...
- OpenGL FrameBufferCopy相关Api比较(glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D)
OpenGL FrameBufferCopy相关Api比较 glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D 标题所述 ...
- QQ音乐的各种相关API
QQ音乐的各种相关API 分类: oc2014-01-29 15:34 2676人阅读 评论(2) 收藏 举报 基本上论坛里做在线音乐的都在用百度的API,进来发现百度的API不仅歌曲的质量不可以保证 ...
- addChildViewController相关api深入剖析
注:本文根据个人的实践和理解写成,若有不当之处欢迎斧正和探讨! addChildViewController是一个从iOS5开始支持的api接口,相关的一系列的接口是用来处理viewcontrolle ...
- [原创]java WEB学习笔记44:Filter 简介,模型,创建,工作原理,相关API,过滤器的部署及映射的方式,Demo
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 关于iOS中用AudioFile相关API解码或播放AAC_HE_V2时仅仅能识别单声首22.05k採样率的问题
关于iOS中用AudioFile相关API解码或播放AAC_HE_V2时仅仅能识别单声首22.05k採样率的问题 在官方AQPlayer Demo 和 aqofflinerender中.都用了Audi ...
随机推荐
- 基于Linux系统的Shell编程-基础篇
1. Shell基础介绍 1.1 Shell编程的意义 为什么使用shell编程 节约时间 1.2 显示脚本执行过程 前面有+表示执行过的命令的 前面没有东西,表示输出到屏幕上的内容. [root@C ...
- php 文件上传限制修改
修改PHP上传文件大小限制的方法 1. 一般的文件上传,除非文件很小.就像一个5M的文件,很可能要超过一分钟才能上传完.但在php中,默认的该页最久执行时间为 30 秒.就是说超过30秒,该脚本就停止 ...
- 【杂谈】HTML5到底给了我们什么?迟到的2016年终总结
其实提笔的时候日期已经到了3月了,不过由于在过去的2016年笔者发生了蛮多的事情,所以还是决定记录一下,那些关于成长的片段. 其实HTML5是在2012年的时候接触的,当时和结果志趣相投的同事,看到了 ...
- srand()
//第一次:5 0 第二次:5 16 srand(); //seed为常数,则每次运行产生的随机数一样 printf(); //产生的随机数都是一样的,都是5 srand(time(NULL)); p ...
- [Code+#1]大吉大利,晚上吃鸡!
输入输出样例 输入样例#1: 7 7 1 7 1 2 2 2 4 2 4 6 2 6 7 2 1 3 2 3 5 4 5 7 2 输出样例#1: 6 输入样例#2: 5 5 1 4 1 2 1 1 3 ...
- HDU 4135 容斥原理
思路: 直接容斥 //By SiriusRen #include <cstdio> using namespace std; #define int long long ; int cas ...
- E - Cheap Kangaroo(求多个数的最大公约数)
Description There are N kangaroos going out to eat at an Indian restaurant. The ith kangaroo wants t ...
- 391 Perfect Rectangle 完美矩形
有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域.每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( ...
- Storm编程入门API系列之Storm的定时任务实现
概念,见博客 Storm概念学习系列之storm的定时任务 Storm的定时任务,分为两种实现方式,都是可以达到目的的. 我这里,分为StormTopologyTimer1.java 和 Sto ...
- 设置Echarts鼠标悬浮样式
在option下 tooltip内添加以下代码: (本文在后台进行传值 也就是其中的viewstate[]) tooltip: { show: true, trigger: 'axis', //sho ...