【深度学习】L1W2-python
发布日期:2022-02-10 08:11:05
浏览次数:13
分类:技术文章
本文共 5412 字,大约阅读时间需要 18 分钟。
你需要记住的内容:
-np.exp(x)适用于任何np.array x并将指数函数应用于每个坐标
-sigmoid函数及其梯度 -image2vector通常用于深度学习 -np.reshape被广泛使用。 保持矩阵/向量尺寸不变有助于我们消除许多错误。 -numpy具有高效的内置功能 -broadcasting非常有用向量化的实现更加简洁高效。 对于更大的向量/矩阵,运行时间的差异变得更大。(主要就是使用numpy的一些内置函数)
在深度学习中,通常需要处理非常大的数据集。 因此,非计算最佳函数可能会成为算法中的巨大瓶颈,并可能使模型运行一段时间。 为了确保代码的高效计算,我们将使用向量化。 例如,尝试区分点/外部/元素乘积之间的区别。
import timex1 = [9, 2, 5, 0, 0, 7, 5, 0, 0, 0, 9, 2, 5, 0, 0]x2 = [9, 2, 2, 9, 0, 9, 2, 5, 0, 0, 9, 2, 5, 0, 0]### CLASSIC DOT PRODUCT OF VECTORS IMPLEMENTATION ###tic = time.process_time()dot = 0for i in range(len(x1)): dot+= x1[i]*x2[i]toc = time.process_time()print ("dot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")### CLASSIC OUTER PRODUCT IMPLEMENTATION ###tic = time.process_time()outer = np.zeros((len(x1),len(x2))) # we create a len(x1)*len(x2) matrix with only zerosfor i in range(len(x1)): for j in range(len(x2)): outer[i,j] = x1[i]*x2[j]toc = time.process_time()print ("outer = " + str(outer) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")### CLASSIC ELEMENTWISE IMPLEMENTATION ###tic = time.process_time()mul = np.zeros(len(x1))for i in range(len(x1)): mul[i] = x1[i]*x2[i]toc = time.process_time()print ("elementwise multiplication = " + str(mul) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")### CLASSIC GENERAL DOT PRODUCT IMPLEMENTATION ###W = np.random.rand(3,len(x1)) # Random 3*len(x1) numpy arraytic = time.process_time()gdot = np.zeros(W.shape[0])for i in range(W.shape[0]): for j in range(len(x1)): gdot[i] += W[i,j]*x1[j]toc = time.process_time()print ("gdot = " + str(gdot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")
输出:
dot = 278 ----- Computation time = 0.08447300000002933msouter = [[81. 18. 18. 81. 0. 81. 18. 45. 0. 0. 81. 18. 45. 0. 0.] [18. 4. 4. 18. 0. 18. 4. 10. 0. 0. 18. 4. 10. 0. 0.] [45. 10. 10. 45. 0. 45. 10. 25. 0. 0. 45. 10. 25. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [63. 14. 14. 63. 0. 63. 14. 35. 0. 0. 63. 14. 35. 0. 0.] [45. 10. 10. 45. 0. 45. 10. 25. 0. 0. 45. 10. 25. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [81. 18. 18. 81. 0. 81. 18. 45. 0. 0. 81. 18. 45. 0. 0.] [18. 4. 4. 18. 0. 18. 4. 10. 0. 0. 18. 4. 10. 0. 0.] [45. 10. 10. 45. 0. 45. 10. 25. 0. 0. 45. 10. 25. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]] ----- Computation time = 0.22404599999992225mselementwise multiplication = [81. 4. 10. 0. 0. 63. 10. 0. 0. 0. 81. 4. 25. 0. 0.] ----- Computation time = 0.10582699999994727msgdot = [26.19713459 12.20793127 23.40980652] ----- Computation time = 0.15482099999997168ms
x1 = [9, 2, 5, 0, 0, 7, 5, 0, 0, 0, 9, 2, 5, 0, 0]x2 = [9, 2, 2, 9, 0, 9, 2, 5, 0, 0, 9, 2, 5, 0, 0]### VECTORIZED DOT PRODUCT OF VECTORS ###tic = time.process_time()dot = np.dot(x1,x2)toc = time.process_time()print ("dot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")### VECTORIZED OUTER PRODUCT ###tic = time.process_time()outer = np.outer(x1,x2)toc = time.process_time()print ("outer = " + str(outer) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")### VECTORIZED ELEMENTWISE MULTIPLICATION ###tic = time.process_time()mul = np.multiply(x1,x2)toc = time.process_time()print ("elementwise multiplication = " + str(mul) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")### VECTORIZED GENERAL DOT PRODUCT ###tic = time.process_time()dot = np.dot(W,x1)toc = time.process_time()print ("gdot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")
输出:
dot = 278 ----- Computation time = 0.0msouter = [[81 18 18 81 0 81 18 45 0 0 81 18 45 0 0] [18 4 4 18 0 18 4 10 0 0 18 4 10 0 0] [45 10 10 45 0 45 10 25 0 0 45 10 25 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [63 14 14 63 0 63 14 35 0 0 63 14 35 0 0] [45 10 10 45 0 45 10 25 0 0 45 10 25 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [81 18 18 81 0 81 18 45 0 0 81 18 45 0 0] [18 4 4 18 0 18 4 10 0 0 18 4 10 0 0] [45 10 10 45 0 45 10 25 0 0 45 10 25 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]] ----- Computation time = 0.0mselementwise multiplication = [81 4 10 0 0 63 10 0 0 0 81 4 25 0 0] ----- Computation time = 0.0msgdot = [ 21.57937154 22.58814194 13.70092277] ----- Computation time = 0.0ms
下面是作业中的一些代码,可看可不看:
import numpy as npdef sigmoid(x): return 1 / (1 + np.exp(-x))def sigmoid_grad(x): return sigmoid(x) * (1 - sigmoid(x))def image2vector(image): v = image.reshape(3 * 3 * 2, 1) return vdef normalizeRows(x): x_norm = np.linalg.norm(x, axis=1, keepdims=True) return x / x_norm # python中有广播 所以可以直接除def softmax(x): # x = np.array([ # [9, 2, 5, 0, 0], # [7, 5, 0, 0, 0]]) x_exp = np.exp(x) x_sum = np.sum(x_exp, axis=1, keepdims=True) s = x_exp / x_sum # python中有广播 所以可以直接除 return s
转载地址:https://blog.csdn.net/qq_43567222/article/details/114586798 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年03月28日 20时51分03秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
CCF CSP 201709-1 打酱油
2019-04-25
CCF CSP 201604-1 折点计数
2019-04-25
CCF CSP 201503-1 图像旋转
2019-04-25
蓝桥杯 ADV-171 算法提高 身份证号码升级 (java)
2019-04-25
蓝桥杯 ADV168 算法提高 快乐司机(贪心 快排)(java)
2019-04-25
蓝桥杯 ADV_193 算法提高 新建Microsoft Word文档 (java)
2019-04-25
蓝桥杯 ADV-155 上帝造题5分钟 (java)
2019-04-25
Qt 非模态对话框exec()与模态对话框show()
2019-04-25
推荐有关Qt书籍、Qt中文参考文档(网上在线教程)、QTCN开发网和Qt入门视频
2019-04-25
蓝桥杯 ADV-150 周期字符串(字符串处理)(java)
2019-04-25
蓝桥杯 ADV-144 算法提高 01背包 (java)(动态规划算法)
2019-04-25
蓝桥杯 ADV-143 扶老奶奶过街(java)
2019-04-25
2440里面有个看门狗 定时器(Watch Dog Timer)
2019-04-25
volatile关键字的作用:防止变量被编译器优化
2019-04-25
Makefile 假想目标:.PHONY
2019-04-25
bss、data和rodata区别与联系
2019-04-25
C代码中如何使用链接脚本.lds中定义的变量
2019-04-25
ARM指令和Thumb指令的区别
2019-04-25
作为一个新人,怎样学习嵌入式Linux?
2019-04-25