前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensor张量

tensor张量

作者头像
润森
发布2019-11-04 22:28:48
8940
发布2019-11-04 22:28:48
举报
文章被收录于专栏:毛利学Python毛利学Python

tensorflow核心和语言支持的API

流动过程

张量

在TensorFlow系统中,张量的维数来被描述为阶.但是张量的阶和矩阵的阶并不是同一个概念.张量的阶(有时是关于如顺序或度数或者是n维)是张量维数的一个数量描述.比如,下面的张量(使用Python中list定义的)就是2阶.

t=[[1,2,3],[4,5,6],[7,8,9]]

在tensorlflow中,有几类特别的张量

  • tf.constant //常量
  • tf.placeholder //占位符
  • tf.Variable //变量

变量:tf创建了可以修改了 多数用Variable创建张量

0阶张量

代码语言:javascript
复制
# 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阶张量

代码语言:javascript
复制
# 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阶张量

代码语言:javascript
复制
# 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>]

上面总结了创建变量的方法,在数据中,数据就是一个多维度的张量

如何这点张量的阶数,并将维数降低

代码语言:javascript
复制
# 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

代码语言:javascript
复制
rank_of_my_image = tf.rank(my_image)rank_of_my_imageOUT:<tf.Tensor 'Rank:0' shape=() dtype=int32>
tf.rank用法

tensor 计算

有些方法2.0不支持

tf的接口,torch ,numpy接口差不多

代码语言:javascript
复制
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)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-11-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小刘IT教程 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 张量
    • tf.rank用法
    • tensor 计算
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档