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 ...
随机推荐
- springboot根据yml配置文件选择性加载bean
@Slf4j @Aspect @Component @ConditionalOnProperty(value = "localCache.apiCache", havingValu ...
- vue-easytable
github地址:https://github.com/huangshuwei/vue-easytable
- 题解报告:hdu 2612 Find a way(双bfs)
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...
- 通过创建元素从而实现三个下拉框的联动效果(create.Element("option"))和提交表单时的验证p.match("请选择")
<html> <head> <meta charset="utf-8"> <title>下拉框</title> < ...
- js复制功能
// 复制功能 copyUrl() { var Url = document.getElementById('biao') Url.select() // 选择对象 document.execComm ...
- [书目20140824]触动人心:设计优秀的iPhone应用
关于作者致谢译者序入门设计令人欣喜且易用的应用等下……先吸口气阅读本书不需要专业知识忠言一切从点击开始 我们是如何使用iPhone应用的行走中:一只手,一只眼睛,一直在抖动尽快搞定满满一箱 ...
- [转]windows 7 下快速搭建php环境(windows7+IIS7+php+mysql)
转贴:http://apps.hi.baidu.com/share/detail/10406992 (1).采用理由: 优点:最大化的桌面图形化操作系统,可维护性优秀.基于IIS v6.0/v7.0( ...
- RabbitMQ三:Rabbit的安装
本章文章,摘自 园友 章为忠 的文章,查找了很多资料,他总结的最细,最全面,我就直接拿过来了 他的原文 http://www.cnblogs.com/zhangweizhong/p/5689209.h ...
- 构建一个.net的干货类库,以便于快速的开发 - 前言
前言: 工作已经快两年了,项目也做过不少,不知道大家有没有一个习惯,就是把在做项目过程中那些好的方法记录下来.我觉得这个习惯在开发的过程中还是很有用的,举个例子,我之前做过一个支付宝的支付功能,然后把 ...
- 纵横填字js
新数据结构设计: 定义一个map: key是横纵坐标字符串,比如“0,4” value是一个json,包含以下属性:字,横向的词(若 有的话,无的话,空串),纵向的词(若有的话,无的话,空串). 另有 ...