library(tensorflow)
datasets <- tf$contrib$learn$datasets
mnist <- datasets$mnist$read_data_sets("MNIST-data", one_hot = TRUE)
img <- matrix(mnist$train$images[1, ], 28, 28)
avg <- function(x) {
output <- c()
for (i in 0:(length(x)/4-1)){
output <- c(output, mean(x[(1 + 4 * i):(4 * i + 4)]))
}
return(output)
}
img <- apply(img, 2, avg)
img <- apply(img, 1, avg)
print(img)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#[1,] 0.000000 0.00000000 0.000000000 0.00000000 0.0000000 0.00000000 0
#[2,] 0.000000 0.00000000 0.023774512 0.08627452 0.0000000 0.00000000 0
#[3,] 0.264951 0.65612750 0.596078470 0.56985298 0.6960785 0.03333334 0
#[4,] 0.000000 0.00000000 0.006127452 0.19632354 0.7279412 0.30784316 0
#[5,] 0.000000 0.19411766 0.595833369 0.49632356 0.6409314 0.31446080 0
#[6,] 0.000000 0.03480392 0.000000000 0.00000000 0.7100491 0.05465687 0
#[7,] 0.000000 0.00000000 0.000000000 0.10318628 0.2573530 0.00000000 0