-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.R
155 lines (147 loc) · 4.8 KB
/
common.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
library(R.matlab)
library(reshape2)
library(ggplot2)
library(R6)
initializeParameters <- function(hiddenSize, visibleSize) {
r <- sqrt(6) / sqrt(hiddenSize+visibleSize+1)
W1 <- runif(hiddenSize * visibleSize) * 2 * r - r
b1 <- rep(0, hiddenSize)
W2 <- runif(visibleSize * hiddenSize) * 2 * r - r
b2 <- rep(0, visibleSize)
c(W1, b1, W2, b2)
}
sigmoid <- function(z) 1 / (1 + exp(-z))
sigmoidRnd <- function(z) {
ifelse(sigmoid(z) > matrix(runif(length(z)), nrow = nrow(z), ncol = ncol(z)), 1, 0)
}
feedForwardAutoencoder <- function(theta, hiddenSize, visibleSize, data) {
W1 <- matrix(theta[1:(hiddenSize*visibleSize)], hiddenSize, visibleSize)
b1 <- theta[(hiddenSize*visibleSize+1) : (hiddenSize*visibleSize+hiddenSize)]
z2 <- W1 %*% data + b1
a2 <- sigmoid(z2)
a2
}
sampleImages <- function(images, patchsize=8, numpatches=100000) {
imageRowDim <- dim(images)[1]
imageColDim <- dim(images)[2]
numImages <- dim(images)[3]
patches <- matrix(0, nrow=patchsize^2, ncol=numpatches)
for(i in 1:numpatches) {
x <- sample(1:(imageRowDim-patchsize+1), size=1)
y <- sample(1:(imageColDim-patchsize+1), size=1)
z <- sample(1:numImages, size=1)
patches[, i] <- as.vector(images[x:(x+patchsize-1), y:(y+patchsize-1), z])
}
patches <- patches - mean(patches)
pstd <- 3 * sd(patches)
patches[patches > pstd] <- pstd
patches[patches < -pstd] <- -pstd
patches <- (patches / pstd + 1) * 0.4 + 0.1
patches
}
sampleColorImages <- function(images, patchsize=8, numpatches=100000) {
imageRowDim <- dim(images)[1]
imageColDim <- dim(images)[2]
imageChannels <- dim(images)[3]
numImages <- dim(images)[4]
patches <- matrix(0, nrow=patchsize^2 * imageChannels, ncol=numpatches)
for(i in 1:numpatches) {
x <- sample(1:(imageRowDim-patchsize+1), size=1)
y <- sample(1:(imageColDim-patchsize+1), size=1)
z <- sample(1:numImages, size=1)
patches[, i] <- as.vector(images[x:(x+patchsize-1), y:(y+patchsize-1), , z])
}
patches
}
rotate <- function(x) t(apply(x, 2, rev))
displayColorNetwork <- function(A) {
if(min(A) >= 0) {
A <- A - mean(A)
}
cols = round(sqrt(ncol(A)))
channelSize <- nrow(A) / 3
dim <- sqrt(channelSize)
dimp <- dim + 1
rows <- ceiling(ncol(A) / cols)
B = A[1:channelSize,]
C = A[(channelSize+1):(channelSize*2),]
D = A[(2*channelSize+1):(channelSize*3),]
B <- B / max(abs(B))
C <- C / max(abs(C))
D <- D / max(abs(D))
I <- array(1, c(dim*rows+rows-1, dim*cols+cols-1, 3))
for(i in 0:(rows-1)) {
for(j in 0:(cols-1)) {
I[(i*dimp+1):(i*dimp+dim), (j*dimp+1):(j*dimp+dim), 1] <-
rotate(matrix(B[, i*cols+j+1],dim, dim))
I[(i*dimp+1):(i*dimp+dim), (j*dimp+1):(j*dimp+dim), 2] <-
rotate(matrix(C[, i*cols+j+1],dim, dim))
I[(i*dimp+1):(i*dimp+dim), (j*dimp+1):(j*dimp+dim), 3] <-
rotate(matrix(D[, i*cols+j+1],dim, dim))
}
}
I <- (I + 1) / 2
R <- melt(I[,, 1])
G <- melt(I[,, 2])
B <- melt(I[,, 3])
displayImage <- merge(R, G, by.x=c('Var1', 'Var2'), by.y = c("Var1", "Var2"), all = TRUE)
displayImage <- merge(displayImage, B, by.x=c('Var1', 'Var2'), by.y = c("Var1", "Var2"), all = TRUE)
colnames(displayImage) <- c('x', 'y', 'r', 'g', 'b')
ggplot(data=as.data.frame(displayImage), aes(x=x, y=y, fill=rgb(r,g,b))) +
geom_tile() +
scale_fill_identity()
}
up2Down <- function(A) A[,ncol(A):1]
displayNetwork <- function(A) {
if(min(A) <= 0) {
A <- A - min(A)
}
cols = round(sqrt(nrow(A)))
rows <- ceiling(nrow(A) / cols)
dim <- sqrt(ncol(A))
dimp <- dim + 1
I <- matrix(0, dim*rows+rows-1, dim*cols+cols-1)
for(i in 0:(rows-1)) {
for(j in 0:(cols-1)) {
I[(i*dimp+1):(i*dimp+dim), (j*dimp+1):(j*dimp+dim)] <-
up2Down(matrix(A[i*cols+j+1, ] / sqrt(sum(A[i*cols+j+1, ]^2)), dim, dim))
}
}
ggplot(melt(I), aes(Var1, Var2, fill=value)) +
geom_tile() +
scale_fill_gradient(low="#FFFFFF", high="#000000")
}
checkNumericalGradient <- function(theta, f, g) {
actualGrad <- g(theta)
epsilon <- 10^-4
gradient <- rep(0, length(theta))
for(i in 1:length(theta)) {
e <- rep(0, length(theta))
e[i] <- epsilon
gradient[i] <- (f(theta + e) - f(theta - e)) / (2 * epsilon)
if(abs(gradient[i] - actualGrad[i]) > 1e-9) {
print(sprintf("%s = %s", i, abs(gradient[i] - actualGrad[i])))
}
}
sum(abs(gradient - actualGrad))
}
loadImageFile <- function(filename) {
f = file(filename,'rb')
readBin(f,'integer',n=1,size=4,endian='big')
n = readBin(f,'integer',n=1,size=4,endian='big')
nrow = readBin(f,'integer',n=1,size=4,endian='big')
ncol = readBin(f,'integer',n=1,size=4,endian='big')
x = readBin(f,'integer',n=n*nrow*ncol,size=1,signed=F)
ret = matrix(x, nrow=nrow*ncol) / 255
close(f)
ret
}
loadLabelFile <- function(filename) {
f = file(filename,'rb')
readBin(f,'integer',n=1,size=4,endian='big')
n = readBin(f,'integer',n=1,size=4,endian='big')
y = readBin(f,'integer',n=n,size=1,signed=F)
close(f)
y[y == 0] <- 10
y
}