【问题标题】:Can I optimize three parameters with a DistributionLambda layer?我可以使用 DistributionLambda 层优化三个参数吗?
【发布时间】:2021-12-14 10:52:21
【问题描述】:

我需要创建一个神经网络层来优化 GeneralizedExtremeValue 分布的三个参数(即 locscale 和 concentration),但是当一个DistributionLambda层传递多个参数时,所有训练指标都是nan,输出分布为空。

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import tensorflow_probability as tfp
import tensorflow as tf
tfd = tfp.distributions
tfkl = tf.keras.layers
tfpl = tfp.layers

# INPUT DATASETS ==========================================================

# creating a GEV distribution 
dist = tfd.GeneralizedExtremeValue(loc=2, scale=1, concentration=0.1)
# sampling the GEV to create a dataset
dataset = dist.sample(10**(5))

# creating some noise on the sample
def add_eps (values):
    eps = np.random.randn(len(values))
    return values + eps

x_train = (0.75* add_eps(dataset[:(8*10**4)]))
y_train = dataset[:(8*10**4)]
x_test = (0.75* add_eps(dataset[(8*10**4):]))

# plotting the input datasets
fig=plt.figure()
sns.histplot(dataset, bins=100, 
             stat='probability', kde=True, 
             color='r', label='Sample from GEV: y_train')
sns.histplot(x_train, bins=100, 
             stat='probability',
             kde=True,
             color='b', label='Modified sample from GEV: x_train')
plt.legend()
plt.show()

绘制x_trainy_train,看来输入数据集是对的。无论如何,神经网络输出一个带有 nan 而不是 meanstddev 的分布。我尝试更改 batch_sizeepochsDense 层的输出数量,但没有任何效果。

# CREATING BAYESIAN NETWORK ================================================

# model architecture
model = tf.keras.Sequential([
    tfkl.Dense(3, input_shape = (1,)),
    tfpl.DistributionLambda(
        lambda t: tfd.GeneralizedExtremeValue(loc=t[0], 
                             scale=t[1], concentration=t[2]))
    ])
negloglik = lambda y_true, y_pred: -y_pred.log_prob(y_true)
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.01), 
              loss=negloglik)

# Model Fitting
history = model.fit(x_train, y_train, 
                    validation_split=0.2, epochs=100, 
                    verbose=True, shuffle=True, 
                    batch_size =300)

# predictions
y_pred_mean = model(x_test.numpy().reshape(-1,1)).mean()
y_pred_std = model(x_test.numpy().reshape(-1,1)).stddev()

【问题讨论】:

    标签: python tensorflow keras tensorflow-probability


    【解决方案1】:

    尝试为参数添加一些约束。我特别不熟悉这种分布,但我很确定 sigma(比例)应该大于零,所以在你的情况下

    scale = 1e-3 + tf.math.softplus(0.01 *t[1])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多