Tensorflow的基础用法
简介
Tensorflow是一个深度学习框架,它使用图(graph)来表示计算任务,使用tensor(张量)表示数据,图中的节点称为OP,在一个会话(Session)的上下文中执行运算,最终产生tensor。
之所以用计算图来表示计算任务,Tensorflow的官网的一张图片就能很好的说明。
tensor在数学中称为张量,表示空间,在计算图模型中就是基本的数据类型,如同我们在sklearn等机器学习框架中使用numpy的矩阵作为基本运算的数据类型一样,无论几维的矩阵都是一个张量
神经网络的前向传播本质上就是一张计算图进行计算。相比其他术语,也许计算图能更好的说明机器学习的本质。
Tensorflow的基本变量
tensor计算图谱的基本类型
- tensor 张量
- Variable 变量
- Constant 常量
- Placeholder 占位符
- Graph 图
- Session 会话
Constant常量
#tf.constant(value, dtype=None, shape=None,name="const") tf.constant(10)Variable变量
import tensorflow as tf
tf.varialbe(tf.zeros([1]))故名思意这个量在图的运算中可以发生改变
文档中的一个例子- tensor 张量
state = tf.Variable(0, name="counter")
# 创建一个 op, 其作用是使 state 增加 1
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)
# 启动图后, 变量必须先经过`初始化` (init) op 初始化,
# 首先必须增加一个`初始化` op 到图中.
init_op = tf.initialize_all_variables()
# 启动图, 运行 op
with tf.Session() as sess:
# 运行 'init' op
sess.run(init_op)
# 打印 'state' 的初始值
print sess.run(state)
# 运行 op, 更新 'state', 并打印 'state'
for _ in range(3):
sess.run(update)
print sess.run(state)
# 输出:
# 0
# 1
# 2
# 3
Placeholder
这个是暂时未定义的一个tensor
在计算图运行时给予,类似函数的参数。
python
input1 = tf.placeholder(tf.float32)
计算图
Tensorflow的程序一般分为两个阶段,构建阶段和执行极端。一般,构建阶段会创建一个图,来表示神经网络,在执行阶段在反复执行训练图。
可以把图的构建看成定义一个复杂的函数。
构建图
import tensorflow as tf
matrix1 = tf.Constant([[3.,3]])
matrix2 = tf.Constant([[2.],[2.]])
product = tf.matmul(matrix1,matrix2)
启动图
python
sess = tf.Session()
result = sess.run(product)
# 运行图定义的product运算
print(result)
sess.close
#执行完毕后关闭会话
还有另一种方法会使用python的特性
python
with tf.Session() as sess:
result = sess.run([product])
print(result)
补充
Fetch取回
在sess的运算中不仅可以取回结果,还可以取回多个tensor,在神经网络中,我们可以取回中间层的结果
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)
with tf.Session() as sess:
result = sess.run([mul, intermed])
print result
# 输出:
# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]
sess.run()接受的参数是个列表,输出会输出这个列表中的值
Feed供给
有时训练过程中我们会执行增量训练,这时就会使用前面介绍的Placeholder()
input1 = tf.placeholder(tf.float32)
input2 = tf.plaeholder(tf.float32)
output = tf.mul(input1, input2)
with tf.Session() as sess:
print(sess.run([ouput], feed_dict={input1:[7:],input2:[2.]})
参考资料:
TF-girls修炼指南
Tensorflow官方文档
Tensorflow的基础用法的更多相关文章
- 第五节,TensorFlow编程基础案例-session使用(上)
在第一节中我们已经介绍了一些TensorFlow的编程技巧;第一节,TensorFlow基本用法,但是内容过于偏少,对于TensorFlow的讲解并不多,这一节对之前的内容进行补充,并更加深入了解讲解 ...
- PropertyGrid控件由浅入深(二):基础用法
目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...
- logstash安装与基础用法
若是搭建elk,建议先安装好elasticsearch 来自官网,版本为2.3 wget -c https://download.elastic.co/logstash/logstash/packag ...
- elasticsearch安装与基础用法
来自官网,版本为2.3 注意elasticsearch依赖jdk,2.3依赖jdk7 下载rpm包并安装 wget -c https://download.elastic.co/elasticsear ...
- BigDecimal最基础用法
BigDecimal最基础用法 用字符串生成的BigDecimal是不会丢精度的. 简单除法. public class DemoBigDecimal { public static void mai ...
- Vue组件基础用法
前面的话 组件(Component)是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码.根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己所需, ...
- Smarty基础用法
一.Smarty基础用法: 1.基础用法如下 include './smarty/Smarty.class.php';//引入smarty类 $smarty = new Smarty();//实例化s ...
- 前端自动化测试神器-Katalon的基础用法
前言 最近由于在工作中需要通过Web端的功能进行一次大批量的操作,数据量大概在5000左右,如果手动处理, 完成一条数据的操作用时在20秒左右的话,大概需要4-5个人/天的工作量(假设一天8小时的工作 ...
- Bootstrap fileinput:文件上传插件的基础用法
官网地址:http://plugins.krajee.com/ 官网提供的样例:http://plugins.krajee.com/file-input/demo 基础用法一 导入核心CSS及JS文件 ...
随机推荐
- nginx的虚拟主机和反向代理和一些技巧应用
前言 nginx是什么,一般用来做反向代理,也可以用来放静态htnl等文件..当然代理tcp协议也没啥问题,但做好是别用哈.性能不咋样. 虚拟主机 什么是虚拟主机? 个人理解,比如一台主机装一个ngi ...
- Robot Framework使用技巧之内部变量
[转载] 1.变量的使用 变量可以在命令行中设置,个别变量设置使用--variable (-v)选项,变量文件的选择使用--variablefile (-V)选项. 通过命令行设置的变量是全局变量,对 ...
- swap的几点理解
一.什么是swap space(交换分区)? 在Linux系统中,当物理内存满了才使用Swap空间.当系统需要更多的内存资源,并且物理内存已经满了,此时,内存中那些不活跃的pages被移动(move) ...
- jQuery查阅api手册
原文&出处:jQuery API 3.3.1 速查表 --作者:Shifone http://jquery.cuishifeng.cn/
- 在windows下使用secure crt传文件到linux的主目录下
SECURT CRT上传文件 使用secure crt连接到Linux上 通过alt+p打开sftp服务 使用put D:\视觉\代码\ch.10.zip 即可传输完成 进入linux直接在主目录下可 ...
- 【leetcode】997. Find the Town Judge
题目如下: In a town, there are N people labelled from 1 to N. There is a rumor that one of these people ...
- 【和孩子一起学编程】 python笔记--第二天
第六章 GUI:用户图形界面(graphical user interface) 安装easygui:打开cmd命令窗口,输入:pip install easygui 利用msgbox()函数创建一个 ...
- c# 7.0新语法特性
public class NewAtturibute { public void TestFunc() { // 01 Out变量 不用初始化 "; if (int.TryParse(inp ...
- testNG之参数化测试
@Parameters 测试的时候,测试的数据可以放在testng.xml文件中,被测试的方法通过@Parameters注解传递到测试方法中 parameterizedTest.java import ...
- UVa 548 Tree (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...