faridcher / ml-course

Starter code of Prof. Andrew Ng's machine learning MOOC in R statistical language
https://www.coursera.org/learn/machine-learning
172 stars 148 forks source link

pred <- predict(Theta1, Theta2, X[rp[1],]) Error in rep(0, m) : invalid 'times' argument #3

Closed rajasekhar-thiru closed 7 years ago

rajasekhar-thiru commented 7 years ago

Hi Fared, The 94th line of the ex3_nn.R reads as follows:
pred <- predict(Theta1, Theta2, X[rp[i],])

It is called from inside this loop: for (i in 1:m){ pred <- predict(Theta1, Theta2, X[rp[i],]) }

I am getting the following error:

pred <- predict(Theta1, Theta2, X[rp[1],]) Error in rep(0, m) : invalid 'times' argument

The predict function worked fine when it was invoked the first time in the same program at line 76 pred <- predict(Theta1, Theta2, X)

I could not find anything wrong with the repeat command used inside the predict function at row 14 p <- rep(0,m) where m <- dim(X)[1]

I am not sure if the function results are cached in R the first time which causes problem when called again.

Can you please help.

https://github.com/faridcher/machine-learning-course/blob/master/Solutions/mlclass-ex3/ex3_nn.R

faridcher commented 7 years ago

The problem could be from the X. what is the value of X? Are you trying my solutions or yours? Debug the code and check for the value of X and then m <- dim(X)[1].

hope it helps.

rajasekhar-thiru commented 7 years ago

Farid, I am trying your solution... X is a 5000 by 400 matrix.

The predict function is called twice in the program. It worked fine first time and aborts with this error 2nd time.

Thanks

Raja


On Sat, 5/13/17, Farid Cheraghi notifications@github.com wrote:

Subject: Re: [faridcher/machine-learning-course] pred <- predict(Theta1, Theta2, X[rp[1],]) Error in rep(0, m) : invalid 'times' argument (#3) To: "faridcher/machine-learning-course" machine-learning-course@noreply.github.com Cc: "rajasekhar-thiru" rajasek@yahoo.com, "Author" author@noreply.github.com Date: Saturday, May 13, 2017, 3:51 PM

The problem could be from the X. what is the value of X? Are you trying my solutions or yours?

Debug the code and check for the value of X and then m <- dim(X)[1]. hope it helps.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

faridcher commented 7 years ago

I just opened the mlclass-ex3.Rproj and sourced the ex3_nn.R and it runs without errors.

predict function could be called many times and R doesn't cache the function calls.

rajasekhar-thiru commented 7 years ago

thanks. I will continue looking line by line...