ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2-1张量数据结构——eat_tensorflow2_in_30_days

2022-04-03 17:33:44  阅读:202  来源: 互联网

标签:tensorflow2 constant dtype 30 days 张量 print tf numpy


程序=数据结构+算法

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

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

TensorFlow的基本数据结构是张量Tensor。张量即多维数组。TensorFlow的张量和NumPy中的ndarray很类似

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

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

常量张量

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

import numpy as np
import tensorflow as tf

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

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)
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])
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]])
print(tf.rank(matrix))
print(np.ndim(matrix.numpy()))

# tf.Tensor(2, shape=(), dtype=int32)
# 2
tensor3 = tf.constant([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]])
print(tf.rank(tensor3))
print(np.ndim(tensor3.numpy))
print(tensor3.shape)

# tf.Tensor(3, shape=(), dtype=int32)
# 0
# (2, 2, 2)
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(tf.rank(tensor4))
print(tensor4.numpy().shape)

# tf.Tensor(4, shape=(), dtype=int32)
# (2, 2, 2, 2)

可以用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())
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, 2.0])
print(c)
print(id(c))

"""
tf.Tensor([1. 2.], shape=(2,), dtype=float32)
2679663757448
tf.Tensor([2. 4.], shape=(2,), dtype=float32)
2679663757800
"""
# 变量的值可以改变,可以通过assign, assign_add等方法给变量重新赋值
v = tf.Variable([1.0, 2.0], name='v')
print(v)
print(id(v))
v.assign_add([1.0, 2.0])
print(v)
print(id(v))

"""
<tf.Variable 'v:0' shape=(2,) dtype=float32, numpy=array([1., 2.], dtype=float32)>
2679663861832
<tf.Variable 'v:0' shape=(2,) dtype=float32, numpy=array([2., 4.], dtype=float32)>
2679663861832
"""

标签:tensorflow2,constant,dtype,30,days,张量,print,tf,numpy
来源: https://www.cnblogs.com/lotuslaw/p/16096515.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有