tensorflow核心和语言支持的API
流动过程
在TensorFlow系统中,张量的维数来被描述为阶.但是张量的阶和矩阵的阶并不是同一个概念.张量的阶(有时是关于如顺序或度数或者是n维)是张量维数的一个数量描述.比如,下面的张量(使用Python中list定义的)就是2阶.
t=[[1,2,3],[4,5,6],[7,8,9]]
在tensorlflow中,有几类特别的张量
变量:tf创建了可以修改了 多数用Variable创建张量
0阶张量
# 0阶张量import tensorflow as tfmammal = tf.Variable("maoli", tf.string)ignition = tf.Variable(20, tf.int16)floating = tf.Variable(3.14159265359, tf.float64)its_complicated = tf.Variable(12.3 - 4.85j, tf.complex64)[mammal, ignition, floating, its_complicated]OUT:[<tf.Variable 'Variable:0' shape=() dtype=string, numpy=b'maoli'>, <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=20>, <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=3.1415927>, <tf.Variable 'Variable:0' shape=() dtype=complex128, numpy=(12.3-4.85j)>]
1阶张量
# 1阶张量mystr = tf.Variable(["Hello", "World"], tf.string)cool_numbers = tf.Variable([3.14159, 2.71828], tf.float32)first_primes = tf.Variable([2, 3, 5, 7, 11], tf.int32)its_very_complicated = tf.Variable([12.3 - 4.85j, 7.5 - 6.23j], tf.complex64)[mystr, cool_numbers, first_primes, its_very_complicated]OUT:[<tf.Variable 'Variable:0' shape=(2,) dtype=string, numpy=array([b'Hello', b'World'], dtype=object)>, <tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array([3.14159, 2.71828], dtype=float32)>, <tf.Variable 'Variable:0' shape=(5,) dtype=int32, numpy=array([ 2, 3, 5, 7, 11], dtype=int32)>, <tf.Variable 'Variable:0' shape=(2,) dtype=complex128, numpy=array([12.3-4.85j, 7.5-6.23j])>]
2阶张量
# 2阶张量mymat = tf.Variable([[7],[11]], tf.int16)myxor = tf.Variable([[False, True],[True, False]], tf.bool)linear_squares = tf.Variable([[4], [9], [16], [25]], tf.int32) squarish_squares = tf.Variable([ [4, 9], [16, 25] ], tf.int32)rank_of_squares = tf.rank(squarish_squares)mymatC = tf.Variable([[7],[11]], tf.int32)[mymat, myxor, linear_squares, squarish_squares, rank_of_squares, mymatC]OUT:[<tf.Variable 'Variable_12:0' shape=(2, 1) dtype=int32_ref>, <tf.Variable 'Variable_13:0' shape=(2, 2) dtype=bool_ref>, <tf.Variable 'Variable_14:0' shape=(4, 1) dtype=int32_ref>, <tf.Variable 'Variable_15:0' shape=(2, 2) dtype=int32_ref>, <tf.Tensor 'Rank:0' shape=() dtype=int32>, <tf.Variable 'Variable_16:0' shape=(2, 1) dtype=int32_ref>]
上面总结了创建变量的方法,在数据中,数据就是一个多维度的张量
如何这点张量的阶数,并将维数降低
# 4维0矩阵my_image = tf.zeros([10, 299, 299, 3]) # batch x height x width x colormy_imageOUT:<tf.Tensor 'zeros:0' shape=(10, 299, 299, 3) dtype=float32>
将维数降低 tf.rank 0
rank_of_my_image = tf.rank(my_image)rank_of_my_imageOUT:<tf.Tensor 'Rank:0' shape=() dtype=int32>
有些方法2.0不支持
tf的接口,torch ,numpy接口差不多
import tensorflow as tfa = tf.constant([1.0, 2.0], name="a")b = tf.constant([2.0, 3.0], name="b")result = a + bprint(result)tf.Tensor([3. 5.], shape=(2,), dtype=float32)x = tf.constant([[1.0, 2.0]]) #定义一个 2 阶张量等于[[1.0,2.0]]w = tf.constant([[3.0], [4.0]]) #定义一个 2 阶张量等于[[3.0],[4.0]]y = tf.matmul(x, w) #实现 xw 矩阵乘法print (y) #打印出结果tf.Tensor([[11.]], shape=(1, 1), dtype=float32)
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有