用负对数似然损失实现简单概率模型

问题描述 投票:0回答:1

首先,我首先在深度学习和学习机器学习中将这个问题发布在Reddit上,但我想我也可能会在这里要求您的专业知识。事不宜迟:

[我目前在Deep Unsupervised Learning Course of Berkeley University年正在挑战自己,尽管我刚开始第1周的热身运动,但我已经遇到了“技术上的困难”。

所讨论的练习是以下文档中的“ 1.预热”:Week 1 Exercises。 (我很抱歉,我对Reddit格式不够熟悉,以至于似乎没有包含图像。

据我所知,我们有一个变量x,它可以从1..100中获取值,该值具有特定的采样概率(在sample_data()函数中定义)。因此,任务是拟合传递给softmax函数的参数theta的向量,并假定给定特定元素x_i被采样的可能性。即,theta_1应该是“凸显”与变量x = 1对应的soft-max值的参数,依此类推。

[使用Tensorflow,我认为我能够创建这样的模型,但是在训练时,我相信我缺少一个关键点,因为程序无法计算关于theta参数的梯度。

我想知道是否不是对任务的误解,以及是否有更好的方法来实现练习的结果。

这里是代码,失败的参数位于# Computing gradients的位置。

import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp

if __name__ == "__main__":
    # Sampling function of the x variable provided in the exercise
    def sample_data():
        count = 10000
        rand = np.random.RandomState(0)
        a = 0.3 + 0.1 * rand.randn(count)
        b = 0.8 + 0.05 * rand.randn(count)
        mask = rand.rand(count) < 0.5
        samples = np.clip(a * mask + b * (1 - mask), 0.0, 1.0)
        return np.digitize(samples, np.linspace(0.0, 1.0, 100))

    full_data = sample_data()
    train_ds = full_data[:int(.8*len( full_data))]
    val_ds = full_data[int(.8*len( full_data)):]

    # Declaring parameters theta
    w_init = tf.zeros_initializer()
    params = tf.Variable(
        initial_value=w_init(shape=(1, 100),
        dtype='float32'), trainable=True, name='params')


    softmax = tf.squeeze( tf.nn.softmax( params, axis=1))

    #Should materialize the loss of the model
    def get_neg_log_likelihood( inputs):
        return - tf.math.log( softmax)

    neg_log_likelihoods = get_neg_log_likelihood( softmax)

    dist = tfp.distributions.Categorical( probs=softmax, dtype=tf.int32)

    optimizer = tf.keras.optimizers.Adam()

    for epoch in range( 100):
        minibatch_size = 200
        n_minibatches = len( train_ds) // minibatch_size

        # Running over minibatches of the data
        for minibatch in range( n_minibatches):
            # Minibatching
            start_index = (minibatch*minibatch_size)
            end_index = (minibatch_size*minibatch + minibatch_size)

            x = train_ds[start_index:end_index]

            with tf.GradientTape() as tape:
                tape.watch( params)
                loss = tf.reduce_mean( - dist.log_prob( x))

            # Computing gradients
            grads = tape.gradient( loss, params)
            print( grads) # Result: None
            # input()
            optimizer.apply_gradients( zip( grads, params))

谢谢您的宝贵时间。

PS:我主要是在深度强化学习方面有背景的,因此我可以理解那里使用的各种模型(策略,价值函数...),但是我试图对模型本身的内部结构有所了解。在生成概率模型(GAN,VAE)和其他一般无监督的学习模型中(RealNVP,Norm Flows,...)]

首先,我首先在深度学习和学习机器学习中将这个问题发布在Reddit上,但我想我也可能会在这里要求您的专业知识。没有...

python tensorflow deep-learning unsupervised-learning
1个回答
0
投票

很确定没有人会看到这个,但我想我也应该对此做些封闭。

首先,我通过从soft-max值的负对数似然直接推导其表达式来计算梯度,因此在同一情况下放弃了Tensorflow框架。

© www.soinside.com 2019 - 2024. All rights reserved.