python - 如何矢量化 for 循环
问题描述
这是深度学习的一部分,我已经使用显式循环实现了前向传播,但我对如何仅使用 numpy 向量化操作感到困惑。
def forward(self, input_t):
output_t = np.zeros_like(input_t)
for i in range(input_t.shape[0]):
curr = input_t[i, :] - np.max(input_t[i, :])
output_t[i, :] = np.exp(curr) / np.sum(np.exp(curr))
self.store = output_t
return output_t
解决方案
像这样的东西怎么样:
import numpy as np
def forward_loop(input_t):
output_t = np.zeros_like(input_t)
for i in range(input_t.shape[0]):
curr = input_t[i, :] - np.max(input_t[i, :])
output_t[i, :] = np.exp(curr) / np.sum(np.exp(curr))
store = output_t
return output_t
def forward(input_t):
output_t = np.zeros_like(input_t)
curr = input_t - np.max(input_t, axis = 1).reshape(-1,1)
output_t = np.exp(curr) / np.sum(np.exp(curr), axis = 1).reshape(-1,1)
store = output_t
return output_t
x = np.random.rand(3, 3)
print(f'x = \n{x}')
print(f'y_loop = \n{forward_loop(x)}')
print(f'y = \n{forward(x)}')
输出:
x =
[[0.98055614 0.0364636 0.07754052]
[0.14524677 0.53440358 0.33880981]
[0.0406863 0.14387749 0.08279181]]
y_loop =
[[0.55729621 0.21680631 0.22589748]
[0.27105399 0.40000412 0.32894188]
[0.31728829 0.35177859 0.33093312]]
y =
[[0.55729621 0.21680631 0.22589748]
[0.27105399 0.40000412 0.32894188]
[0.31728829 0.35177859 0.33093312]]
推荐阅读
- intellij-idea - 如何跳转到同一个文件中的类声明?
- matlab - Matlab中SQL数据库的并行计算数据提取
- jmeter - 使用 Apache Jmeter 进行 JPOS 测试(功能和性能)
- vb.net - 调用带有两组括号和 Of 关键字的 VB 类方法是什么意思?
- linux - 是否可以使用 scp 创建一个空文件?
- python - 捕获感兴趣的区域
- angular - 在 Medium 上嵌入 Stackblitz 编辑器
- node.js - 尽管设置了 npm 和正确的路径,npm install 和 ng serve 命令仍无法正常工作
- excel - INDEX 和 MATCH 有两个引用
- php - PHP Telegram Bot 多次发送相同的消息