【问题标题】:Octave: which method is more efficient八度:哪种方法更有效
【发布时间】:2012-10-01 08:11:13
【问题描述】:

我正在 GNU Octave 中尝试一些机器学习算法,例如平方误差成本函数。我所说的正确的矢量化论坛是:

J = (X * theta - y)' * (X * theta - y) * (1/(2*m)

其中 X 是 m x n+1 矩阵,theta 是 n+1 x 1 向量,y 是 m x 1 向量。我的问题是第二种方式是否更快:

J = sum((X * theta - y).^2) * (1/(2*m))

因为它只计算一次X * theta -y。作为八度的新手,它似乎在windows上运行在一个非常有气质的环境中,我不知道如何对自己进行基准测试。

由于这比什么都更令人好奇,请随时告诉我,这根本不重要。

【问题讨论】:

  • 您确定这些公式会产生相同的结果吗?如果你想优化,为什么不写:TMP = (X * theta - y)J = TMP' * TMP * (1/(2*m))

标签: math optimization octave mathematical-optimization


【解决方案1】:

这会检查挂钟时间:

octave:2> tic; sleep(3); toc
Elapsed time is 3.00161 seconds.
octave:3> help tic

分辨率不太高,因此您可能需要在循环中运行多次计算。

要测量 CPU 时间,请使用cputime

octave:7> cputime()
ans =  0.21000
octave:8> sleep(3)
octave:9> cputime()
ans =  0.21000

【讨论】:

  • 谢谢,这些会有用的。
猜你喜欢
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 2013-06-05
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多