tensorflow2.0 基础API,张量(常量、变量)的定义、运算和操作,字符串矩阵、不规则张量、稀疏张量的定义和运算。
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
2.0.0
#常量
t = tf.constant([[1.,2.,3.],[4.,5.,6.]]) # 2维矩阵
#index
print(t)
print(t[:,1:])
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)
tf.Tensor([2. 5.], shape=(2,), dtype=float32)
# 常量运算
print(t+10)
print(tf.square(t))
print(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 conversion
print(t.numpy()) # tensor 转 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)
# Scalars 0维
t = tf.constant(2.718)
print(t.numpy()) # 显示数值
print(t.shape) # (): 0维矩阵
2.718
()
# strings
t = tf.constant("cafe")
print(t)
print(tf.strings.length(t))
print(tf.strings.length(t,unit="UTF8_CHAR")) # UTF8长度
print(tf.strings.unicode_decode(t,"UTF8")) #转成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)
# strings array
t = tf.constant(["cafe","coffee","咖啡"])
print(tf.strings.length(t))
print(tf.strings.length(t,unit="UTF8_CHAR"))
print(tf.strings.unicode_decode(t,"UTF8")) # RaggedTensor: 不完整的矩阵,不等长的tensor
tf.Tensor([4 6 6], shape=(3,), dtype=int32)
tf.Tensor([4 6 2], shape=(3,), dtype=int32)
<tf.RaggedTensor [[99, 97, 102, 101], [99, 111, 102, 102, 101, 101], [21654, 21857]]>
# Ragged Tensor
r = tf.ragged.constant([[11,12],[21,22,23],[],[41]])
print(r)
print(r[1])
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]]>
# operations on ragged tensor
r2 = tf.ragged.constant([[51,52],[],[71]])
print(tf.concat([r,r2],axis=0)) # 拼接
r3 = tf.ragged.constant([[13,14],[15],[],[42,43]])
print(tf.concat([r,r3],axis=1))
print(r.to_tensor()) # 补零变为普通tensor
<tf.RaggedTensor [[11, 12], [21, 22, 23], [], [41], [51, 52], [], [71]]>
<tf.RaggedTensor [[11, 12, 13, 14], [21, 22, 23, 15], [], [41, 42, 43]]>
tf.Tensor(
[[11 12 0]
[21 22 23]
[ 0 0 0]
[41 0 0]], shape=(4, 3), dtype=int32)
# sparse tensor 稀疏张量
s = tf.SparseTensor(indices = [[0,1],[1,0],[2,3]],
values = [1.,2.,3.],
dense_shape=[3,4])
print(s)
print(tf.sparse.to_dense(s))# 转为普通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)
# operations on sparse tensor
s2 = s * 2.0
print(s2)
try:
s3 = s + 1
except TypeError as ex:
print(ex) # 不支持加法
s4 = tf.constant([[10.0,20.0],[30,40],
[50,60],[70,80]])
print(tf.sparse.sparse_dense_matmul(s,s4)) # 矩阵乘法
SparseTensor(indices=tf.Tensor(
[[0 1]
[1 0]
[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(
[[ 30. 40.]
[ 20. 40.]
[210. 240.]], shape=(3, 2), dtype=float32)
# 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) # 排序
print(tf.sparse.to_dense(s6)) #需要传入排序的tensor
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)
# Variables
# tensorflow 中常量不可变,不可被重新赋值
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(分派) value,重新赋值
v.assign(2 * v)
print(v.numpy())
v[0,1].assign(42) #给[0,1]重新赋值
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.]]
# tensorflow变量赋值只能用assign不能用‘=’
try:
v[1] = [7.,8,9]
except TypeError as ex:
print(ex)
'ResourceVariable' object does not support item assignment