集团站切换校区

验证码已发送,请查收短信

复制成功
微信号:togogoi
添加微信好友, 详细了解课程
已复制成功,如果自动跳转微信失败,请前往微信添加好友
打开微信
图标

学习文章

当前位置:首页 > >学习文章 > >

{HCNA-AI TensorFlow编程基础}之图操作

发布时间: 2018-12-27 16:31:22

7.1.1 实验介绍

7.1.2 关于本实验

本实验通过几个实例操作,了解计算图的相关操作。

7.1.3 实验目的

理解图的相关操作。

7.1.4 实验介绍

本例演示了如何建立图,并设置为默认图,使用    get_default_graph()方法获取当前默认图,验证默认图的设置生效。

演示获取图中的相关内容的操作。

7.1.5 实验步骤

步骤 1  登陆华为云。

步骤 2 点击右上方的控制台。

步骤 3 选择弹性云服务器,网页中会显示该弹性云的可进行的操作,选择远程登录。即登录到弹性云服务器。

步骤 4 输入指令:ll,查看当前目录下的文件。

步骤 5 输入命令:vi graph.py,创建新的 Python 脚本。

步骤 6 输入命令:i,进入编辑模式开始编辑,输入脚本内容。

步骤 7 输入命令:‘:wq!’,保存并退出。

步骤 8 输入命令:cat graph.py,查看代码。

步骤 9 运行测试。输入命令:python3 graph.py。


7.2 实验过程

# -*- coding: utf-8 -*- import numpy as np




import tensorflow as tf7.2.1 创建图# 1 创建图的方法

c = tf.constant(0.0)


g = tf.Graph()

with g.as_default(): c1 = tf.constant(0.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)7.2.2 获取 tensor# 2. 获取 tensor

print(c1.name)

t = g.get_tensor_by_name(name = "Const:0") print(t)7.2.3 获取 op# 3 获 取 op

a = tf.constant([[1.0, 2.0]])

b = tf.constant([[1.0], [3.0]])


tensor1 = tf.matmul(a, b, name='exampleop') print(tensor1.name,tensor1)

test = g3.get_tensor_by_name("exampleop:0") print(test)


print(tensor1.op.name)

testop = g3.get_operation_by_name("exampleop") print(testop)



with tf.Session() as sess: test = sess.run(test) print(test)

test = tf.get_default_graph().get_tensor_by_name("exampleop:0")




print (test)7.2.4 获取所有列表#4 获取所有列表

#返回图中的操作节点列表

tt2 = g.get_operations() print(tt2)7.2.5 获取对象#5 获取对象

tt3 = g.as_graph_element(c1) print(tt3)7.2.6 实验结果<tensorflow.python.framework.ops.Graph object at 0x0000000009B17940>

<tensorflow.python.framework.ops.Graph object at 0x0000000009B17940>

<tensorflow.python.framework.ops.Graph object at 0x0000000002892DD8>

<tensorflow.python.framework.ops.Graph object at 0x0000000002892DD8>

<tensorflow.python.framework.ops.Graph object at 0x0000000009B17A90> Const:0

Tensor("Const:0", shape=(), dtype=float32)

exampleop:0 Tensor("exampleop:0", shape=(1, 1), dtype=float32) Tensor("exampleop:0", shape=(1, 1), dtype=float32)

exampleop

name: "exampleop" op: "MatMul" input: "Const" input: "Const_1" attr {

key: "T" value {

type: DT_FLOAT

}

}

attr {

key: "transpose_a" value {

b: false

}

}

attr {

key: "transpose_b" value {

b: false

}

}






2018-05-11 15:43:16.483655: I

T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140]

Your CPU suppo

rts instructions that this TensorFlow binary was not compiled to use: AVX2 [[7.]]

Tensor("exampleop:0", shape=(1, 1), dtype=float32) [<tf.Operation 'Const' type=Const>] Tensor("Const:0", shape=(), dtype=float32)



7.3 实例描述

使用 tf.reset_default_graph 函数必须保证当前图的资源已经完全释放,否则会报错。可以通过名字得到对应的元素。Get_tensor_by_name 可以获取到图里的张量。

利用 get_operation_by_name 获取节点操作。利用 get_operations 函数获取元素列表。

根据对象获取元素即使用 tf.Graph.as_graph_element(),传入一个对象,返回一个张量或者

OP。函数 as_graph_element 获得了 c1 的真实张量对象,并赋给了变量 tt3。

本实验只介绍了图比较简单的操作

上一篇: {HTML5}DOM 和 CSS 操作-第四节-CSS 方法

下一篇: {HTML5}DOM 和 CSS 操作-第三节-元素属性操作

十五年老品牌
微信咨询:togogoi 咨询电话:18922156670 咨询网站客服:在线客服

相关课程推荐

在线咨询 ×

您好,请问有什么可以帮您?我们将竭诚提供最优质服务!