【发布时间】:2018-09-17 14:00:28
【问题描述】:
我正在尝试了解自动编码器的深度特征。
我用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)`
然后我手动计算了第一层的深度特征
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) 如何手动计算输出特征?
【问题讨论】:
标签: r deep-learning h2o autoencoder