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. ...
随机推荐
- Vue - watch高阶用法
1. 不依赖新旧值的watch 很多时候,我们监听一个属性,不会使用到改变前后的值,只是为了执行一些方法,这时可以使用字符串代替 data:{ name:'Joe' }, watch:{ name:' ...
- MySQL InnoDB 存储引擎原理浅析
注:本文主要基于MySQL 5.6以后版本编写,多数知识来着书籍<MySQL技术内幕++InnoDB存储引擎>,本文章仅记录个人认为比较重要的部分,有兴趣的可以花点时间读原书. 一.MyS ...
- stm32 io操作 头文件规范
在stm32众多项目开发中,有太多的对io进行操作,若置1或清0,使用官方库提供的函数,固然方便,规范,但是需要包含标准的库,尺寸较大,还得处理不同版本兼容问题,包括io初始化也太繁琐,于是操作原子等 ...
- Jmeter性能测试配置
目录 Jmeter检查点/断言 Jmeter事务 Jmeter集合点 Jmeter检查点/断言 在上一章节中,我们通过调试脚本,通过人工验证脚本可以完成业务功能, 但在性能测试中,我们希望能通过自动验 ...
- 比SecureCRT更好用的工具MobaXterm下载安装使用教程
一.下载 1.官网下载:下载地址 下载左边的免费版本 2.百度网盘下载:下载地址 提取码:tbge java开发工具下载地址及安装教程大全,点这里. 更多深度技术文章,在这里. 二.安装 1.双击安 ...
- git 中的 merge 和 rebase
示例分支:master . dev 把 dev 分支上的新内容合并到 master 上 先切换分支到master git checkout master 合并操作 git merge dev 或者 g ...
- 函数式编程 -> Lambda
一.函数式编程 函数式编程,同面向对象编程.指令式编程一样,是一种软件编程范式,在多种编程语言中都有应用.百科词条中有很学术化的解释,但理解起来并不容易.不过,我们可以借助于数学中函数的概念,来理解函 ...
- CCF-CSP题解 201503-3 节日
模拟题. 良心出题人并没有\(y_1>y_2\)的样例.注意闰年的处理. #include <bits/stdc++.h> using namespace std; int dayO ...
- 起言-----UE4学习方法
1.bilibili 2.官网教程 3.我觉得以上两个就够了 官方文档链接 https://docs.unrealengine.com/ 官网在线视频链接 https://learn.unrealen ...
- ELK查询命令详解
目录 ELK查询命令详解 倒排索引 使用ElasticSearch API 实现CRUD 批量获取文档 使用Bulk API 实现批量操作 版本控制 什么是Mapping? 基本查询(Query查询) ...