tensorflow创建常量与变量及其使用

1.创建常量

1.用tf.constant创建一般常量

1.创建矩阵型tensor.

创建

t = tf.constant([[1., 2., 3.], [4., 5., 6.]])

# index
print(t)
print(t[:, 1:])
print(t[..., 1])#这里的...和:作用是一样的

tf.Tensor(
[[1. 2. 3.]
[4. 5. 6.]], shape=(2, 3), dtype=float32)
tf.Tensor(
[[2. 3.]
[5. 6.]], shape=(2, 2), dtype=float32)
tf.Tensor([2. 5.], shape=(2,), dtype=float32)

使用

# ops
print(t+10)
print(tf.square(t))
print(tf.matmul(t,tf.transpose(t)))#就是(t @ tf.transpose(t)),矩阵乘法

tf.Tensor(
[[11. 12. 13.]
[14. 15. 16.]], shape=(2, 3), dtype=float32)
tf.Tensor(
[[ 1. 4. 9.]
[16. 25. 36.]], shape=(2, 3), dtype=float32)
tf.Tensor(
[[14. 32.]
[32. 77.]], shape=(2, 2), dtype=float32)

numpy与tensor之间可以相互转换

# numpy conversion
print(t.numpy())
print(np.square(t))
np_t = np.array([[1., 2., 3.], [4., 5., 6.]])
print(tf.constant(np_t))

[[1. 2. 3.]
[4. 5. 6.]]
[[ 1. 4. 9.]
[16. 25. 36.]]
tf.Tensor(
[[1. 2. 3.]
[4. 5. 6.]], shape=(2, 3), dtype=float64)

2.创建scalar(标量)

当tf.cnstant()传入的是一个值时,创建的就是标量

# Scalars
t = tf.constant(2.718)
print(t.numpy())
print(t.shape)

2.718
()

3.创建字符和字符串矩阵

创建字符

# strings
t = tf.constant("cafe")
print(t)
print(tf.strings.length(t))
print(tf.strings.length(t, unit="UTF8_CHAR"))
print(tf.strings.unicode_decode(t, "UTF8"))

tf.Tensor(b’cafe’, shape=(), dtype=string)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor([ 99 97 102 101], shape=(4,), dtype=int32)

创建字符矩阵

# string array
t = tf.constant(["cafe", "coffee", "咖啡"])
print(tf.strings.length(t, unit="UTF8_CHAR"))
r = tf.strings.unicode_decode(t, "UTF8")
print(r)

tf.Tensor([4 6 2], shape=(3,), dtype=int32)
<tf.RaggedTensor [[99, 97, 102, 101], [99, 111, 102, 102, 101, 101], [21654, 21857]]>

2.创建ragged_tensor

生活中常常遇到数据并不是标准的矩形的时候,即不规则形状,这时候就需要用ragged_tensor来储存,ragged:adj.参差不齐的
创建

# ragged tensor
#用来存储参差不齐的数据,tagged就是参差不齐的意思
r = tf.ragged.constant([[11, 12], [21, 22, 23], [], [41]])#这里要用tf.ragged.constant()构建一个ragged tensor数据,用tf.constant会报错。
# index op
print(r)
print(r[1])#取到第一行(从第0行开始)
print(r[1:2])#左闭右开,顾头不顾尾,实际上取到的还是第一行

<tf.RaggedTensor [[11, 12], [21, 22, 23], [], [41]]>
tf.Tensor([21 22 23], shape=(3,), dtype=int32)
<tf.RaggedTensor [[21, 22, 23]]>

