对于损失函数,我使用定制的负对数似然法进行正态分布,并应用了log-sum-exp技术。
我正在使用两个具有relu激活功能的隐藏层和60个神经元,一批60个和e-4学习率。
但是,结果显示,其中一个分布的概率始终为1,而另一个分布的概率为0。是否增加时期数没有关系。请注意,概率为1.0的分布的结果是完全合理的,但是鉴于有关该主题的先前工作,我很难相信在6万多个小时内,没有一个具有两种不同分布的混合。
任何有关如何纠正概率或可能是0-1概率的原因的建议都将受到高度赞赏。
from tensorflow.keras import backend as bk
# reading inputs, etc.
components = 2 # Number of normal distributions in mixture
no_parameters = 3 # Number of parameters of the mixtures (weight, mean, std. dev)
neurons = 60 # Number of neurons per layer
SB = 1 # Number of outputs we want to predict
# Make the input tensor: two covariates-- quantity & price.
inputs = ks.Input(shape=(X_train.shape[1],))
h1 = ks.layers.Dense(neurons, activation="relu",
kernel_initializer='ones', bias_initializer='ones')(inputs)
h2 = ks.layers.Dense(neurons, activation="relu",
kernel_initializer='ones', bias_initializer='ones')(h1)
alphas = ks.layers.Dense(components, activation="softmax", name="alphas",
kernel_initializer='ones', bias_initializer='ones')(h2)
mus = ks.layers.Dense(components, name="mus")(h2)
sigmas = ks.layers.Dense(components, activation="relu", name="sigmas",
kernel_initializer='ones', bias_initializer='ones')(h2)
outputVector = ks.layers.Concatenate(name="output")([alphas, mus, sigmas])
model = ks.Model(inputs=inputs, outputs=outputVector)
def slice_parameter_vectors(parameter_vector):
""" Returns an unpacked list of parameter vectors. """
return [parameter_vector[:, i * components:(i + 1) * components] for i in range(no_parameters)]
def log_sum_exp(x, axis=None):
"""Log-sum-exp trick implementation"""
x_max = bk.max(x, axis=axis, keepdims=True)
return bk.log(bk.sum(bk.exp(x - x_max),
axis=axis, keepdims=True)) + x_max
def mean_log_Gaussian_like2(y, parameter_vector):
""" Computes the mean negative log-likelihood loss of the observed price given the mixture parameters. """
alpha, mu, sigma = slice_parameter_vectors(parameter_vector) # Unpack parameter vectors
mu = tf.keras.backend.reshape(mu, [-1, SB, 2])
alpha = bk.softmax(bk.clip(alpha, 1e-8, 1.))
exponent = bk.log(alpha) - .5 * float(SB) * bk.log(2 * np.pi) \
- float(SB) * bk.log(sigma) \
- bk.sum((bk.expand_dims(y, 2) - mu) ** 2, axis=1) / (2 * (sigma) ** 2)
log_likelihood = log_sum_exp(exponent, axis=1)
return -bk.mean(log_likelihood)
model.compile(optimizer=ks.optimizers.Adam(learning_rate=1e-4, clipvalue=1.0), # , clipvalue=0.5
loss= mean_log_Gaussian_like2,
metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=60, epochs=500)
y_pred = model.predict(X_test)
答案 0 :(得分:0)
我解决了这个问题。解决方案是通过重新定义alpha来摆脱softmax函数。那就是alpha = bk.softmax(bk.clip(alpha,1e-8,1.))应该是alpha = bk.clip(alpha,1e-8,1.)。谢谢大家。