TensorFlow2:基础组件

TensorFlow由两部分组成,Tensor(张量) 和 Flow(流,计算图)

1.Tensor(张量)

In TensorFlow, data isn’t stored as integers, floats, or strings. These values are encapsulated in an object called a tensor.

TensorFlow使用Tensor来表示所有的数据,Tensor可以理解为N维数组或列表。

在这里插入图片描述

2.Session

TensorFlow’s api is built around the idea of a computational graph, a way of visualizing a mathematical process. Let’s take the TensorFlow code you ran and turn that into a graph:

在这里插入图片描述

A “TensorFlow Session”, as shown above, is an environment for running a graph. The session is in charge of allocating the operations to GPU(s) and/or CPU(s), including remote machines.

The code creates a session instance, sess, using tf.Session. The sess.run() function then evaluates the tensor and returns the results.

Tensor是一个张量,用于存放各种数据,多个Tensor按计算图组织数据关系,sess会话调用run()执行计算,促使Tensor按计算图流动,得到计算结果,是为Tensor Flow。

More in :
https://medium.com/tebs-lab/deep-neural-networks-as-computational-graphs-867fcaa56c9?

3.常量

import tensorflow as tf
import os

#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' #默认显示所有信息
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 只显示警告和错误
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # 只显示错误

hello = tf.constant('hello')
n = tf.constant(99)

print(hello)
print(n)
print(type(hello))

Tensor(“Const_12:0”, shape=(), dtype=string)
Tensor(“Const_13:0”, shape=(), dtype=int32)
<class ‘tensorflow.python.framework.ops.Tensor’>

session = tf.Session()
print(session.run(hello))

b’hello’

print(str(session.run(hello),'utf-8'))

hello

print(session.run(n))

99

指定常量的数据类型

str_value = tf.constant('hello tensorflow!', dtype=tf.string)
print(str(session.run(str_value),'utf-8'))

hello tensorflow!


print(session.run(tf.constant(32,tf.int32)))

32

常量运算

x = tf.constant(20)
y = tf.constant(60)

session = tf.Session()
add = tf.add(x,y)
sub = tf.subtract(x,y)
mul = tf.multiply(x,y)
div = tf.divide(x,y)
print(session.run(add))
print(session.run(sub))
print(session.run(mul))
print(session.run(div))

80
-40
1200
0.3333333333333333

# x*x + x*y + 10
result = x * x + x*y +10
value = tf.constant(20)
print(session.run(result))

1610

4.占位符,Input

不指定值,可以重用tensor,必须指定类型;

Tf.placeholder: 传入普通变量,返回一个tensor,tensor用于tf.session.run()。

如果feed_dict与tensor类型不匹配且不能强制转换, you’ll get the error “ValueError: invalid literal for…”.

input1 = tf.placeholder(tf.int32)
input2 = tf.placeholder(tf.int32)

output = tf.add(input1, input2)
session = tf.Session()

# print(session.run(output)) 直接运行,出错
print(session.run(output, feed_dict={input1:2, input2:3}))
print(session.run(output, feed_dict={input1:10, input2:20}))
print(session.run(output, feed_dict={input1:[1,2,3], input2:[2,3,4]}))

5
30
[3 5 7]

5.变量

x = tf.Variable(2)
y = tf.Variable(3)

f = x*x + y +3

session = tf.Session()
session.run(x.initializer) # 必须调用run, 初始化变量
session.run(y.initializer)
session.run(y)

3

session.run(f)

10

x.load(10,session)
y.load(20,session)
session.run(y)

20

session.run(f)

123

session.close()
with tf.Session() as session:
    y.initializer.run()
    x.initializer.run()
    r = f.eval() #
print(r)

10

并不会一下子初始化,而是在计算图中创建一个节点,这个节点会在绘画执行时初始化所有变量;惰性初始化

一个TensorFlow程序分为两部分:
1.用于构建计算图的部分;
2.用于执行计算图部分;
我们的主要工作是构建计算图。

init = tf.global_variables_initializer()
with tf.Session() as sess:
    init.run()
    ret = f.eval() #执行计算图
print(ret)

10

6.TensorFlow运算函数

官网API: https://www.tensorflow.org/api_docs/python/tf/math

import tensorflow as tf
import os

#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' #默认显示所有信息
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 只显示警告和错误
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # 只显示错误

x = tf.Variable(10)
y = tf.Variable(20)
z = tf.Variable(-5)

value = tf.Variable(2.5,dtype=tf.float32)
init = tf.global_variables_initializer()

with tf.Session() as session:
    init.run()
    print(session.run(tf.add(x,y)))
    print(session.run(tf.subtract(x,y)))
    print(session.run(tf.multiply(x,y)))
    print(session.run(tf.divide(x,y)))
    print(session.run(tf.mod(x,y)))
    print(session.run(tf.abs(x)))
    print(session.run(tf.negative(x)))
    print(session.run(tf.square(x)))
    print(session.run(tf.sqrt(value))) #不能是整数
    print(session.run(tf.sign(z)))
    print(session.run(tf.sin(value))) #float

30
-10
200
0.5
10
10
-10
100
1.5811388
-1
0.5984721

类型转换

tf.subtract(tf.constant(2.0),tf.constant(1)) # Fails with ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32:

保持表达式类型一致,或强制转换

tf.subtract(tf.cast(tf.constant(2.0), tf.int32), tf.constant(1)) # 1

7.管理计算图

import tensorflow as tf
import os

#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' #默认显示所有信息
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 只显示警告和错误
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # 只显示错误

sess = tf.Session()
x1 = tf.Variable(5, name='x1')
sess.run(x1.initializer)

print(tf.get_default_graph().get_operation_by_name('x1'))
print(tf.get_default_graph().get_operation_by_name('x1').get_attr('dtype'))
print(x1.graph is tf.get_default_graph())

#创建一个新计算图
graph = tf.Graph()
with graph.as_default():
    x2 = tf.Variable(20)
    print(x2.graph is graph)

print(x2.graph is tf.get_default_graph())

name: “x1”
op: “VariableV2”
attr {
key: “container”
value {
s: “”
}
}
attr {
key: “dtype”
value {
type: DT_INT32
}
}
attr {
key: “shape”
value {
shape {
}
}
}
attr {
key: “shared_name”
value {
s: “”
}
}

<dtype: ‘int32’>
True
True
False

8.计算图节点的依赖与生命周期

import tensorflow as tf
import os

#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' #默认显示所有信息
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 只显示警告和错误
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # 只显示错误

k = tf.constant(333)
x = k + 1
y = k + 2
z = x + 5

# 依赖重复计算两次
with tf.Session() as sess:
    print(y.eval())
    print(z.eval())
#依赖只计算一次
with tf.Session() as sess:
    y_val, z_val = sess.run([y,z])
    print(y_val, z_val)
#在计算图每次执行时,所有节点都会被丢弃,但变量不会,变量是由Session维护,除非关闭Session,否则变量一直存在

335
339
335 339

发布了263 篇原创文章 · 获赞 23 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/luteresa/article/details/103834717