I am training a neural network for kaggle's handwitten digit compitition. To practice R, I translated Andrew Ng's neural network octave program into R. So I need to find a substitute for fmincg.
I found optimx and played with many methods in it. I observed that fmincg was faster and more accurate than optimx.
Here's my test:
Let's minimize y = x^4
, without constraint. Everyone knows x
should equal to zero.
Here's my matlab program:
clc;
clear all;
close all;
options = optimset('MaxIter', 50);
init = 1:.1:50;
init = init';
newFunc = @(p) costFunc(p);
tic
[nn_params, cost] = fmincg(newFunc, init, options);
toc
with costFunc
function:
function [cost grad] = costFunc(x)
cost = sum(x.^4);
grad = 4*x.^3;
end
And the R script:
costFunc = function (x)
{
return(sum(x^4))
}
grad = function(x) {
return(4*x^3)
}
startTime = proc.time()
results = optimx(seq(1, 50,.1), fn=costFunc, gr=grad,
itnmax=50, method=c("Rcgmin"))
proc.time() - startTime
Octave finished in 0.064
seconds with accuracy to 10^-8
.
R finished in 1.8
seconds with accuracy only to 10^-4
.
Of course this problem is far too easy. When I trained neural networks, for some calculations, octave can give results in minutes but R seems running forever.
I am wondering if anyone has come across this issue and who can suggest better minimization function in R as the substitute of fmincg in octave?