TensorFlow docs明确指出,批次应沿第一个轴,例如文档中的第一个示例:
model.add(Dense(32, input_shape=(16,)))
# now the model will take as input arrays of shape (*, 16)
# and output arrays of shape (*, 32)
例如,如果批量大小为10,则此处的输入为(10, 16)
,输出为(10, 32)
。那么,为什么我的2个神经元密集层输出(2, *)
而不是(*, 2)
?
我插入了print()
形状的激活层从该密集层接收的形状,因此编译后可以看到shape=(2,)
。
library(keras)
library(tidyverse)
set.seed(2019)
weibull_activate <- function(ab) {
print(k_shape(ab))
a = k_exp(ab[, 1])
b = k_softplus(ab[, 2])
a = k_reshape(a, c(-1, 1))
b = k_reshape(b, c(-1, 1))
return(k_concatenate(list(a, b)))
}
weibull_loglik_continuous <- function(y_true, y_pred) {
y_ = y_true[, 1]
u_ = y_true[, 2]
a_ = y_pred[, 1]
b_ = y_pred[, 2]
ya = (y_ + 1e-35) / a_
return(-1 * k_mean(u_ * (k_log(b_) + b_ * k_log(ya)) - k_pow(ya, b_)))
}
test_data <- tibble(
x = runif(1e5, min = -1, max = 1),
true_shape = 2*x + 2.5,
true_scale = 10*x + 10.5,
y = map2_dbl(true_shape, true_scale, rweibull, n = 1),
o = 1
)
test_model <-
keras_model_sequential() %>%
layer_dense(input_shape = 1,
units = 2,
name = "dense_1") %>%
layer_activation(weibull_activate,
name = "weibull_activate") %>%
compile(optimizer = "rmsprop",
loss = weibull_loglik_continuous)
#> Tensor("weibull_activate/Shape:0", shape=(2,), dtype=int32)
很抱歉,MWE非常大,但也可能有助于了解整体情况。那两个神经元应该是Weibull参数,但是在训练我的真实数据时它们会一直为负,即使在这个简单的测试中,尽管它们会收敛于某些东西,但它们仍无法收敛于真实参数。我的怀疑是,此处进行的激活和损失计算在数学上是有效的,但却是无意义的,因为参数正在交换,或在观测值之间聚合或发生其他变化。
由reprex package(v0.3.0)
创建于2019-10-26