TensorFlow2总结

Tensor数据类型

  • list: [1,1.2,'hello'] ,内存占用大,处理速度慢
  • np.array,数据同类型,但不支持GPU和自动求导
  • tf.Tensor,为了弥补numpy的缺点,为深度学习而生,支持GPU与自动求导
  • tensor:
    • scalar:标量,1.1,dim = 0
    • vector:向量,[1.1],[1.1,2.2,...],dim = 1
    • matrix: 矩阵,[[1.1,2.2],[3.3,4.4]],dim = 2
    • tensor:rank>2 ,dim > 2
  • 数据类型:
    • Int, float, double
    • bool
    • string
  • 定义tensor
tf.constant(1)  # 定义int,普通的tensor
tf.constant(1.)  # 定义float
tf.constant([True, False])  # 定义bool
tf.constant('hello nick')   # 定义string

属性

with tf.device('cpu'):
  a = tf.constant([1])   # 在cpu创建
with tf.device('gpu'):
    b = tf.constant([1]) # 在gpu创建 
  
a.device # 设备属性
a.gpu()  # cpu转gpu
b.cpu()  # gpu转cpu
a.numpy()# 获取numpy数据类型
a.shape  # 获取a的属性
a.ndim   # 获取维度
tf.rank(a)  # 获取维度
a.name  # 1.+历史遗留问题

数据类型判断

instance(a,tf.Tensor) # 判断是否为tensor (不推荐)
tf.is_tensor(a)  # 判断是否为tensor   (推荐)
a.dtype,b.dtype,c.dtype  # 判断数据类型

数据类型转换

a = np.arange(5)
aa = tf.convert_to_tensor(a,dtype=tf.int32) # numpy转tensor

tf.cast(aa,dtype=tf.float32)  # tensor之间数据类型转换 tf.int32转tf.float32

b = tf.constant([0,1])
tf.cast(b,dtype=tf.bool) # int转bool

# tf.Variable
a = tf.range(5)    # 生成一个tensor
b = tf.Variable(a, name='input_data') # tensor转为Variable后具有求导的特性,即自动记录a的梯度相关信息
b.name      # input_data:0
b.trainable # True  # 可训练

isinstance(b,tf.Tensor)    # False
isinstance(b,tf.Variable)  # True
tf.is_tensor(b)  # True    # 推荐使用

 tensor转numpy

a= tf.range(5)
a.numpy()

# a必须是scalar
a = tf.ones([])
a.numpy()  
int(a)
float(a)

创建Tensor

from numpy, list
* zeros, ones, fill
* random  # if big dimension, random initial
* constant
* Application

numpy, list

numpy

import numpy as np
import tensorflow as tf

tf.convert_to_tensor(np.ones([2, 3]))
tf.convert_to_tensor(np.zeros([2, 3]))

list

tf.convert_to_tensor([1, 2])
tf.convert_to_tensor([1, 2.])
tf.convert_to_tensor([[1], [2.]])

zeros, ones, fill

zeros

tf.zeros([])
tf.zeros([1])
tf.zeros([2, 2])
tf.zeros([2, 3, 3])
a = tf.constant([0])
tf.zeros_like(a)  # 等同于tf.zeros(a.shape)

ones

 

tf.ones(1)
tf.ones([])
tf.ones([2])
tf.ones([2, 3])
a = tf.constant([0])
tf.ones_like(a)  # # 等同于tf.ones(a.shape)

fill

 

tf.fill([2, 2], 0)
tf.fill([2, 2], 0)
tf.fill([2, 2], 1)
tf.fill([2, 2], 9)

random

 

# 正态分布,均值为1,方差为1
tf.random.normal([2, 2], mean=1, stddev=1)
tf.random.normal([2, 2])
# 截断的正态分布,
tf.random.truncated_normal([2, 2], mean=0, stddev=1)

如下图所示为截断正态分布,截掉红色部分,对新的正态分布重新采样。因为sigmoid的和新的正态分布不冲突的地方的区域,对于sigmoid函数来说是近似于平滑的直线,梯度为0,因此会有梯度消失。

 

# 均匀分布
tf.random.uniform([2, 2], minval=0, maxval=1)
tf.random.uniform([2, 2], minval=0, maxval=100, dtype=tf.int32)

打乱idx后,a和b的索引不变

idx = tf.range(10)
idx = tf.random.shuffle(idx)
idx
a = tf.random.normal([10, 784])
b = tf.random.uniform([10], maxval=10, dtype=tf.int32)
b
a = tf.gather(a, idx)
b = tf.gather(b, idx)
b

constant

tf.constant(1)
tf.constant([1])
tf.constant([1, 2.])
tf.constant([[1, 2], [3., 2]])

loss计算

无bias的loss

out = tf.random.uniform([4, 10])
out
y = tf.range(4)
y = tf.one_hot(y, depth=10)
y
loss = tf.keras.losses.mse(y, out)
loss
loss = tf.reduce_mean(loss)
loss

 

你可能感兴趣的:(TensorFlow2总结)