【问题标题】:Is there a way to speed up this piece of code in MATLAB?有没有办法在 MATLAB 中加快这段代码的速度?
【发布时间】:2015-10-08 19:02:17
【问题描述】:
Input = rand(32,32,3);
Theta = rand(10,16);


Output = zeros(30,30,3,16); % preallocate
for i = 1:30
     for j = 1:30
          Output(i,j,:,:) = permute(cat(2,ones(1,1,3),reshape(Input(i:i+2,j:j+2,1:3),1,9,3)), [2 3 1]).'*Theta;
     end
end

哇!我知道这里发生了很多事情,但也许有一种方法可以加快速度。此代码将 32 x 32 CMY 图像输入的通道分解为 3 x 3 重叠矩阵,将它们重新整形为向量,加 1 并乘以矩阵 Theta,以获得(卷积神经网络的)特征图​​作为输出。

【问题讨论】:

  • 你能写成矩阵形式吗?
  • @Drazick 你是什么意思?
  • @ĬnfernalSeraphím 您可能正试图将一个方程式转换为代码。这个方程是什么(就矩阵和运算符而言)?
  • 您的操作有多大,您正在寻找加速代码的速度?代码本身看起来不错。检查@Rafael 的答案

标签: matlab optimization multidimensional-array neural-network matrix-multiplication


【解决方案1】:

尝试更改此行:

Output(i,j,:,:) = permute(cat(2,ones(1,1,3),reshape(Input(i:i+2,j:j+2,1:3),1,9,3)), [2 3 1]).'*Theta;

到这里:

Output2(i,j,:,:) = [1 1 1; reshape(Input(i:i+2,j:j+2,:),9,3,1)].'*Theta;

平均一千次循环,代码的速度从 16.3ms 提高到 6.9ms

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多