脚本宝典收集整理的这篇文章主要介绍了TensorFlow1,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
TensorFlow
Tensorflow是一个开源软件库,它使用数据流图的形式进行数值计算。
什么是数据流图(Data Flow Graph)
- 节点(Nodes):表示数学运算操作符
- 边(Edges):用于传送节点之间的多维数组,即张量。
安装TensorFlow
- CPU版本:pip install --upgrade tensorflow
- GPU版本:pip install --upgrade tensorflow-gpu
检查安装以及版本
>>> import tensorflow as tf
>>> tf.__version__
'1.3.0'
利用TensorFlow打印‘Hello World!’
# 创建一个constant运算符(op)
# 这个op,作为一个node,添加到graph中
hello = tf.constant('Hello, TensorFlow!')
# 启动TF进程(session)
sess = tf.Session()
# 运行op,并输出结果
print(sess.run(hello))
b'Hello, TensorFlow!'
b'String': 'b'表示字节符。
计算图(Computational Graph)
# 1.使用TensorFlow运算符op搭建graph
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0)
node3 = tf.add(node1, node2)
# 2.丢入数据,并运行graph:sess.run(op)
# 3.自动更新graph中的变量并返回
sess = tf.Session()
print('sess.run(node1, node2):', sess.run([node1, node2]))
print('sess.run(node3):', sess.run(node3))
sess.run(node1, node2): [3.0, 4.0]
sess.run(node3): 7.0
TensorFlow运行机制
- 1.使用TensorFlow运算符op搭建graph
- 2.丢入数据,并运行graph:sess.run(op)
- 3.自动更新graph中的变量并返回值
占位符(Placeholder)
# 1.使用TensorFlow运算符op搭建graph
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = tf.add(a, b)
# 2.丢入数据,并运行graph:sess.run(op, feed_dict{x: x_data})
# 3.自动更新graph中的变量并返回值
print(sess.run(adder_node, feed_dict={a: 3, b: 4.5}))
print(sess.run(adder_node, feed_dict={a: [1, 3], b: [2, 4]}))
7.5
[3. 7.]
TensorFlow机制
- 1.使用TensorFlow运算符op搭建graph
- 2.丢入数据,并运行graph:sess.run(op, feed_dict{x: x_data})
- 3.自动更新graph中的变量并返回值
万物皆张量(Everything is Tensor)
Tensors
3 # rank为0的张量,即shape为[]的表量
[1., 2., 3.] # rank为1的张量;shape为[3]的向量
[[1., 2., 3.], [4., 5., 6.]] # rank为2的张量;shape为[2, 3]的矩阵
[[[1., 2., 3.]], [[7., 8., 9.]]] # rank为3的张量,形状为[2, 1, 3]
张量的阶、形状和类型
Tersor Ranks, Shapes, and Types
阶(Ranks)
秩/阶
数学名称
Python实例
0
标量
s = 483
1
向量
v = [1., 2., 3.]
2
矩阵
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
3
3-Tensor
t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]
n
n-Tensor
...
形状(Shapes)
Rank
Shape
维数
示例
0
[]
0-D
0维Tensor; 标量
1
[D0]
1-D
形状为[5]的1维张量
2
[D0, D1]
2-D
形状为[3, 4]的2维张量
3
[D0, D1, D2]
3-D
形状为[1, 4, 3]的3维张量
n
[D0, D1, ..., Dn-1]
n-D
形状为[D0, D1, ..., Dn-1]的n维张量
类型(Types)
数据类型
Python数据类型
描述
FLOAT
tf.float32
32位浮点型
DOUBLE
tf.float64
64位浮点型
INT8
tf.int8
有符号8位整型
INT16
tf.int16
有符号16位整型
INT32
tf.int32
有符号32位整型
INT64
tf.int64
有符号64位整型
以上是脚本宝典为你收集整理的TensorFlow1全部内容,希望文章能够帮你解决TensorFlow1所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。