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的更多相关文章

  1. TensorFlow - 相关 API

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

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

  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. 开源框架---通过Bazel编译使用tensorflow c++ API 记录

    开源框架---通过Bazel编译使用tensorflow c++ API 记录 tensorflow python API,在python中借用pip安装tensorflow,真的很方便,几句指令就完 ...

  5. OpenGL FrameBufferCopy相关Api比较(glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D)

    OpenGL FrameBufferCopy相关Api比较 glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D 标题所述 ...

  6. QQ音乐的各种相关API

    QQ音乐的各种相关API 分类: oc2014-01-29 15:34 2676人阅读 评论(2) 收藏 举报 基本上论坛里做在线音乐的都在用百度的API,进来发现百度的API不仅歌曲的质量不可以保证 ...

  7. addChildViewController相关api深入剖析

    注:本文根据个人的实践和理解写成,若有不当之处欢迎斧正和探讨! addChildViewController是一个从iOS5开始支持的api接口,相关的一系列的接口是用来处理viewcontrolle ...

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

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

  9. 关于iOS中用AudioFile相关API解码或播放AAC_HE_V2时仅仅能识别单声首22.05k採样率的问题

    关于iOS中用AudioFile相关API解码或播放AAC_HE_V2时仅仅能识别单声首22.05k採样率的问题 在官方AQPlayer Demo 和 aqofflinerender中.都用了Audi ...

随机推荐

  1. 32.ExtJS简单的动画效果

    转自:http://blog.sina.com.cn/s/blog_74684ec501015lhq.html 说明:这篇文章的大部分内容来源于网上,经过自己实现其效果后,整理如下: 在进行 Java ...

  2. 10.24afternoon清北学堂刷题班

    /* 这是什么题... */ #include<iostream> #include<cstdio> #include<cstring> #include<q ...

  3. bzoj3450 Easy(概率期望dp)

    3450: Tyvj1952 Easy Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 876  Solved: 648[Submit][Status] ...

  4. 【Java】3到5年开发常见的Java面试题

    一.Java基础和高级 String类为什么是final的. HashMap的源码,实现原理,底层结构. 反射中,Class.forName和classloader的区别 session和cookie ...

  5. Asp.NET 知识点总结(一)

    1.简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有类,私有成员, 在类的内部才可以访问. protected : ...

  6. 无法收集统计信息,怎样优化SQL。

    特殊情况如下 客户的统计信息是固定的,没办法收集统计信息 . SQL profile 是最后考虑方案,因为同样写法sql 比较多,几十条. Parallle 并行客户一般不考虑接受,OLTP 系统. ...

  7. cocos2d-x lua中实现异步加载纹理

    原文地址:  http://www.cnblogs.com/linchaolong/p/4033118.html 前言   问题:最近项目中需要做一个loading个界面,界面中间有一个角色人物走动的 ...

  8. RabbitMQ二:AMQP协议

    参考这个:http://kb.cnblogs.com/page/73759/ 参考这个:http://www.cnblogs.com/charlesblc/p/6286875.html 写的挺好 Ra ...

  9. Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1

    记录一次傻逼的问题, 自己把自己蠢哭:Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1 错误说明: 返回的1是影响的行数,并不是自增的主键id: 想要获取自增主键id,需要 ...

  10. UVM基础之---------uvm factory机制base

    从名字上面就知道,uvm_factory用来制造uvm_objects和component.在一个仿真过程中,只有一个factory的例化存在. 用户定义的object和component types ...