The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

537690 (3) [Avatar] Offline
#1
There is a slight error in the logic of the naive element-wise examples in section 2.3.

The for loops need a sequence to work with.

naive_relu <- function(x) {
  for (i in 1:nrow(x)) 
    for (j in 1:ncol(x)) 
      x[i, j] <- max(x[i, j], 0)
  x
}
naive_add <- function(x, y) {
  for (i in 1:nrow(x))
    for (j in 1:ncol(x))
      x[i, j] = x[i, j] + y[i, j]
  x
}


256385 (47) [Avatar] Offline
#2
I'm not sure exactly what the problem is. These functions both seem to be valid and work as expected (they both accept matrices as input)
537690 (3) [Avatar] Offline
#3
The existing line
for (i in nrow(x))

causes the for loop to execute once and the value of i will be the number of rows in x.

Similarly, the second for loop will execute once and j will be the number of columns in x.

This line
x[i, j] <- max(x[i, j], 0)

will execute once and will modify the last row and last column of x.

The naive_relu and pmax functions should return the same thing which I tested like this
set.seed(1)
mxy = matrix(runif(9)-0.5,3,3)
naive_relu(mxy)
pmax(mxy,0)

I had to make sure some of the values where less than 0 to show the max function working correctly.
537690 (3) [Avatar] Offline
#4
537690 wrote:The existing line
for (i in nrow(x))

causes the for loop to execute once and the value of i will be the number of rows in x.

Similarly, the second for loop will execute once and j will be the number of columns in x.

This line
x[i, j] <- max(x[i, j], 0)

will execute once and will modify the last row and last column of x.

The naive_relu and pmax functions should return the same thing which I tested like this
set.seed(1)
mxy = matrix(runif(9)-0.5,3,3)
naive_relu(mxy)
pmax(mxy,0)

I had to make sure some of the values were less than 0 to show the max function working correctly.
256385 (47) [Avatar] Offline
#5
Okay, wow, I can't believe I missed that! Thank you so much for patiently pointing this out. I will make this change in errata (as the book has just gone to it's first printing) and then remedy in the second printing.