Tensorflow2.0学习(8):基础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__)
print(sys.version_info)
for module in mpl, np ,pd, sklearn, tf, keras:
    print(module.__name__, module.__version__)
2.0.0
sys.version_info(major=3, minor=7, micro=6, releaselevel='final', serial=0)
matplotlib 3.1.3
numpy 1.18.1
pandas 1.0.0
sklearn 0.22.1
tensorflow 2.0.0
tensorflow_core.keras 2.2.4-tf
  • 常量及其基本操作
# 常量
t = tf.constant([[1, 2, 3], [4, 5, 6]])
print(t)
print(t[:,1:])
print(t[:,1])

t_1 = tf.constant([[1., 2., 3.], [4., 5., 6.]])
print(t_1)
print(t_1[:,1:])
print(t_1[:,1])
tf.Tensor(
[[1 2 3]
 [4 5 6]], shape=(2, 3), dtype=int32)
tf.Tensor(
[[2 3]
 [5 6]], shape=(2, 2), dtype=int32)
tf.Tensor([2 5], shape=(2,), dtype=int32)
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)
# 常量的操作
# 加
print(t+10)
# 平方
print(tf.square(t))
# 矩阵乘以自己的转置(得用@)
# python中矩阵的叉乘:用@或者tf.matmul(A,C)或者np.dot(A,C)
# python中矩阵的点乘:用*或者tf.multiply(A,C)
print(t @ tf.transpose(t))

tf.Tensor(
[[11 12 13]
 [14 15 16]], shape=(2, 3), dtype=int32)
tf.Tensor(
[[ 1  4  9]
 [16 25 36]], shape=(2, 3), dtype=int32)
tf.Tensor(
[[14 32]
 [32 77]], shape=(2, 2), dtype=int32)
  • tensorflow对象与numpy对象的转换
# tensorflow对象和numpy的转化
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=int32)
# tensor为0维,也就是一个数,称为 Scalars
t = tf.constant(2.63554)
print(t.numpy())
print(t.shape)
2.63554
()
  • 字符串及其编码
# 字符串 string

t = tf.constant("cafe")
print(t)
print(tf.strings.length(t))
# 获得utf-8编码长度
print(tf.strings.length(t, unit="UTF8_CHAR"))
# 从unicode转化为UTF-8(UTF-8是unicode编码的一种实现)
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)
# RaggerTensor:数组是不规则的意思:第一行有4个,第二行有6个,第三行有2个
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:不规则张量
# ragger tensor
r = tf.ragged.constant([[11,22],[21, 22 ,33],[1],[4,5,6,9,3]])
# 索引
print(r)
print(r[1])
print(r[1:3])
<tf.RaggedTensor [[11, 22], [21, 22, 33], [1], [4, 5, 6, 9, 3]]>
tf.Tensor([21 22 33], shape=(3,), dtype=int32)
<tf.RaggedTensor [[21, 22, 33], [1]]>
# 拼接操作

r2 = tf.ragged.constant([[51,52],[],[77]])
# 行拼接
print(tf.concat([r, r2], axis = 0))
# # 列拼接会出错,因为行数不同
# print(tf.concat([r, r2], axis = 1))
# 当行数相同时,将列重新拼接
r3 = tf.ragged.constant([[1,2],[2, 22 ,33],[],[4,9,3]])
print(tf.concat([r, r3], axis = 1))
<tf.RaggedTensor [[11, 22], [21, 22, 33], [1], [4, 5, 6, 9, 3], [51, 52], [], [77]]>
<tf.RaggedTensor [[11, 22, 1, 2], [21, 22, 33, 2, 22, 33], [1], [4, 5, 6, 9, 3, 4, 9, 3]]>
# 将ragged tensor转变为普通的tensor
# 所有0值都在正常值后面
print(r.to_tensor())
tf.Tensor(
[[11 22  0  0  0]
 [21 22 33  0  0]
 [ 1  0  0  0  0]
 [ 4  5  6  9  3]], shape=(4, 5), dtype=int32)
  • sparse tensor:稀疏张量
# sparse tensor
# indices:values的位置
# values:所填值的大小
# dense_shape:矩阵的真实大小
s = tf.SparseTensor(indices = [[0,1], [1, 0], [2, 3]],
                    values = [1., 2., 3.],
                     dense_shape = [3, 4])
print(s)
# 转化成普通的tensor
print(tf.sparse.to_dense(s))
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)
# sparse tensor支持的操作:
s2 = s * 2.0
print(s2)
try:
    s3 = s + 1 # 加法不行
except TypeError as ex:
    print(ex)
s4 = tf.constant([[10., 20.],
                 [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 常见的错误,顺序不对,转化成普通tensor会报错
s5 = tf.SparseTensor(indices = [[0, 2], [0, 1], [2, 3]],
                    values = [1., 2., 3.],
                     dense_shape = [3, 4])
print(s5)
# 将顺序reorder即可
s6 = tf.sparse.reorder(s5)
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)
  • 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.]]
# 变量重新赋值(常量不可)
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.]]
# 赋值是不能用等号
try:
    v[1]=[7., 8., 9.]
except TypeError as ex:
    print(ex)
'ResourceVariable' object does not support item assignment
发布了35 篇原创文章 · 获赞 3 · 访问量 2505

猜你喜欢

转载自blog.csdn.net/Smile_mingm/article/details/104512309