一、tf-gpu的安装
- cuda--让我们的电脑可以调用显卡进行运算。
- cuda--下载和安装
- cuda---程序员可以使用这个编译和控制cpu
- cudnn--这几个版本必须是对应的。
- 具体的安装过程百度就行了
- https://tensorflow.google.cn/install/source
-
上面是官方提供的上面三个的版本对应信息
版本对应信息.png
二、tensorflow实现线性回归算法
- 线性回归的原理是最小二乘法
*tensorflow是神经网络--无比强大 - 普通的应用--并不是很神秘
代码:
导包、声明数据
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
X = np.linspace(2,14,40)
w = np.random.randint(1,10,size = 1)[0]
b = np.random.randint(-5,5,size = 1)[0]
y = X*w + b + np.random.randn(40)*2
plt.scatter(X,y)
<matplotlib.collections.PathCollection at 0x248f7b3a388>
output_2_1.png
epoch = 601 #循环次数
learning_rate = 0.01#学习率,步幅
display_num = 10#每运算10次,打印输出一下
定义模型变量:w、b
定义了模型和损失
定义优化算法
# 初始化,给了一个,随机给的,占位符,不是结果!
W = tf.Variable(tf.random.normal(shape = [1]),name = 'weights')#权重、斜率
B = tf.Variable(tf.random.normal(shape = [1]),name = 'bias')# 截距
def linear_model(X):#线性模型 f(X) = X*W + B
# return tf.multiply(X,W) + B
return X*W + B
def sqare_loss(y_pred,y_true):#最小二乘法
# return tf.reduce_mean(tf.pow(tf.subtract(y_true,y_pred),2))
return tf.reduce_mean((y_pred - y_true)**2)
# 优化算法:随机梯度下降
sgd = tf.optimizers.SGD(learning_rate)#随机梯度下降
sgd
<tensorflow.python.keras.optimizer_v2.gradient_descent.SGD at 0x248f8ea7f88>
定义优化过程,随机梯度下降
def run_optimization():
with tf.GradientTape() as g:# 自动求导,自动计算
y_pred = linear_model(X)# 使用模型进行预测
loss = sqare_loss(y_pred,y)# 计算最小二乘法的损失
# 计算梯度,导数,可以根据梯度,进行随机梯度下降
gradients = g.gradient(loss,[W,B])# 求loss对W和B的偏导数
# 进行梯度下降,更新了W和B
sgd.apply_gradients(zip(gradients,[W,B]))
return loss.numpy(),W.numpy(),B.numpy()
for循环进行计算
%%time
for i in range(1,51):
loss_,W_,B_ = run_optimization()
print(W_,B_)
[7.371913] [0.9411092]
Wall time: 128 ms
print('标准答案:',w,b)
标准答案: 7 4
plt.scatter(X,y)
x = np.linspace(0,14,100)
plt.plot(x,W_[0]*x + B_[0],color = 'green')
[<matplotlib.lines.Line2D at 0x248f5a0f188>]
output_12_1.png
![![output_2_1.png](https://img.haomeiwen.com/i4956968/111babe18d3ea176.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
](https://img.haomeiwen.com/i4956968/f9de53e19dc8837a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
from sklearn.linear_model import LinearRegression
linear = LinearRegression()
linear.fit(X.reshape(-1,1),y)
display(linear.coef_,linear.intercept_)
array([7.10730639])
3.4698396103817117
from sklearn.linear_model import SGDRegressor
%%time
sgdRegressor = SGDRegressor()
sgdRegressor.fit(X.reshape(-1,1),y)
display(sgdRegressor.coef_,sgdRegressor.intercept_)
array([7.29481099])
array([1.80328889])
Wall time: 4.99 ms
三、GradientTape--自动求导数
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
import numpy as np
常量求导(g.gradient只能调用一次)
x = tf.constant(3.0)
with tf.GradientTape() as g:# 作用域
g.watch(x)#watch 观察,查看
y = x**2
t = g.gradient(y,x) # 调用gradient()作用域结束了
t2 = g.gradient(y,x)# 第二次调用
print(t2)
display(t)
-------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-26-28dfc1262ee8> in <module>
4 y = x**2
5 t = g.gradient(y,x) # 调用gradient()作用域结束了
----> 6 t2 = g.gradient(y,x)# 第二次调用
7 print(t2)
8 display(t)
d:\python3.7.4\lib\site-packages\tensorflow_core\python\eager\backprop.py in gradient(self, target, sources, output_gradients, unconnected_gradients)
978 """
979 if self._tape is None:
--> 980 raise RuntimeError("GradientTape.gradient can only be called once on "
981 "non-persistent tapes.")
982 if self._recording:
RuntimeError: GradientTape.gradient can only be called once on non-persistent tapes.
GradientTape多次连续求导
x = tf.constant(3.0)
with tf.GradientTape(persistent=True) as g:
g.watch(x)# watch定义求导的变量,默认情况下,Tape观察变量
y = x**2
z = y**2
t1 = g.gradient(y,x)# x**2 -------->2x
t2 = g.gradient(z,x)# x**4 -------->4x**3
display(t1,t2)
# del 内存中的对象删除
del g
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>
<tf.Tensor: shape=(), dtype=float32, numpy=108.0>
GradientTape自动对变量求导
x = tf.Variable(3.0)# 变量,可变量,方程的一个元
f = lambda x:x**3 + 2*x + 5 # f(x) = wx + b
with tf.GradientTape() as g:#变量不需要watch,自动监控
y = f(x)
g.gradient(y,x)# 3*x**2 + 2 = 3*3**2 + 2 = 29
<tf.Tensor: shape=(), dtype=float32, numpy=29.0>
GradientTape给了两个变量
x = tf.Variable([1.0,3.0])
y = tf.Variable([2.0,7.0])
f = lambda x,y:x**2 + y**3 + 3
with tf.GradientTape() as g:
z = f(x,y)
gradients = g.gradient(z,[x,y])
gradients
[<tf.Tensor: shape=(2,), dtype=float32, numpy=array([2., 6.], dtype=float32)>,
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([ 12., 147.], dtype=float32)>]
四、逻辑斯蒂回归
import warnings
warnings.filterwarnings('ignore')
import numpy as np
from tensorflow.keras.datasets import mnist
import tensorflow as tf
加载数据
(X_train,y_train),(X_test,y_test) = mnist.load_data()
# 独热编码:概率形式表示数据
# 分类大部分都是概率问题
# 目标值变换成概率
y_train = tf.one_hot(y_train,depth=10)
y_test = tf.one_hot(y_test,depth=10)
# 归一化
X_train = X_train.reshape(60000,784).astype(np.float32)/255.0
X_test = X_test.reshape(10000,784).astype(np.float32)/255.0
data_train = tf.data.Dataset.from_tensor_slices((X_train,y_train))
data_train = data_train.repeat(100).shuffle(2000).batch(256)
data_test = tf.data.Dataset.from_tensor_slices((X_test,y_test))
data_test = data_test.repeat(5).shuffle(3000).batch(1000)
声明模型的斜率和截距
声明模型和损失
声明优化算法SGD
# X -----w + b -----> y
w = tf.Variable(tf.random.normal(shape = [784,10],stddev = 0.1),name = 'weights')
b = tf.Variable(tf.random.normal(shape = [10],stddev = 0.1),name = 'bias')
# 逻辑斯蒂回归模型
def logistic_model(X):
y_pred = tf.nn.softmax(tf.matmul(X,w) + b)# 软最大,转化成概率
return y_pred
# 定义损失函数,使用交叉熵
# tf.reduce_mean:平均交叉熵
# 线性回归时:平均二乘法
def cross_entropy(y_pred,y_true):#y_true和y_pred都是多个样本的
y_pred = tf.clip_by_value(y_pred,clip_value_min=1e-8,clip_value_max=1.0)
loss = tf.reduce_mean(tf.reduce_sum(tf.multiply(y_true,tf.math.log(1/y_pred)),axis = -1))
return loss
# 优化方式:随机梯度下降
sgd = tf.optimizers.SGD(learning_rate=0.01)
定义优化方法
# 定义优化过程和线性回归,一模一样!!!
def run_optimizer(X_train,y_train):
with tf.GradientTape() as g:
y_pred = logistic_model(X_train)
loss = cross_entropy(y_pred,y_train)
gradients = g.gradient(loss,[w,b])
sgd.apply_gradients(zip(gradients,[w,b]))
运行优化代码,计算准确率
for i,(X_train,y_train) in enumerate(data_train.take(1000),1):
run_optimizer(X_train,y_train)
if i %50 == 0:#计算准确率,测试数据data_test测试数据
for X_test,y_test in data_test.take(1):#每次和每次取出来的值,不同的
y_ = logistic_model(X_test).numpy().argmax(axis = 1)
y_true = y_test.numpy().argmax(axis = 1)
accuracy = (y_ == y_true).mean()
print('运行次数:%d。准确率是:%0.4f'%(i,accuracy))
运行次数:50。准确率是:0.7710
运行次数:100。准确率是:0.8090
运行次数:150。准确率是:0.8140
运行次数:200。准确率是:0.8240
运行次数:250。准确率是:0.8060
运行次数:300。准确率是:0.8300
运行次数:350。准确率是:0.8210
运行次数:400。准确率是:0.8230
运行次数:450。准确率是:0.8230
运行次数:500。准确率是:0.8100
运行次数:550。准确率是:0.8380
运行次数:600。准确率是:0.8390
运行次数:650。准确率是:0.8210
运行次数:700。准确率是:0.8420
运行次数:750。准确率是:0.8420
运行次数:800。准确率是:0.8250
运行次数:850。准确率是:0.8300
运行次数:900。准确率是:0.8470
运行次数:950。准确率是:0.8360
运行次数:1000。准确率是:0.8560
网友评论