TensorFlow——Graph的基本操作
1.创建图
在tensorflow中,一个程序默认是建立一个图的,除了系统自动建立图以外,我们还可以手动建立图,并做一些其他的操作。
下面我们使用tf.Graph函数建立图,使用tf.get_default_graph函数来获取图,使用reset_default_graph对图进行重置。
import tensorflow as tf
import numpy as np c = tf.constant(1.5)
g = tf.Graph() with g.as_default(): c1 = tf.constant(2.0)
print(c1.graph)
print(g)
print(c.graph) g2 = tf.get_default_graph()
print(g2) tf.reset_default_graph()
g3 = tf.get_default_graph()
print(g3)
上述的代码运行结果如下所示:

根据上述的运行结果,c是在刚开始的默认图中建立的,所以打印的结果就是13376A1FE10,和g2获取的默认图的值是一样的,然后使用tf.Graph建立了一个新的图,并添加了变量c1,最后又对图进行了重置,替代了原来的默认图。
在使用reset_default_graph()函数的时候,要保证当前图中资源都已经全部进行了释放,否则将会报错。
2.获取张量
我们可以在图中通过名字得到其对应的元素,比如获取图中的变量和OP等元素。
import tensorflow as tf
import numpy as np g = tf.Graph() with g.as_default():
c1 = tf.constant(2.5, name='c1_constant')
c2 = tf.Variable(1.5, dtype=tf.float32, name='c2_constant')
add = tf.multiply(c1, c2, name='op_add') c_1 = g.get_tensor_by_name(name='c1_constant:0')
c_2 = g.get_tensor_by_name(name='c2_constant:0')
c_3 = g.get_tensor_by_name(name='op_add:0') print(c_1)
print(c_2)
print(c_3)

在进行测试时,我们为元素添加了变量名,在设置变量名的时候,设置好的名字会自动添加后面的:0字符。一般我们可以将名字打印出来,在将打印好的名字进行回填。
3.获取节点操作
获取节点操作OP的方法和获取张量的方法非常类似,使用get_operation_by_name.下面是运行实例:
import tensorflow as tf
import numpy as np a = tf.constant([[1.0, 2.0]])
b = tf.constant([[1.0], [3.0]]) tensor_1 = tf.matmul(a, b, name='matmul_1') with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
t1 = tf.get_default_graph().get_operation_by_name(name='matmul_1')
t2 = tf.get_default_graph().get_tensor_by_name(name='matmul_1:0')
print(t1)
print('t1: ', sess.run(t1))
print('t2: ', sess.run(t2))
在上述的代码中,定义了一个OP操作,命名为matmul_1,在运行时我们将op打印出来,在使用名字后面加上:0我们就能得到OP运算的结果的tensor,注意这两者的区别。
我们还可以通过get_opreations函数获取图中的所有信息。此外,我们还可以使用tf.Grapg.as_graph_element函数将传入的对象返回为张量或者op。该函数具有验证和转换功能。
TensorFlow——Graph的基本操作的更多相关文章
- 第四节:tensorflow图的基本操作
基本使用 使用图(graph)来表示计算任务 激活会话(Session)执行图 使用张量(tensor)表示数据 定义变量(Variable) 使用feed可以任意赋值或者从中获取数据,通常与占位符一 ...
- Tensorflow学习笔记2:About Session, Graph, Operation and Tensor
简介 上一篇笔记:Tensorflow学习笔记1:Get Started 我们谈到Tensorflow是基于图(Graph)的计算系统.而图的节点则是由操作(Operation)来构成的,而图的各个节 ...
- TensorFlow 初级教程(三)
TensorFlow基本操作 import os import tensorflow as tf os.environ[' # 使用TensorFlow输出Hello # 创建一个常量操作( Cons ...
- TensorFlow白皮书
TensorFlow [1] is an interface for expressing machine learning algorithms, and an implementation for ...
- Tensorflow学习笔记4:分布式Tensorflow
简介 Tensorflow API提供了Cluster.Server以及Supervisor来支持模型的分布式训练. 关于Tensorflow的分布式训练介绍可以参考Distributed Tenso ...
- TensorFlow 深度学习笔记 TensorFlow实现与优化深度神经网络
转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地址 视频/字幕下载 全 ...
- TensorFlow实现与优化深度神经网络
TensorFlow实现与优化深度神经网络 转载请注明作者:梦里风林Github工程地址:https://github.com/ahangchen/GDLnotes欢迎star,有问题可以到Issue ...
- 【译】TensorFlow Python Docs 之 overview
Overview 综述 TensorFlow has APIs available in several languages both for constructing and executing a ...
- Tensorflow实践:CNN实现MNIST手写识别模型
前言 本文假设大家对CNN.softmax原理已经比较熟悉,着重点在于使用Tensorflow对CNN的简单实践上.所以不会对算法进行详细介绍,主要针对代码中所使用的一些函数定义与用法进行解释,并给出 ...
随机推荐
- kindeditor编辑器微软雅黑样式font-family值变成"
http://www.100cm.cn/article-126-764.html kindeditor编辑器中选中文字, 修改字体(字体名称中带有空格, 例如"Microsoft YaHei ...
- Codeforces Round #189 (Div. 1 + Div. 2)
A. Magic Numbers 不能出现连续的3个4,以及1.4以外的数字. B. Ping-Pong (Easy Version) 暴力. C. Malek Dance Club 考虑\(x\)二 ...
- tf.reduce_sum()函数
1234567reduce_sum 是 tensor 内部求和的工具.其参数中: input_tensor 是要求和的 tensor axis 是要求和的 rank,如果为 none,则表示所有 ra ...
- H3C 更新发送全部路由表浪费网络资源
- 【u227】BOOK
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 陈老师喜欢网购书籍,经常一次购它个百八十本,然后拿来倒卖,牟取暴利.前些天,高一的新同学来了,他便像往 ...
- 《代码整洁之道 中文版》高清 PDF 电子书下载
代码整洁之道.PDF 下载 代码整洁之道.PDF 中文版 高清 PDF 电子书下载 代码整洁之道下载 点我下载 作者简介 · · · · · · Robert C. Martin,Object ...
- Vue v-if和v-show的使用.区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- el-table翻页序号不从1开始(已解决)
法一:赋值方式(亲测有效) <el-table-column type="index" fixed="left" align="center&q ...
- 【React】富文本编辑器 清空文本内容 获取HTML
富文本编辑器 React 传入 import React,{Component } from 'react'; import { Card, Button, Table, Form, Select ...
- SpringSide 3 中的安全框架
在SpringSide 3的官方文档中,说安全框架使用的是Spring Security 2.0.乍一看,吓了我一跳,以为Acegi这么快就被淘汰了呢.上搜索引擎一搜,发现原来Spring Secur ...