30天干掉tensorflow2.0-day05

TensorFlow的核心概念

TensorFlow™ 是一个采用 数据流图(data flow graphs),用于数值计算的开源软件库。节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组,即张量(tensor)。它灵活的架构让你可以在多种平台上展开计算,例如台式计算机中的一个或多个CPU(或GPU),服务器,移动设备等等。TensorFlow 最初由Google大脑小组(隶属于Google机器智能研究机构)的研究员和工程师们开发出来,用于机器学习和深度神经网络方面的研究,但这个系统的通用性使其也可广泛用于其他计算领域。

TensorFlow的主要优点:

灵活性:支持底层数值计算,C++自定义操作符

可移植性:从服务器到PC到手机,从CPU到GPU到TPU

分布式计算:分布式并行计算,可指定操作符对应计算设备

俗话说,万丈高楼平地起,TensorFlow这座大厦也有它的地基。

Tensorflow底层最核心的概念是张量,计算图以及自动微分。

张量数据结构

程序 = 数据结构+算法。

TensorFlow程序 = 张量数据结构 + 计算图算法语言

张量和计算图是 TensorFlow的核心概念。

Tensorflow的基本数据结构是张量Tensor。张量即多维数组。Tensorflow的张量和numpy中的array很类似。

从行为特性来看,有两种类型的张量,常量constant和变量Variable.

常量的值在计算图中不可以被重新赋值,变量可以在计算图中用assign等算子重新赋值。

一,常量张量

张量的数据类型和numpy.array基本一一对应。

import numpy as np
import tensorflow as tf

# tf.int32 类型常量
i = tf.constant(1)
# tf.int64 类型常量
l = tf.constant(1,dtype = tf.int64)
#tf.float32 类型常量
f = tf.constant(1.23)
# tf.double 类型常量
d = tf.constant(3.14,dtype = tf.double)
# tf.string类型常量
s = tf.constant("hello world")
#tf.bool类型常量
b = tf.constant(True)


print(tf.int64 == np.int64) 
print(tf.bool == np.bool)
print(tf.double == np.float64)
print(tf.string == np.unicode)
# tf.string类型和np.unicode类型不等价
True
True
True
False

不同类型的数据可以用不同维度(rank)的张量来表示。

标量为0维张量,向量为1维张量,矩阵为2维张量。

彩色图像有rgb三个通道,可以表示为3维张量。

视频还有时间维,可以表示为4维张量。

可以简单地总结为:有几层中括号,就是多少维的张量。

scalar = tf.constant(True)  #标量,0维张量

print(tf.rank(scalar))
print(scalar.numpy().ndim)  # tf.rank的作用和numpy的ndim方法相同
tf.Tensor(0, shape=(), dtype=int32)
0
scalar = tf.constant(3)  #标量,0维张量

print(tf.rank(scalar))
print(scalar.numpy().ndim)
tf.Tensor(0, shape=(), dtype=int32)
0
vector = tf.constant([1.0,2.0,3.0,4.0]) #向量,1维张量

print(tf.rank(vector))
print(np.ndim(vector.numpy()))
tf.Tensor(1, shape=(), dtype=int32)
1
matrix = tf.constant([[1.0,2.0],[3.0,4.0]]) #矩阵, 2维张量

print(tf.rank(matrix).numpy())
print(np.ndim(matrix))
2
2
tensor3 = tf.constant([[[1.0,2.0],[3.0,4.0]],[[5.0,6.0],[7.0,8.0]]])  # 3维张量
print(tensor3)
print(tf.rank(tensor3))
tf.Tensor(
[[[1. 2.]
  [3. 4.]]

 [[5. 6.]
  [7. 8.]]], shape=(2, 2, 2), dtype=float32)
tf.Tensor(3, shape=(), dtype=int32)
tensor4 = tf.constant([[[[1.0,1.0],[2.0,2.0]],[[3.0,3.0],[4.0,4.0]]],
                        [[[5.0,5.0],[6.0,6.0]],[[7.0,7.0],[8.0,8.0]]]])  # 4维张量
print(tensor4)
print(tf.rank(tensor4))
tf.Tensor(
[[[[1. 1.]
   [2. 2.]]

  [[3. 3.]
   [4. 4.]]]


 [[[5. 5.]
   [6. 6.]]

  [[7. 7.]
   [8. 8.]]]], shape=(2, 2, 2, 2), dtype=float32)
