h2o.deeplearning 自动编码器,手动计算深度特征

h2o.deeplearning autoencoder, calculating deep features manually

我想了解自动编码器中的特征有多深。

我用 h2o.deeplearning 创建了一个自动编码器,然后我尝试 手动计算深度特征。

自动编码器

fit = h2o.deeplearning(
x = names(x_train),
training_frame = x_train,
activation = "Tanh",
autoencoder = TRUE,
hidden = c(25,10),
epochs = 100,
export_weights_and_biases = TRUE,
)    

我使用 Tanh 作为激活函数和 2 个没有 dropout 的隐藏层,以 让事情变得简单。

手动计算隐藏层 1 深度特征

然后我提取了从输入层到隐藏层 1 的权重和偏差

w12 = as.matrix(h2o.weights(fit, 1))
b12 = as.matrix(h2o.biases (fit,1))

我准备了训练数据,用于在 [-0.5 , 0.5] 的紧凑区间因为 h2o 在 自编码器。

normalize = function(x) {(((x-min(x))/(max(x)-min(x))) - 0.5)}
d.norm =  apply(d, 2, normalize)`

然后我手动计算了第一层的deepfeatures

a12 = d.norm %*% t(w12)
b12.rep = do.call(rbind, rep(list(t(b12)), nrow(d.norm)))
z12 = a12 + b12.rep
f12 = tanh(z12)

当我将这些值与隐藏层 1 深层特征进行比较时,它们不匹配

hl1.output = as.matrix(h2o.deepfeatures(fit, x_train, layer = 1))
all.equal(
as.numeric(f12[,1]),
hl1.output[, 1],
check.attributes = FALSE,
use.names = FALSE,
tolerance = 1e-04
)
[1] "Mean relative difference: 0.4854887"

手动计算隐藏层 2 深层特征

然后我尝试做同样的事情来手动计算的深层特征 从隐藏层 1 的深层特征得到隐藏层 2

a23 = hl1.output %*% t(w23)
b23.rep = do.call(rbind, rep(list(t(b23)), nrow(a23)))
z23 = a23 + b23.rep
f23 = tanh(z23)

将这些值与隐藏层 2 的深层特征进行比较 我看到他们完美匹配

hl2.output = as.matrix(h2o.deepfeatures(fit,x_train,layer = 2))
all.equal(
as.numeric(f23[,1]),
hl2.output[, 1],
check.attributes = FALSE,
use.names = FALSE,
tolerance = 1e-04
)
[1] TRUE

正在手动计算输出层特征

我对输出层尝试了同样的事情

a34 = hl2.output %*% t(w34)
b34.rep = do.call(rbind, rep(list(t(b34)), nrow(a34)))
z34 = a34 + b34.rep
f34 = tanh(z34)

我将结果与我的输出进行了比较,但我无法得到相同的结果

all.equal(
as.numeric(f34[1,]),
output[1,],
check.attributes = FALSE,
use.names = FALSE,
tolerance = 1e-04
)
[1] "Mean relative difference: 3.019762"

问题

我认为我没有以正确的方式对数据进行归一化,因为我可以用隐藏层 1 的特征重新创建隐藏层 2 的深层特征。我不明白哪里出了问题,因为 autoencoder = TRUE h2o 应该规范化 [-0.5:0.5]

之间的数据

不明白为什么手动计算输出层没有 工作

1) 如何手动计算隐藏层1的深层特征?

2) 如何手动计算输出特征?

您正在使用:

 normalize = function(x) {(((x-min(x))/(max(x)-min(x))) - 0.5)}

他们正在使用 this Java code:

 normMul[idx] = (v.max() - v.min() > 0)?1.0/(v.max() - v.min()):1.0;
 normSub[idx] = v.mean();

然后使用like this:

numVals[i] = (numVals[i] - normSub[i])*normMul[i];

即减去平均值,然后除以范围(或者,等效地,在范围内乘以 1)。所以,忽略 divide-by-zero 的检查,我认为你的 R 代码需要是:

 normalize = function(x) {(x-mean(x))/(max(x)-min(x))}

检查是否为零,例如:

 normalize = function(x) {mul=max(x)-min(x);if(mul==0)mul=1;return((x-mean(x))/mul)}

随便玩玩,它的范围似乎是 1.0,但它不是以 0.0 为中心,即它不是 H2O 文档中描述的 -0.5 到 +0.5(例如深度学习手册中的第 20 页) ).我是否遗漏了 Java 代码中的某些内容?

顺便说一下,this line 是它决定对 auto-encoders 进行标准化的地方,而不是对其他深度学习进行标准化的地方。