tensorflow(一)---张量

tensorflow----张量

一、张量
1.定义:

张量广义上表示任意形式的“数据0”.张量可以理解为0阶(rank)标量、1阶向量、二阶矩阵在高维空间的推广,张量的阶描述它表示数据的最大纬度。
张量和矢量有点类似,但是张量包含的信息比矢量的信息多,也可以理解为矢量的以一个推广。

2.张量的两个重要属性

(1)(相同的)数据类型
(2)矩阵的形状

3.tensorflow中常见的张量

(1)tf.constant 常量
(2)tf.placeholder 占位符
(3)tf.Variable 变量
解释:tf.constant就是里面的元素都是常数,已经确定了就无法改变。tf.placeholder就是只是定义了形状的躯壳,没有数据,需要后续填充数据。tf.Variable定义了形状,并且里面有值,后续值可以被覆盖。

注意:常量与占位符建立的张量,在一次迭代后就会将空间释放,而变量不会,变量是一种特殊的张量,他会一直放在内存中,直到运算完毕!

4.张量的建立

(1)零阶张量:

a = tf.Variable('aaa', tf.string)
b = tf.constant(1, tf.int32)
print(a)
print(b)

# 结果:
<tf.Variable 'Variable:0' shape=() dtype=string_ref>
Tensor("Const:0", shape=(), dtype=int32)

(2)一阶张量

a = tf.Variable(['aaa', 'a'], tf.string)
b = tf.constant([1, 2], tf.int32)
c = tf.placeholder(tf.float16, (3,))
print(a)
print(b)
print(c)

# 结果:
<tf.Variable 'Variable_1:0' shape=(2,) dtype=string_ref>
Tensor("Const_1:0", shape=(2,), dtype=int32)
Tensor("Placeholder:0", shape=(3,), dtype=float16)

(3)二阶张量

a = tf.Variable([['aaa'], ['a']], tf.string)
b = tf.constant([[1], [2]], tf.int32)
c = tf.placeholder(tf.float16, (3, 3))
print(a)
print(b)
print(c)

# 结果:
<tf.Variable 'Variable_2:0' shape=(2, 1) dtype=string_ref>
Tensor("Const_2:0", shape=(2, 1), dtype=int32)
Tensor("Placeholder_1:0", shape=(3, 3), dtype=float16)
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ElegantCodingWH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值