tf.constant,tf.strings,RaggedTensor,SparseTensor,tf.Variable实例--tensorflow2.0学习笔记

导入所需库:

import tensorflow as tf
import numpy as np
t = tf.constant([[1.,2.,3.],[4.,5.,6.]])#二维向量
print(t)
print(t[:,1:])#打印第二列之后的
print(t[...,1])#打印第二列
print(t[...,0])#打印第1列
print(t[0])#取第一行
print(t[0:2])#取1到2行,左闭右开区间

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)

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

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

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

#op运算
print(t+10)#每个值加10
print(tf.square(t))#每个值得平方
print(tf.transpose(t))#t矩阵的转置
print(t@tf.transpose(t))#t乘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(
[[1. 4.]
[2. 5.]
[3. 6.]], shape=(3, 2), dtype=float32)

tf.Tensor(
[[14. 32.]
[32. 77.]], shape=(2, 2), dtype=float32)

#numpy ocnversion

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

[[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)

#scalar 0维
t = tf.constant(3.33)#0维数据
print(t)#打印tensor数据
print(t.numpy())#打印numpy数据
print(t.shape)#打印维度

tf.Tensor(3.33, shape=(), dtype=float32)

3.33

()

#strings
t=tf.constant('milk')#定义字符串
print(t)
print(tf.strings.length(t))#打印字符串长度
print(tf.strings.length(t,unit = 'UTF8_CHAR'))
print(tf.strings.unicode_decode(t,'UTF8'))#转换为UTF8类型

tf.Tensor(b’milk’, shape=(), dtype=string)

tf.Tensor(4, shape=(), dtype=int32)

tf.Tensor(4, shape=(), dtype=int32)

tf.Tensor([109 105 108 107], shape=(4,), dtype=int32)

#stirng array
t = tf.constant(['milk','cofe','牛奶'])#定义字符串数组
print(t)
print(tf.strings.length(t,unit = "UTF8_CHAR"))#打印字符串数字长度
print(tf.strings.unicode_decode(t,'UTF8'))#转换为UTF8类型

tf.Tensor([b’milk’ b’cofe’ b’\xe7\x89\x9b\xe5\xa5\xb6’], shape=(3,), dtype=string)

tf.Tensor([4 4 2], shape=(3,), dtype=int32)

<tf.RaggedTensor [[109, 105, 108, 107], [99, 111, 102, 101], [29275, 22902]]>

#Ragged tensor不规则维度
r= tf.ragged.constant([[1,1,2],[2,3,2,1],[1],[]])
print(r)
print(r[1])#打印第2行,tensor向量
print(r[0:3])#打印前3行,左闭右开区间,RaggedTensor向量

<tf.RaggedTensor [[1, 1, 2], [2, 3, 2, 1], [1], []]>

tf.Tensor([2 3 2 1], shape=(4,), dtype=int32)

<tf.RaggedTensor [[1, 1, 2], [2, 3, 2, 1], [1]]>

#op on ragged tensor运算
r2 = tf.ragged.constant([[1,2],[],[23,3]])
print(tf.concat([r,r2],axis = 0))#沿x轴拼接

<tf.RaggedTensor [[1, 1, 2], [2, 3, 2, 1], [1], [], [1, 2], [], [23, 3]]>

r3 = tf.ragged.constant([[2,3],[1],[3,31]])#r2,r3列数相同才可以沿y轴拼接
print(tf.concat([r3,r2],axis = 1))

<tf.RaggedTensor [[2, 3, 1, 2], [1], [3, 31, 23, 3]]>

print(r.to_tensor())#把raggedtensor变成tensor

tf.Tensor(
[[1 1 2 0]
[2 3 2 1]
[1 0 0 0]
[0 0 0 0]], shape=(4, 4), dtype=int32)

#sparese tensor
s = tf.SparseTensor(indices = [[0,2],[1,1],[2,3]],#3个值的位置索引
                    values = [1.,2.,3.],#3个值
                    dense_shape = [3,4])#密集矩阵的维度
print(s)
print(tf.sparse.to_dense(s))#打印密集矩阵

SparseTensor(indices=tf.Tensor(
[[0 2]
[1 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. 0. 1. 0.]
[0. 2. 0. 0.]
[0. 0. 0. 3.]], shape=(3, 4), dtype=float32)

#ops on sparse tensor
s2 = s*2.0
print(s2)
try:
    s3 = s+1#加法出错
except TypeError as ex:
    print(ex)
    
s4=tf.constant([[1.,2.],
                [2.,4.],
                [3.,4.],
                [.3,5.]])
print(tf.sparse.sparse_dense_matmul(s,s4))#[3,4]*[4,2]=[3,2]

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

unsupported operand type(s) for +: ‘SparseTensor’ and ‘int’

tf.Tensor(
[[ 3. 4. ]
[ 4. 8. ]
[ 0.90000004 15. ]], shape=(3, 2), dtype=float32)

#sparese tensor
s5 = tf.SparseTensor(indices = [[1,2],[1,1],[2,3]],#3个值的位置索引,打乱索引顺序,前面的索引大于后面的
                    values = [1.,2.,3.],#3个值
                    dense_shape = [3,4])#密集矩阵的维度
print(s)
s6 = tf.sparse.reorder(s5)#索引顺序恢复
print(tf.sparse.to_dense(s6))#打印密集矩阵

SparseTensor(indices=tf.Tensor(
[[0 2]
[1 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. 0. 0. 0.]
[0. 2. 1. 0.]
[0. 0. 0. 3.]], shape=(3, 4), dtype=float32)

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

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

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

[[1. 2.]
[3. 4.]
[5. 6.]]

#assign value
v.assign(v*2) #整个v乘以2
print(v.numpy)
print(v[0,1].assign(34).numpy) #[0,1]位置重新赋值
print(v[1].assign([2.,4.]).numpy) #第2行重新赋值,不支持等号赋值

<bound method BaseResourceVariable.numpy of <tf.Variable ‘Variable:0’ shape=(3, 2) dtype=float32, numpy=
array([[ 2., 4.],
[ 6., 8.],
[10., 12.]], dtype=float32)>>

<bound method BaseResourceVariable.numpy of <tf.Variable ‘UnreadVariable’ shape=(3, 2) dtype=float32, numpy=
array([[ 2., 34.],
[ 6., 8.],
[10., 12.]], dtype=float32)>>

<bound method BaseResourceVariable.numpy of <tf.Variable ‘UnreadVariable’ shape=(3, 2) dtype=float32, numpy=
array([[ 2., 34.],
[ 2., 4.],
[10., 12.]], dtype=float32)>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值