本学习笔记基于北京大学-tensorflow2.0教学视频所写,方便自己日后复习。
## 虚拟环境 创建虚拟环境:conda create -n 名称 python=3.7
进入虚拟环境:conda activate TF2.1
(我创建的环境在D:\anaconda\envs)
tf
损失函数:预测值和标准值的差
常用损失函数为均方误差:
学习率和梯度下降法
Tensor张量
张量:多维数组(列表) 阶:张量的维数
t=[[[:有几个括号就是几阶张量
创建张量
#创建张量
#tf.constant(张量内容,dtype=数据类型(可选))
a = tf.constant([[1, 5],[2, 4]], dtype=tf.int64)
print("a:", a)
print("a.dtype:", a.dtype)
print("a.shape:", a.shape)
运行结果:
a: tf.Tensor(
[[1 5]
[2 4]], shape=(2, 2), dtype=int64)
a.dtype: <dtype: 'int64'>
a.shape: (2, 2)
其中shape=(2,2) 逗号隔开了几个数字,就是几维张量,数字是几,则该部分有几个元素。
numpy转tensor
import tensorflow as tf
import numpy as np
a = np.arange(0, 5)
b = tf.convert_to_tensor(a, dtype=tf.int64)
print("a:", a)
print("b:", b)
运行结果:
a: [0 1 2 3 4]
b: tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int64)
tf.zeros tf.ones和tf.fill
import tensorflow as tf
a = tf.zeros([2, 3])
b = tf.ones(4)
c = tf.fill([2, 2], 9)
print("a:", a)
print("b:", b)
print("c:", c)
运行结果:
a: tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]], shape=(2, 3), dtype=float32)
b: tf.Tensor([1. 1. 1. 1.], shape=(4,), dtype=float32)
c: tf.Tensor(
[[9 9]
[9 9]], shape=(2, 2), dtype=int32)
生成随机数(标准差stddev很重要)
1.生成正态分布随机数
tf.random.normal(维度,mean=均值,stddev=标准差)
2.生成截断式正态分布随机数
tr.random.truncated_normal(维度,mean=均值,stddev=标准差)
3.生成均匀分布随机数
tf.random.uniform(维度,minval=最小值,maxval=最大值)
类型转换,最大值和最小值
tf.cast(张量名,dtype=数据类型)#转换数据类型
tf.reduce_max(张量名)#计算最大值
tf.reduce_min(张量名)#计算最小值
variable 可训练标记
张量运算
四则运算
平方、次方和开方
矩阵乘法
标签和特征配对
tf.data.Dataset.from_tensor_slices()
GradientTape()和gradient()
import tensorflow as tf
x = tf.constant(3.0)
with tf.GradientTape() as tape:
#x = tf.Variable(tf.constant(3.0))
tape.watch(x)
y = tf.pow(x, 2)
grad = tape.gradient(y, x)
print(grad)
运行结果:
tf.Tensor(6.0, shape=(), dtype=float32)
其中GradientTape默认只监控由tf.Variable创建的traiable=True属性
所以x = tf.Variable(tf.constant(3.0))等价于x = tf.constant(3.0);tape.watch(x)。
gradient是用来求出张量的梯度,具体为gradient(函数,对谁求导)
enumerate(列表名)
遍历列表,元组,字符串中的每个元素,结果输出为:索引 元素
seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print(i, element)
运行结果
0 one
1 two
2 three
one_hot(待转数据,depth=几分类)
独热码
import tensorflow as tf
classes = 3
labels = tf.constant([1, 0, 2]) # 输入的元素值最小为0,最大为2
output = tf.one_hot(labels, depth=classes)
print("result of labels1:", output)
print("\n")
运行结果
result of labels1: tf.Tensor(
[[0. 1. 0.]
[1. 0. 0.]
[0. 0. 1.]], shape=(3, 3), dtype=float32)
softmax
softmax函数,又称归一化指数函数。它是二分类函数sigmoid在多分类上的推广,目的是将多分类的结果以概率的形式展现出来。
很棒的总结:softmax就是模型已经有分类预测结果以后,将预测结果输入softmax函数,进行非负性和归一化处理,最后得到0-1之内的分类概率.
import tensorflow as tf
y = tf.constant([1.01, 2.01, -0.66])
y_pro = tf.nn.softmax(y)
print("After softmax, y_pro is:", y_pro) # y_pro 符合概率分布
print("The sum of y_pro:", tf.reduce_sum(y_pro)) # 通过softmax后,所有概率加起来和为1
运行结果
After softmax, y_pro is: tf.Tensor([0.25598174 0.69583046 0.0481878 ], shape=(3,), dtype=float32)
The sum of y_pro: tf.Tensor(1.0, shape=(), dtype=float32)
assign_sub
argmax
神经网络中Batch和Epoch之间的区别是什么?
解决卡Adding visible gpu devices: 0
tf.equal(tf1,tf2)
tf.equal(tf1,tf)
输出结果
tf.Tensor(
[ True True True True True ], shape=(5,), dtype=bool)
可以用利用tf.cast()将其转换为0 1形式
tf.cast(tf.equal(pred, y_test), dtype=tf.int32)
输出结果
tf.Tensor(
[ 1 1 1 1 1 ], shape=(5,), dtype=int32)