gpu_accelerate.py
import torch
import time
print(torch.__version__)
print(torch.cuda.is_available())
# print('hello, world.')
a = torch.randn(10000, 1000) #a矩阵的行和列
b = torch.randn(1000, 2000)
#使用cpu
t0 = time.time() #开始的进行时刻
c = torch.matmul(a, b) #cpu模式的矩阵乘法
t1 = time.time() #结束时刻
print(a.device, t1 - t0, c.norm(2))
#首次使用cuda,会需要初始化,耗时会长一些
device = torch.device('cuda')
a = a.to(device)
b = b.to(device)
t0 = time.time()
c = torch.matmul(a, b)
t2 = time.time()
print(a.device, t2 - t0, c.norm(2))
#第二次使用cuda就没有初始化操作了
t0 = time.time()
c = torch.matmul(a, b)
t2 = time.time()
print(a.device, t2 - t0, c.norm(2))
autograd_demo.py
import torch
from torch import autograd
x = torch.tensor(1.)
a = torch.tensor(1., requires_grad=True) #requires_grad=True 告诉pycharm我们需要对a、b、c求导
b = torch.tensor(2., requires_grad=True)
c = torch.tensor(3., requires_grad=True)
y = a**2 *x + b * x + c
print('before:', a.grad, b.grad, c.grad)
grads = autograd.grad(y, [a, b, c])
print('after:', grads[0], grads[1], grads[2])
nn.linear
nn.Conv2d 对图片的卷积层
nn.LSTM 对时序信号的LSTM层
nn.ReLU 激活函数
nn.Sigmoid Sigmoid函数
nn.Softmax
nn.CrossEntropyLoss
nn.MSE
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L0gPO3hz-1616773452812)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.29.23.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ucxmTiQ8-1616773452816)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.29.45.png)]
anaconda下载网址
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IpccEx5g-1616773452818)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.32.51.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hPV1VmvZ-1616773452820)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.33.04.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8YaoJzYh-1616773452821)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.33.43.png)]
CUDA下载网址
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qHxd2Nne-1616773452823)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.36.07.png)]
配置CUDA环境变量
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ye1X3PCJ-1616773452824)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.37.19.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hw8NIjjl-1616773452824)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.37.47.png)]
终端查看nvcc版本,安装成功!!!
Pytorch下载网址
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PjZrRgzB-1616773452825)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.40.58.png)]
复制安装pytorch的指令(pip或者conda)
用管理员权限打开终端,复制指令,安装Pytorch
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cAAFjYQd-1616773452826)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.42.58.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dDAzL6uF-1616773452827)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.44.01.png)]
PyCharm下载地址
File–>New Project–>输入工程名–>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FJRkomsw-1616773452828)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.47.13.png)]
设置conda下的python编译器
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fAqMqzfJ-1616773452829)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.48.07.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-togSagef-1616773452831)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.49.02.png)]
勾选Make available to all projects
测试程序main.py
import torch
print(torch.__version__)
print('gpu:', torch.cuda.is_available())
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HylTFoUt-1616773452833)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.51.19.png)]
Gradient Descent(梯度下降法)—DeepLearning的核心
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ElFT4pdz-1616773452834)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午8.54.31.png)]
learningrate(学习率)
各种不同的求解器:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Qj8xzzlD-1616773452835)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午9.07.32.png)]
均方误差(mean-square error, MSE)损失函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UdhZaSWu-1616773452836)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午9.10.06.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fjq0DdDv-1616773452838)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午9.12.22.png)]
凸优化
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eE2c2Z19-1616773452839)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午9.15.17.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pdkg61En-1616773452839)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午9.16.08.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kwo9ECA0-1616773452840)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午9.46.01.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dVCWuciP-1616773452841)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午9.54.08.png)]
迭代优化num_iterations次后得到的b和m
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EnDsK395-1616773452842)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午9.55.30.png)]
gd.py
import numpy as np
#损失函数
def compute_error_for_line_given_points(b, w, points): #points就是一系列x和y的值
totalError = 0
for i in range(0, len(points)):
x = points[i, 0] #取第i个点的x值
y = points[i, 1] #取第i个点的y值
totalError += (y - (w * x + b)) ** 2 #经过for循环求得总的误差
return totalError / float(len(points)) #对总的误差求平均值
#求每一次下降的最新梯度
def step_gradient(b_current, w_current, points, learningRate):
b_gradient = 0
w_gradient = 0
N = float(len(points))
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
b_gradient += -(2/N) * (y - (w_current * x) + b_current)
w_gradient += -(2/N) * x * (y - (w_current * x) + b_current)
new_b = b_current - (learningRate * b_gradient)
new_w = w_current - (learningRate * w_gradient)
return [new_b, new_w]
#通过step_gradient函数求出最终的b和w
def gradient_descent_runner(points, starting_b, starting_w, learning_rate, num_iteration):
b = starting_b
w = starting_w
for i in range(num_iteration):
b, w = step_gradient(b, w, np.array(points), learning_rate)
return [b, w]
#run函数
def run():
#获取数据
points = np.genfromtxt("data.csv", delimiter=",")
#设置参数
learning_rate = 0.0001
initial_b = 0 #initial y-intercept guess
initial_w = 0 #initial slope guess
num_iteration = 1000
print("Strating gradient descent at b = {0}, m = {1}, error = {2}"
.format(initial_b, initial_w,
compute_error_for_line_given_points(initial_b, initial_w, points)))
print("Running...")
[b, w] = gradient_descent_runner(points, initial_b, initial_w, learning_rate, num_iteration)
#每一次迭代的参数值和损失函数值
print("After {0} iterations b = {1}, m = {2}, error = {3}".
format(num_iteration, b, w, compute_error_for_line_given_points(b, w, points)))
#调用run函数
if __name__ == '__main__':
run()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1OGTACTs-1616773452843)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午10.04.53.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-urrM6RJr-1616773452844)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午10.10.18.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aJ4HTHoo-1616773452845)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午10.13.26.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ie3VNqIl-1616773452845)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午10.14.31.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TrmiK8Zi-1616773452846)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午10.16.10.png)]
ReLU非线性表达能力
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GMMAi1HH-1616773452847)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午10.18.38.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2LpVytGu-1616773452848)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午10.19.57.png)]
argmax(pred)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bHi8RHFg-1616773452849)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午10.24.30.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LtBX2GmA-1616773452850)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午10.37.58.png)]
三层先行层的嵌套、并且在线性层的末尾添加一个非线性的激活函数来增加非线性的表达能力
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-R74NwBXj-1616773452850)(/Users/jerry/Library/Application Support/typora-user-images/截屏2021-03-26 下午10.38.39.png)]