TensorFlow数据模型--张量

1. 张量(tensor)

定义张量:

全1:

a = tf.ones((2,2), dtype = tf.int16, name = 'a')
# 输出:
#  <tf.Tensor 'a:0' shape=(2, 2) dtype=int16>

除了维度,dtype, name 可选,空时,dtype 为tf.float32

常量:

a = tf.constant(1, shape = (2,3))
# 输出:
# <tf.Tensor 'Const_1:0' shape=(2, 3) dtype=int32>

全0:

a = tf.zeros((3,3))
# 输出:
# <tf.Tensor 'zeros_1:0' shape=(3, 3) dtype=float32>

定义变量:
tf.Variable:

a = tf.Variable(initial_value = 1, trainable = True)
# 输出:
# <tf.Variable 'Variable:0' shape=() dtype=int32_ref>

a = tf.ones((2,2))
# 输出:
# <tf.Tensor 'ones_4:0' shape=(2, 2) dtype=float32>

运算操作:

加法:

>>> result = tf.add(a,b,name='addTwo')
>>> print(result)
Tensor("addTwo:0", shape=(2,), dtype=float32)

注:shape(2,)是一个一维数组,长度为2。

张量,有三个属性,分别为:名称addTwo,维度shape,类型type。

张量本身并没有存储具体的数值,需要会话session来计算。

2. tensor支持的类型

tensor,支持14种不同类型,分别为

实数
(tf.float32, tf.float64),

整数
(tf.int8, tf.int16, tf.int32,tf.int64, tf.unit8),

布尔型
(tf.bool),

复数
(tf.complex64, tf.complex128).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

rosefunR

你的赞赏是我创作的动力!

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

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

打赏作者

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

抵扣说明:

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

余额充值