首页 > 解决方案 > 如何矢量化 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

标签: pythonnumpydeep-learning

解决方案


像这样的东西怎么样:

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]]

推荐阅读