1. 程式人生 > >tensorflow-計算圖(3)

tensorflow-計算圖(3)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 23 11:26:04 2018

@author: myhaspl
"""
import tensorflow as tf
#建立圖
c=tf.constant(0.0)
g=tf.Graph()
with g.as_default():
    c1=tf.constant(0.1)
g2=tf.get_default_graph()
print(g2)
tf.reset_default_graph()
g3=tf.get_default_graph()
print(g3)
print(c1.name)
t=g.get_tensor_by_name(name="Const:0")
print(t)
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)
tt2=g.get_operations()
print(tt2)

tt3=g.as_graph_element(c1)
print(tt3)

<tensorflow.python.framework.ops.Graph object at 0x7f8aaa8e5160>
<tensorflow.python.framework.ops.Graph object at 0x7f8a90a9a710>
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-12-25 17:26:56.179157: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
[[7.]]
Tensor("exampleop:0", shape=(1, 1), dtype=float32)
[<tf.Operation 'Const' type=Const>]
Tensor("Const:0", shape=(), dtype=float32)
[