tensorflow基本用法个人笔记
综述
TensorFlow程序分为构建阶段和执行阶段。通过构建一个图、执行这个图来得到结果。
构建图
创建源op,源op不需要任何输入,例如常量constant
,源op的输出被传递给其他op做运算。
import tensorflow as tf
# 创建一个常量 op, 产生一个 1x2 矩阵. 这个 op 被作为一个节点
matrix1 = tf.constant([[3., 3.]])
# 创建另外一个常量 op, 产生一个 2x1 矩阵.
matrix2 = tf.constant([[2.],[2.]])
# 创建一个矩阵乘法 matmul op , 把 'matrix1' 和 'matrix2' 作为输入.
# 返回值 'product' 代表矩阵乘法的结果.
product = tf.matmul(matrix1, matrix2)
在一个会话中启动图
构造图之后,需要创建Session
对象来启动图。
# 启动默认图.
sess = tf.Session()
#执行刚才的图
result = sess.run(product)
print result
# ==> [[ 12.]]
# 任务完成, 关闭会话.
sess.close()
使用“with”代码块来自动完成关闭动作。
with tf.Session() as sess:
result = seff.run([product])
print result
Tensorflow一般自动检测GPU来执行操作,并默认使用第一个GPU,因此有些情况可能需要手动指定GPU。with...Device
with tf.Session() as sess:
with tf.device("/gpu:1"):
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
...
"/cpu:0":机器的CPU
"/gpu:0":机器的第一个GPU
"/gpu:1":机器的第二个GPU
交互式使用
# 进入一个交互式 TensorFlow 会话.
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])
# 使用初始化器 initializer op 的 run() 方法初始化 'x'
x.initializer.run()
# 增加一个减法 sub op, 从 'x' 减去 'a'. 运行减法 op, 输出结果
sub = tf.sub(x, a)
print sub.eval()
# ==> [-2. -1.]
Tensor(张量)
TensorFlow程序使用tensor数据结构来代表所有数据。
变量
# 创建一个变量, 初始化为标量 0.
state = tf.Variable(0, name="counter")
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value) #将新值赋给变量state
# 启动图后, 变量必须先经过`初始化` (init) 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
Feed
input1 = tf.placeholder(tf.types.float32)
input2 = tf.placeholder(tf.types.float32)
#placeholder为临时占位符
output = tf.mul(input1, input2)
with tf.Session() as sess:
print sess.run([output], feed_dict={input1:[7.], input2:[2.]})
#这里通过feed_dict将数据填入刚才的占位符。
# 输出:
# [array([ 14.], dtype=float32)]
参考网址:TensorFlow中文社区
tensorflow基本用法个人笔记的更多相关文章
- TensorFlow机器学习框架-学习笔记-001
# TensorFlow机器学习框架-学习笔记-001 ### 测试TensorFlow环境是否安装完成-----------------------------```import tensorflo ...
- Tensorflow Summary用法
本文转载自:https://www.cnblogs.com/lyc-seu/p/8647792.html Tensorflow Summary用法 tensorboard 作为一款可视化神器,是学习t ...
- 第一节,TensorFlow基本用法
一 TensorFlow安装 TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tsnsor(张量)意味着N维数组,Flow(流)意味着基 ...
- tensorflow安装排坑笔记
由于项目需求,得用tensorflow完成,只能将mxnet的学习先放在一边,开始用tensorflow,废话不多说 首先安装anaconda+vs2015+cuda8.0+cudnn6.0 首先安装 ...
- 《TensorFlow实战》读书笔记(完结)
1 TensorFlow基础 ---1.1TensorFlow概要 TensorFlow使用数据流图进行计算,一次编写,各处运行. ---1.2 TensorFlow编程模型简介 TensorFlow ...
- Tensorflow Object_Detection 目标检测 笔记
Tensorflow models Code:https://github.com/tensorflow/models 编写时间:2017.7 记录在使用Object_Detection 中遇到的问题 ...
- tensorflow SavedModelBuilder用法
训练代码: # coding: utf-8 from __future__ import print_function from __future__ import division import t ...
- tensorflow add_to_collection用法
训练代码: # coding: utf-8 from __future__ import print_function from __future__ import division import t ...
- Ubuntu18.04下安装、测试tensorflow/models Tensorflow Object Detection API 笔记
参考:https://www.jianshu.com/p/1ed2d9ce6a88 安装 安装conda+tensorflow库 下载protoc linux x64版,https://github. ...
随机推荐
- Remember the Word (UVA-1402)
Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing ...
- python学习-文件创建读取
# 文件创建 # 读写# 文件存在?不存在?在操作系统上# 读 read r 写 write w# 打开一个文件# fs = open("xiaojian.txt",encodin ...
- Linux常见指令大全
转载自https://www.cnblogs.com/caozy/p/9261224.html 前言 本文特点 授之以渔:了解命令学习方法.用途:不再死记硬背,拒绝漫无目的: 准确无误:所有命令执行通 ...
- linux指令-date
1.在linux中要显示日期,则可以直接输入指令date 2.如果想以这样2016/12/26的方式输出呢,那就是,Y是年份,m是月份,d是日 date +%Y/%m/%d 3.如果要显示时间,则da ...
- Spire.Cloud 在线编辑
简介 Spire.Cloud在线编辑器是一款基于网页的 Office 文件编辑工具,支持在网页中打开.编辑.打印 Word.Excel.PPT 文件,支持将文档保存到私有云盘.支持 IE.Chrome ...
- Android.mk语法说明
版权申明: 本文原创首发于以下网站,您可以自由转载,但必须加入完整的版权声明 博客园:https://www.cnblogs.com/MogooStudio/ csdn博客:https://blog. ...
- React 组件的生命周期方法
React 组件的生命周期方法 按渲染顺序: 1: componentWillMount() – 在渲染之前执行,在客户端和服务器端都会执行. 2: componentDidMount() – 仅在第 ...
- CSS(3)---块级标签、行内标签、行内块标签
块级标签.行内标签.行内块标签 html中的标签元素三种类型:块级标签.行内标签.行内块标签. 一.概述 1.块级标签 概念 每个块元素通常都会独自占据一整行或多整行,可以对其设置宽度.高度.对齐等属 ...
- python函数编程-装饰器decorator
函数是个对象,并且可以赋值给一个变量,通过变量也能调用该函数: >>> def now(): ... print('2017-12-28') ... >>> l = ...
- Ubuntu 无法打开系统设置
最近不知道咋搞得,导致系统设置打不开,可能是系统输入法的问题吧,运行以下命令: sudo apt-get install gnome-control-center