tf.Tensor(4, shape=(), dtype=int32)
  • 可以用tf.cast改变张量的数据类型。

  • 可以用numpy方法将tensorflow中的张量转化成numpy中的张量。

  • 可以用shape方法查看张量的尺寸。

h = tf.constant([123,456],dtype = tf.int32)
f = tf.cast(h,tf.float32)
print(h.dtype, f.dtype)
<dtype: 'int32'> <dtype: 'float32'>
y = tf.constant([[1.0,2.0],[3.0,4.0]])
print(y.numpy()) #转换成np.array
print(y.shape)
[[1. 2.]
 [3. 4.]]
(2, 2)
u = tf.constant(u"你好 世界")
print(u.numpy())  
print(u.numpy().decode("utf-8"))
b'\xe4\xbd\xa0\xe5\xa5\xbd \xe4\xb8\x96\xe7\x95\x8c'
你好 世界

二,变量张量

模型中需要被训练的参数一般被设置成变量。

# 常量值不可以改变,常量的重新赋值相当于创造新的内存空间
c = tf.constant([1.0,2.0])
print(c)
print(id(c))
c = c + tf.constant([1.0,1.0])
print(c)
print(id(c))
tf.Tensor([1. 2.], shape=(2,), dtype=float32)
2438466057320
tf.Tensor([2. 3.], shape=(2,), dtype=float32)
2438466058024
# 变量的值可以改变,可以通过assign, assign_add等方法给变量重新赋值,地址不会改变
v = tf.Variable([1.0,2.0],name = "v")
print(v)
print(id(v))
v.assign_add([1.0,1.0])
print(v)
print(id(v))
<tf.Variable 'v:0' shape=(2,) dtype=float32, numpy=array([1., 2.], dtype=float32)>
2438466063048
<tf.Variable 'v:0' shape=(2,) dtype=float32, numpy=array([2., 3.], dtype=float32)>
2438466063048
w = tf.Variable([1.0,2.0],name = "w")
print(w)
print(id(w))
w.assign([1.0,1.0])
print(w)
print(id(w))
<tf.Variable 'w:0' shape=(2,) dtype=float32, numpy=array([1., 2.], dtype=float32)>
2438467906952
<tf.Variable 'w:0' shape=(2,) dtype=float32, numpy=array([1., 1.], dtype=float32)>
2438467906952
print(tensor4.shape)
(2, 2, 2, 2)
print(tf.shape(tensor4))
tf.Tensor([2 2 2 2], shape=(4,), dtype=int32)
tensor4 = tf.constant([[[[1.0,1.0],[2.0,2.0]],[[3.0,3.0],[4.0,4.0]]],
                        [[[5.0,5.0],[6.0,6.0]],[[7.0,7.0],[8.0,8.0]]]])
print(id(tensor4))
print(tensor4)
tensor5 = tf.reshape(tensor4,(4,4))
print(tensor4)
print(id(tensor4))
print(tensor5)
print(id(tensor5))
2438465956328
tf.Tensor(
[[[[1. 1.]
   [2. 2.]]

  [[3. 3.]
   [4. 4.]]]


 [[[5. 5.]
   [6. 6.]]

  [[7. 7.]
   [8. 8.]]]], shape=(2, 2, 2, 2), dtype=float32)
tf.Tensor(
[[[[1. 1.]
   [2. 2.]]

  [[3. 3.]
   [4. 4.]]]


 [[[5. 5.]
   [6. 6.]]

  [[7. 7.]
   [8. 8.]]]], shape=(2, 2, 2, 2), dtype=float32)
2438465956328
tf.Tensor(
[[1. 1. 2. 2.]
 [3. 3. 4. 4.]
 [5. 5. 6. 6.]
 [7. 7. 8. 8.]], shape=(4, 4), dtype=float32)
2438466056440
print(id(tensor4))
t = tensor4.numpy().reshape((4,4))
print(t)
print(tensor4)
print(id(t))
2438465956328
[[1. 1. 2. 2.]
 [3. 3. 4. 4.]
 [5. 5. 6. 6.]
 [7. 7. 8. 8.]]
tf.Tensor(
[[[[1. 1.]
   [2. 2.]]

  [[3. 3.]
   [4. 4.]]]


 [[[5. 5.]
   [6. 6.]]

  [[7. 7.]
   [8. 8.]]]], shape=(2, 2, 2, 2), dtype=float32)
2438399390304
原创文章 58 获赞 7 访问量 6210

猜你喜欢

转载自blog.csdn.net/Elenstone/article/details/105379023