# ops on ragged tensor
r2 = tf.ragged.constant([[51, 52], [], [71]])
print(tf.concat([r, r2], axis = 0)#这里用concat拼接ragged_tensor数据,注意拼接是第一行要形状相同。

<tf.RaggedTensor [[11, 12], [21, 22, 23], [], [41], [51, 52], [], [71]]>

print(r.to_tensor())#用to_tensor方法将ragged_tensor转化为tensor,空白位置用0补齐。注意所有0都在非零数的后面。

tf.Tensor(
[[11 12 0]
[21 22 23]
[ 0 0 0]
[41 0 0]], shape=(4, 3), dtype=int32)

3.创建sparse_tensor

ragged_tenosr只能创建0在非零数据后面的数据,比如[[0,2],[3,0]]这个数据就没办法用ragged_tensor表示,(s=tf.ragged.constant([[0,2],[3,0]])也是可以的,但是to_tensor中0会自动在后面补全,)
而当数据中0比较多的时候,sparse_tensor就不失为一种好的数据格式。
创建sparse_tensor

# sparse tensor
#可以储存0比较多,而且有0在非零数字前面的数据,sparse:稀疏的
s = tf.SparseTensor(indices = [[0, 1], [1, 0], [2, 3]],#这里的【0,1】指的是矩阵中(0,1)这个位置,写的时候必须按顺序,不能先写[0,2],再写[0,1].
                    values = [1., 2., 3.],#与上面对应,(0,1)位置是1.,(1,0)位置是2.
                    dense_shape = [3, 4])#设置最终sparse_tensor的形状。
print(s)
#这里生成的sparse_tensor就不能用ragged_tensor表示。
print(tf.sparse.to_dense(s))#用来将sparse_tensor转化为正常的密集矩阵

SparseTensor(indices=tf.Tensor(
[[0 1]
[1 0]
[2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
tf.Tensor(
[[0. 1. 0. 0.]
[2. 0. 0. 0.]
[0. 0. 0. 3.]], shape=(3, 4), dtype=float32)

s4 = tf.constant([[10., 20.],
                  [30., 40.],
                  [50., 60.],
                  [70., 80.]])
print(tf.sparse.sparse_dense_matmul(s, s4))#sparse_tensor和密集矩阵的乘积结果是密集矩阵

[[ 30. 40.]
[ 20. 40.]
[210. 240.]], shape=(3, 2), dtype=float32)

tf.sparse.reorder

# sparse tensor
s5 = tf.SparseTensor(indices = [[0, 2], [0, 1], [2, 3]],
                    values = [1., 2., 3.],
                    dense_shape = [3, 4])
print(s5)
s6 = tf.sparse.reorder(s5)#tf.sparse.reorder(s5)是用来将上面创建sparse_tensor时的错序重新排序,使下一步tf.sparse.to_dense()能正常进行。
print(tf.sparse.to_dense(s6))

SparseTensor(indices=tf.Tensor(
[[0 2]
[0 1]
[2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
tf.Tensor(
[[0. 2. 1. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 3.]], shape=(3, 4), dtype=float32)

2.创建变量

前面介绍了创建tenor常量的方法,下面介绍一下创建变量的方法,其实变量的用法与常量类似,也具有矩阵乘法,转置等性质,唯一多出来的可能就是变量的值可以变化,tf中用tf.variable()创建变量
创建

# Variables
v = tf.Variable([[1., 2., 3.], [4., 5., 6.]])
print(v)
print(v.value())
print(v.numpy())

<tf.Variable ‘Variable:0’ shape=(2, 3) dtype=float32, numpy=
array([[1., 2., 3.],
[4., 5., 6.]], dtype=float32)>
tf.Tensor(
[[1. 2. 3.]
[4. 5. 6.]], shape=(2, 3), dtype=float32)
[[1. 2. 3.]
[4. 5. 6.]]

用.assign改变变量的值(注意不能用“=”直接给变量赋值,会报错的)

# assign value
v.assign(2*v)
print(v.numpy())
v[0, 1].assign(42)
print(v.numpy())
v[1].assign([7., 8., 9.])
print(v.numpy())

[[ 2. 4. 6.]
[ 8. 10. 12.]]
[[ 2. 42. 6.]
[ 8. 10. 12.]]
[[ 2. 42. 6.]
[ 7. 8. 9.]]

3.文末总结

其实tensorflow中创建数据的方法与python语法以及numpy都有诸多相同之处,但也有区别,记忆是要注意区别记忆。
比如在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值