鱼C论坛

 找回密码
 立即注册
查看: 3354|回复: 1

怎样使用Python进行深度学习呢

[复制链接]
发表于 2022-1-23 12:12:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Python深度学习的源代码有吗哦?

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-7-7 15:35:28 | 显示全部楼层
  1. import xlrd
  2. import math
  3. import matplotlib.pyplot as plt
  4. from mpl_toolkits.mplot3d.axes3d import Axes3D
  5. import numpy as np
  6. import time

  7. def loadData(filename):#将数据集加载到data数组
  8.     workbook = xlrd.open_workbook(filename)
  9.     boyinfo = workbook.sheet_by_index(0)
  10.     col_num = boyinfo.ncols
  11.     row_num = boyinfo.nrows
  12.     col0 = boyinfo.col_values(0)[1:]
  13.     data = np.array(col0)
  14.     if col_num == 1:
  15.         return data
  16.     else:
  17.         for i in range(col_num-1):
  18.             coltemp = boyinfo.col_values(i+1)[1:]
  19.             data = np.c_[data, coltemp]
  20.     return data

  21. def plotData(X,y):
  22.     pos = np.where(y == 1)
  23.     neg = np.where(y == 0)
  24.     p1 = plt.plot(X[pos, 0], X[pos, 1], marker='s', markersize=5, color='red')[0]
  25.     p2 = plt.plot(X[neg, 0], X[neg, 1], marker='o', markersize=5, color='green')[0]

  26.     return p1, p2

  27. def nomalization(X):
  28.     Xmin = np.min(X , axis = 0)
  29.     Xmax = np.max(X , axis = 0)
  30.     Xmu = np.mean(X , axis = 0)
  31.     X_norm = (X - Xmu) / (Xmax - Xmin)
  32.     return X_norm

  33. # sigmoid: 定义一个 激活(激励)函数 sigmoid 函数 (activation function),输入参数是 wx, 返回的是 sigmoid 函数值
  34. def sigmoid(wx):
  35.     sigmoidV =  1.0/(1.0+np.exp(-wx))#请补全                             # 请补全  计算激活函数 sigmoid 函数 的函数值,计算公式为:1.0/(1.0+np.exp(-wx))
  36.     return sigmoidV

  37. # BGD 批量梯度下降法求最优参数
  38. # 定义一个BGD 函数,即:批量梯度下降法(Batch Gradient Descent,BGD),输入参数是 数据集 X 和 y,
  39. # 迭代次数 iter_num, 学习率 alpha,又写作 lr (learning rate), 它表示每次向着J最陡峭的方向迈步的大小, 返回的是 权重 w
  40. # 通过批量梯度下降法(Batch Gradient Descent,BGD),不断更新权重 W
  41. def BGD(X, y, iter_num, alpha):
  42.     trainMat = np.mat(X)                            # 通过使用 np.mat 函数,将数据集 X 转换成矩阵类型,并赋值给 trainMat
  43.     trainY = np.mat(y).T                            # 通过使用 np.mat 函数,将数据集 y 转换成矩阵类型,并且转置,然后赋值给 trainY
  44.     m, n = np.shape(X)                              # 通过使用 np.shape 函数,得到数据集 X 的形状大小,其中,m 为数据集 X 的行数,n 为数据集 X 的列数
  45.     w = np.ones((n,1))                              # 通过使用 np.ones 函数,创建元素全为 1 的矩阵,矩阵的大小为 n 行 1 列,并赋值给 w, 即:进行权重 w 的初始化,令其全为 1
  46.     for i in range(iter_num):                       # 通过 for 循环结构,开始进行迭代,其中,i 可取的数依次为:0,1 ,2,....,iter_num-1, 迭代次数总共有 iter_num 次
  47.         error =  sigmoid(trainMat*w)-trainY#请补全  # 计算迭代的误差 error:将预测得到的激活函数的数值 sigmoid(trainMat*w) 减去 实际的 trainY 数值
  48.         w = w - (1.0/m) * alpha * trainMat.T * error#请补全  # 更新权重 w , BGD 批量梯度下降法 的核心, w = w - (1.0/m)*alpha*trainMat.T*error
  49.     return w                                        # 返回 w

  50. # plot decision boundary:定义一个 plotDecisionBoundaryn 函数,输入参数是 训练集 trainX, 训练集 trainY, 直线斜率截距相关参数 w, 迭代次数 iter_num ,目的是为了画出决策的判断边界
  51. def plotDecisionBoundary(trainX, trainY, w, mylr, iter_num = 0):
  52.     # prepare data
  53.     xcord1 = [];ycord1 = [];xcord2 = [];ycord2 = [] # 准备数据,定义四个空的列表,并分别赋值给 xcord1、ycord1、xcord2、ycord2,进行初始化
  54.     m, n = np.shape(trainX)                         # 通过使用 np.shape 函数,得到训练集 trainX 的形状大小,其中,m 为训练集 trainX 的行数,n 为训练集 trainX 的列数
  55.     for i in range(m):                              # 通过使用 for 循环语句,遍历训练集 trainX 所有的行,其中,i 可以取得值分别是 0,1,2,...,m-1,总共是 m 行
  56.         if trainY[i] == 1:                          # 通过使用 if 条件判断语句,如果训练集 trainY(标志)中的元素为 1,那么将训练集 trainX中的 trainX[i,1] 和 trainX[i,2] 分别添加到 xcord1 和 ycord1 列表中
  57.             xcord1.append(trainX[i,1])              # 通过 append 的方法,将训练集 trainX中 的 trainX[i,1] 添加到 xcord1 列表中,保存的是 pos 的横坐标, 代表 positive 的数据
  58.             ycord1.append(trainX[i,2])              # 通过 append 的方法,将训练集 trainX中 的 trainX[i,2] 添加到 ycord1 列表中,保存的是 pos 的纵坐标, 代表 positive 的数据
  59.         else:                                       # 否则,如果训练集 trainY(标志)中的元素不为 1,那么将训练集 trainX中的 trainX[i,1] 和 trainX[i,2] 分别添加到 xcord2 和 ycord2 列表中
  60.             xcord2.append(trainX[i,1])              # 通过 append 的方法,将训练集 trainX中 的 trainX[i,1] 添加到 xcord2 列表中,保存的是 neg 的横坐标, 代表 negative 的数据
  61.             ycord2.append(trainX[i,2])              # 通过 append 的方法,将训练集 trainX中 的 trainX[i,2] 添加到 ycord2 列表中,保存的是 neg 的纵坐标, 代表 negative 的数据
  62.     x_min = min(trainX[:,1])                        # 通过使用 min 函数,计算出 trainX[:,1] ,即 trainX 第2列的最小值,并赋值给 x_min
  63.     y_min = min(trainX[:,2])                        # 通过使用 min 函数,计算出 trainX[:,2] ,即 trainX 第3列的最小值,并赋值给 y_min
  64.     x_max = max(trainX[:,1])                        # 通过使用 max 函数,计算出 trainX[:,1] ,即 trainX 第2列的最大值,并赋值给 x_max
  65.     y_max = max(trainX[:,2])                        # 通过使用 max 函数,计算出 trainX[:,2] ,即 trainX 第3列的最大值,并赋值给 y_max

  66.     # plot scatter  & legend
  67.     fig = plt.figure(1)                              # 通过使用 plt.figure 函数,开始创建一个图形窗口,并赋值给 fig
  68.     # 通过使用 plt.scatter 函数,绘制散点图,横坐标为 xcord1, 纵坐标为 ycord1,标记大小为30,颜色为红色,形状样式为 s (正方形),代表 square, 图例标签为 'I like you'
  69.     plt.scatter(xcord1, ycord1, s=30, c='red', marker='s', label='I like you')
  70.     # 请补全 通过使用 plt.scatter 函数,绘制散点图,横坐标为 xcord2, 纵坐标为 ycord2,标记大小为30,颜色为绿色,形状样式为 o (圆形),代表 circle, 图例标签为 'I don't like you'
  71.     plt.scatter(xcord2, ycord2, s=30, c='green', marker='o', label='I don\'t like you')#请补全

  72.     plt.legend(loc='upper right')                   # 设置图例的位置为右上角

  73.     # set axis and ticks
  74.     delta_x = x_max-x_min                           # 计算横坐标的极差为横坐标最大值与最小值的差,并赋值给 delta_x
  75.     delta_y = y_max-y_min                           # 计算纵坐标的极差为纵坐标最大值与最小值的差,并赋值给 delta_y
  76.     # 设置横坐标的刻度:从 x_min - delta_x / 10 到 x_max + delta_x / 10,使用 np.arange 函数创建数组,步长为 1,并赋值给 my_x_ticks
  77.     my_x_ticks = np.arange(x_min - delta_x / 10, x_max + delta_x / 10, 1)
  78.     # 设置纵坐标的刻度:从 y_min - delta_y / 10 到 y_max + delta_y / 10,使用 np.arange 函数创建数组,步长为 1,并赋值给 my_y_ticks
  79.     my_y_ticks = np.arange(y_min - delta_y / 10, y_max + delta_y / 10, 1)

  80.     plt.xticks(my_x_ticks)                          # 通过使用 plt.xticks 函数,设置作图的横坐标的刻度为 my_x_ticks
  81.     plt.yticks(my_y_ticks)                          # 通过使用 plt.yticks 函数,设置作图的纵坐标的刻度为 my_y_ticks
  82.     # 通过使用 plt.axis 函数,设置作图的横坐标和纵坐标的显示范围,分别是[x_min-delta_x/10, x_max+delta_x/10] 和 [y_min-delta_y/10, y_max+delta_y/10]
  83.     plt.axis([x_min-delta_x/10, x_max+delta_x/10, y_min-delta_y/10, y_max+delta_y/10])

  84.     # drwa a line:绘制一条直线,用于决策判断
  85.     x = np.arange(x_min-delta_x/10, x_max+delta_x/10, 0.01) # 通过使用 np.arange 函数创建数组, 从 x_min - delta_x / 10 到 x_max + delta_x / 10,步长为 0.01,并赋值给 x
  86.     y = y = (-w[0]-w[1]*x)/w[2] #请补全                                             # 通过公式计算得到直线的纵坐标: y = (-w[0]-w[1]*x)/w[2]
  87.     plt.plot(x, y.T)                                # 通过使用 plt.plot 函数绘制图象,其中,横坐标是 x , 纵坐标是 y.T, “.T” 表示的是矩阵的转置,因为绘图时需要横纵坐标的维度一致

  88.     # figure name:设置图像的文件名和标题名
  89.     # 设置图像的文件名为 'Training ' + str(iter_num) + ' times.png',其中,str(iter_num) 表示将迭代次数 iter_num 转变成字符串,图片格式为 “png”
  90.     fig_name = 'Training ' + str(iter_num) + 'times' + str(mylr)+'.png'
  91.     # 设置图像的标题名为'Training ' + str(iter_num) + ' times.png',其中,str(iter_num) 表示将迭代次数 iter_num 转变成字符串,图片格式为 “png”
  92.     plt.title(fig_name)
  93.     fig.savefig(fig_name)                           # 通过使用 fig.savefig 函数,保存图片,分辨率等参数采取默认值
  94.     # plt.show()                                    # 通过使用 plt.show 函数,显示绘制好的图片,注意的是必须关闭图像窗口,才可以进入执行后续的程序
  95.     plt.clf()#清除框线

  96. def loss(X, Y, w):                                  # 定义一个 损失函数 loss 函数 (loss function),输入参数是 X, Y, w, 返回的是 损失函数的值
  97.     m, n = np.shape(X)                              # 通过使用 np.shape 函数,得到数据集 X 的形状大小,其中,m 为数据集 X 的行数,n 为数据集 X 的列数
  98.     trainMat = np.mat(X)                            # 通过使用 np.mat 函数,将数据集 X 转变成矩阵类型,并赋值给 trainMat
  99.     Y_ = []                                         # 准备数据,定义一个空的列表,并赋值给 Y_,进行初始化, 后续会通过 append 的方法向空列表内不断添加新的元素
  100.     for i in np.arange(m):                          # 通过 for 循环结构,遍历数据集 X 所有的行,其中,i 可取的数依次为:0,1 ,2,....,m-1, 数据集 X总共有 m 行
  101.         # 通过 append 的方法向空列表 Y_ 内不断添加新的元素,新元素是通过 训练的矩阵数据集  trainMat[i] 乘以权重 w 之后,再计算激活函数 sigmoid 的函数值
  102.         Y_.append(sigmoid(trainMat[i]*w))
  103.     m = np.shape(Y_)[0]                             # 通过使用 np.shape 函数,得到数据集 X 的形状大小,其中,np.shape(Y_)[0] 为数据集 X 的行数,并赋值给 m
  104.     sum_err = 0.0                                   # 初始化误差的总和为 0.0, 赋值给 sum_err, 后续会不断更新 误差的总和 sum_err 的数值
  105.     for i in range(m):                              # 通过 for 循环结构,遍历数据集 Y_ 所有的行,其中,i 可取的数依次为:0,1 ,2,....,m-1, 数据集 Y_ 总共有 m 行
  106.         # 请补全 更新误差的总和 sum_err 的数值, 每次 误差的总和 sum_err 递减 Y[i]*np.log(Y_[i])+(1-Y[i])*np.log(1-Y_[i]),这是 交叉熵损失函数( Cross Entropy Loss )的计算公式
  107.         sum_err = Y_[i]*np.log(Y_[i])+(1-Y[i])*np.log(1-Y_[i])#请补全
  108.     return sum_err/m                                # 返回 sum_err

  109. # classify:定义一个 classify 函数,输入参数是 wx, 返回的是标志 1 或者 0
  110. def classify(wx):
  111.     prob = sigmoid(wx)                              # 计算概率:将激活函数 sigmoid(wx) 的数值作为预测的概率,并赋值给 prob
  112.     if prob > 0.5:                                  # 如果 概率 prob 大于 0.5, 那么返回数值 1
  113.         return 1
  114.     else:                                           # 否则,如果 概率 prob 不大于 0.5, 那么返回数值 0
  115.         return 0


  116. # predict:定义一个 predict 函数,输入参数是 测试集 testX 和权重 w, 返回的是预测的结果 result
  117. def predict(testX, w):
  118.     m, n = np.shape(testX)                          # 通过使用 np.shape 函数,得到测试集 testX 的形状大小,其中,m 为测试集 testX 的行数,n 为测试集 testX 的列数
  119.     testMat = np.mat(testX)                         # 通过使用 np.mat 函数,将测试集 testX 转换成矩阵类型,并赋值给 testMat
  120.     result = []                                     # 准备数据,定义一个空的列表,并赋值给结果 result,进行初始化, 后续会通过 append 的方法向空列表内不断添加新的元素
  121.     for i in np.arange(m):                          # 通过 for 循环结构,遍历测试集 testX 所有的行,其中,i 可取的数依次为:0,1 ,2,....,m-1, 测试集 testX 总共有 m 行
  122.         # 通过 append 的方法向空列表 result 内不断添加新的元素,新元素是通过调用 classify 函数进行预测得到,将返回的浮点型的 1 或者 0 添加到 空列表 result 内
  123.         result.append(classify(float(testMat[i]*w)))
  124.     return result                                   # 返回预测结果result


  125. # Precision:定义一个 Precision 函数,输入参数是数据集 X, Y 和权重 w, 返回的是 测试集的正确率
  126. def Precision(X, Y, w):
  127.     result = predict(X, w)                          # 通过调用 predict 函数,输入测试集 X 和权重 w, 计算得到预测结果,并把返回的结果赋值给 result
  128.     right_sum = 0                                   # 进行初始化预测正确的数目,赋值 0 给 right_sum,后续如果预测正确,会不断增加 1
  129.     # 通过 for 循环结构,开始进行遍历,其中,i 可取的数依次为:0,1 ,2,....,len(result)-1, 预测结果 result 内元素的个数总和为 len(result)
  130.     for i in range(len(result)):
  131.         if result[i]-int(Y[i]) == 0:                # 通过条件判断语句 if, 如果结果 result 的元素与 int(Y[i])相等,即:预测正确! 那么更新预测正确的数目 right_sum
  132.             right_sum += 1                          # 如果预测正确! 那么更新预测正确的数目 right_sum,每次递增加 1
  133.     # 最后返回测试集预测的正确率,计算公式为:1.0*right_sum/len(Y),注意:乘以 1.0 的原因是把正确率变成浮点型,当然也可以直接用 float 强制转换
  134.     return 1.0*right_sum/len(Y)


  135. def main(times,mylr):
  136.     begin = time.time()
  137.     data = loadData('data.xls')
  138.     X = data[:,:2]#身高与收入
  139.     y = data[:,2]#1与0
  140.    
  141.     '''
  142.     plt_data = plt.figure(1)
  143.     p1,p2 = plotData(X,y)
  144.     plt.xlabel('tall')
  145.     plt.ylabel('salary')
  146.     plt.legend((p1, p2), ('I like you', "I do not like you"), numpoints=1, handlelength=0)
  147.     plt_data.savefig('visualization_org.png')  # 通过调用 plt.savefig 函数,保存图像,并且图像的文件名为:'visualization_org.jpg',其中,图片的格式为 'jpg'
  148.     #plt.show()    # 通过调用 plt.show 函数,显示图像
  149.     #plt.close(plt_data)
  150.     '''
  151.    
  152.     '''将数据X归一化'''
  153.     X_norm = nomalization(X)
  154.     '''
  155.     plt_norm = plt.figure(1)#新建一个归一化的函数框
  156.    
  157.     p1_norm, p2_norm = plotData(X_norm, y)
  158.     #print(p1_norm)
  159.     #print(p2_norm)
  160.     plt.xlabel('tall')
  161.     plt.ylabel('salary')
  162.     plt.legend((p1_norm, p2_norm), ('I like you', 'I do not like you'), numpoints = 1, handlelength=0)
  163.     #plt_norm.savefig("visualization_norm.png")#保存图片
  164.     #plt.show()  # 通过调用 plt.show 函数,显示归一化后的图像
  165.     '''
  166.    
  167.     '''optimizing by BSD'''
  168.    
  169.     iter_num = times#训练次数
  170.     lr = mylr#alpha值
  171.     m, n = np.shape(data)#data为50行,三列的array
  172.     offset = np.ones((m, 1))#返回50行,1列的由[1.]填充的array
  173.     trainMat = np.c_[offset, X_norm]#合并offset, X_norm,返回50行3列的,第一列为1.的array
  174.     theta = BGD(trainMat, y, iter_num, lr)
  175.    
  176.    
  177.     # 通过调用 plotDecisionBoundary 函数,绘制分类决策的直线,其中,输入参数分别是:训练集 trainMat, 标签 y, 最优化后的权重 theta 和 迭代次数 iter_num
  178.     #plotDecisionBoundary(trainMat, y, theta, mylr, iter_num)
  179.     cost = loss(trainMat, y, theta)  # 通过调用 loss 函数,计算出本模型算法的损失函数,其中, 输入参数分别是: 训练集 trainMat, 标签 y 和 最优化后的权重 theta, 并赋值给 cost
  180.     print('Cost theta: {0}'.format(cost))  # 在屏幕上输出 损失函数的数值,其中,.format(cost) 的格式是更加规范的输出格式,当然也可以用转义字符 %s
  181.     print('%.10f'%cost)

  182.     # Compute accuracy on our training set
  183.     p = Precision(trainMat, y, theta)  # 通过调用 Precision 函数,计算出预测 测试集结果的正确率,其中,输入参数分别是: 训练集 trainMat, 标签 y 和 最优化后的权重 theta, 并赋值给 p
  184.     print('Train Accuracy: {0}'.format(p))  # 在屏幕上输出 测试集正确率的数值,其中,.format(p) 的格式是更加规范的输出格式,当然也可以用转义字符 %s
  185.     print('finished!')  # 在屏幕上输出完成的信息,'finished!'
  186.     end = time.time()
  187.     print(f'time:{end-begin:.1f} s')
  188.    
  189.     '''保存数据到txt
  190.     with open('result-lrdif.txt' , 'a') as result_txt:
  191.         result_txt.write(f' 200组数据-训练[{times}]次结果:\n lr(alpha):[{lr}]\n Cost theta(损失率):{cost}\n Train Accuracy(正确率):{p}\n Time(用时):{end-begin:.1f}\n =======================================\n\n')
  192.     '''

  193.     return p,cost
  194. def create3Dview(times,lr,p,base,fuck):
  195.     #显示中文,避免warning
  196.     plt.rcParams['font.sans-serif']=['SimHei']
  197.     plt.rcParams['axes.unicode_minus']=False

  198.     # 创建画布
  199.     fig = plt.figure()
  200.     # 创建3D坐标系
  201.     axes3d = Axes3D(fig,auto_add_to_figure=False)
  202.     fig.add_axes(axes3d)

  203.     times = np.array(times)
  204.     lr = np.array(lr)
  205.     colors = ['green','pink','blue','yellow','gray','red','purple','brown','orange']
  206.     #规定柱子的长宽
  207.     if len(lr)>1:
  208.         dy =  (lr[1]-lr[0])/2
  209.     else:
  210.         dy = lr[0]//2
  211.     if len(times)>1:
  212.         dx = (times[1]-times[0])/2
  213.     else:
  214.         dx = times[0]//2
  215.     for i in range(len(lr)):
  216.         axes3d.bar3d(times,[lr[i]]*base,[0]*base,#[0]*base是规定底部
  217.                      dx,dy,p[i]*fuck,
  218.                      color = np.random.choice(colors,1))
  219.     #plt.xticks(times)
  220.     plt.yticks(lr, lr)
  221.     plt.xlabel('times')
  222.     plt.ylabel('lr')
  223.     # 将三维的灰色背诵面换成白色
  224.     axes3d.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
  225.     axes3d.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
  226.     axes3d.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
  227.     axes3d.view_init(elev=30,azim=-60)
  228.     plt.show()
  229.    
  230. if __name__ == '__main__':
  231.     '''开始试验设置不同的alpha和times对于模型正确率以及损失函数之间的关系'''
  232.     diflr = []#添加不同的alpha到列表,注意必须是等差的
  233.     for i in range(1,5):
  234.         diflr.append(i/10)
  235.         
  236.     diftimes = []#添加不同的times(学习次数)到列表,注意必须是等差的
  237.     for i in range(1,5):
  238.         diftimes.append(i*10)
  239.         
  240.     p_result = []#正确率(结果)
  241.     cost_result = []#损失率(结果)
  242.     for each_lr in diflr:
  243.         for time_ in diftimes:
  244.             p,cost = main(time_,each_lr)
  245.             p_result.append(p)
  246.             cost_result.append(np.matrix.tolist(cost)[0][0])#将矩阵转换为浮点数后添加
  247.             
  248.     base = int(math.sqrt(len(p_result)))#base是作三维柱状图的参数,x轴的系数个数
  249.     '''将结果转换为base*base的二维array'''
  250.     p_result = np.array(p_result).reshape((base,base))
  251.     cost_result = np.array(cost_result).reshape((base,base))
  252.     '''调用画图函数'''
  253.     create3Dview(diftimes,diflr,p_result,base,1)
  254.     create3Dview(diftimes,diflr,cost_result,base,-1)
  255.    
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-28 02:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表