[PYTHON] Maximum likelihood estimation of mean and variance with TensorFlow

There is a misunderstanding that Tensorflow is a machine learning library, so I tried to find the mean and variance to deepen my understanding.

--Generate 100 normal random numbers with an average of 50 and a standard deviation of 10. --Some changes to the learning coefficient

If it's just the average, I tried to find the average of the sequence with TensorFlow. .

Gradient decent

python


import matplotlib.pylab as plt
%matplotlib inline
import numpy as np
import tensorflow as tf

x_train = np.random.randn(100) * 10 + 50

n_itr = 10000

m = tf.Variable([30.0], tf.float32)
s = tf.Variable([3.0], tf.float32)
x = tf.placeholder(tf.float32)
N = tf.count_nonzero(x, dtype=tf.float32)

loss = N / 2 * tf.log(2 * np.pi * s**2) + 1.0 / (2 * s**2) * tf.reduce_sum(tf.square(x - m))

for lr in [0.1, 0.01, 0.001]:

    optimizer = tf.train.GradientDescentOptimizer(lr)
    train = optimizer.minimize(loss)

    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    est_m = []
    est_s = []
    for i in range(n_itr):
        sess.run(train, {x:x_train})
        est_m.append(sess.run([m]))
        est_s.append(sess.run([s]))
        
    est_m = np.array(est_m)
    est_s = np.array(est_s)
    plt.plot(est_s.reshape(n_itr)[::100], est_m.reshape(n_itr)[::100], marker=".", label="lr={}".format(lr))

plt.title("batch gradient decent")
plt.xlabel("std")
plt.ylabel("mean")
plt.legend()
plt.show();

Unknown-5.png

RMS Prop

python


n_itr = 1000

for lr in [1, 0.5, 0.1]:

    optimizer = tf.train.RMSPropOptimizer(lr)
    train = optimizer.minimize(loss)

    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    est_m = []
    est_s = []
    for i in range(n_itr):
        sess.run(train, {x:x_train})
        est_m.append(sess.run([m]))
        est_s.append(sess.run([s]))
        
    est_m = np.array(est_m)
    est_s = np.array(est_s)
    plt.plot(est_s.reshape(n_itr)[::10], est_m.reshape(n_itr)[::10], marker=".", label="lr={}".format(lr))

plt.title("batch RMS Prop")
plt.xlabel("std")
plt.ylabel("mean")
plt.legend()
plt.show();

Unknown-4.png

Adam

python


n_itr = 1000

for lr in [5, 1, 0.1, 0.01]:

    optimizer = tf.train.AdamOptimizer(lr)
    train = optimizer.minimize(loss)

    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    est_m = []
    est_s = []
    for i in range(n_itr):
        sess.run(train, {x:x_train})
        est_m.append(sess.run([m]))
        est_s.append(sess.run([s]))
        
    est_m = np.array(est_m)
    est_s = np.array(est_s)
    plt.plot(est_s.reshape(n_itr)[::10], est_m.reshape(n_itr)[::10], marker=".", label="lr={}".format(lr))

plt.title("batch Adam")
plt.xlabel("std")
plt.ylabel("mean")
plt.legend()
plt.show();

Unknown-3.png

AdaGrad

python


n_itr = 1000

for lr in [20, 10, 5, 1]:

    optimizer = tf.train.AdagradOptimizer(lr)
    train = optimizer.minimize(loss)

    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    est_m = []
    est_s = []
    for i in range(n_itr):
        sess.run(train, {x:x_train})
        est_m.append(sess.run([m]))
        est_s.append(sess.run([s]))
        
    est_m = np.array(est_m)
    est_s = np.array(est_s)
    plt.plot(est_s.reshape(n_itr)[::10], est_m.reshape(n_itr)[::10], marker=".", label="lr={}".format(lr))

plt.title("batch AdaGrad")
plt.xlabel("std")
plt.ylabel("mean")
plt.legend()
plt.show();

Unknown-2.png

AdaDelta

python


n_itr = 1000

for lr in [5000, 1000, 100]:

    optimizer = tf.train.AdadeltaOptimizer(lr)
    train = optimizer.minimize(loss)

    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    est_m = []
    est_s = []
    for i in range(n_itr):
        sess.run(train, {x:x_train})
        est_m.append(sess.run([m]))
        est_s.append(sess.run([s]))
        
    est_m = np.array(est_m)
    est_s = np.array(est_s)
    plt.plot(est_s.reshape(n_itr)[::10], est_m.reshape(n_itr)[::10], marker=".", label="lr={}".format(lr))

plt.title("batch AdaDelta")
plt.xlabel("std")
plt.ylabel("mean")
plt.legend()
plt.show();

Unknown-1.png

Recommended Posts

Maximum likelihood estimation of mean and variance with TensorFlow
Advantages and disadvantages of maximum likelihood estimation
Maximum likelihood estimation of various distributions with Pyro
Example of python code for exponential distribution and maximum likelihood estimation (MLE)
Let's try again Maximum likelihood estimation and fitting of model (probability distribution) ① Discrete probability distribution
Let's try again Maximum likelihood estimation and fitting of model (probability distribution) ② Continuous probability distribution
Maximum likelihood estimation implementation of topic model in python
Least squares method and maximum likelihood estimation method (comparison by model fitting)
Coexistence of Python2 and 3 with CircleCI (1.0)
Compare raw TensorFlow with tf.contrib.learn and Keras
pix2 pix tensorflow2 Record of trial and error
Numerical calculation of differential equations with TensorFlow 2.0
Real-time calculation of mean values with coroutines
2. Mean and standard deviation with neural network!
Visualization of CNN feature maps and filters (Tensorflow 2.0)
Shuffle hundreds of thousands of images evenly with tensorflow.
Coexistence of Fcitx and Zoom ~ With Japanese localization ~
Recipe collection comparing versions 1 and 2 of TensorFlow (Part 1)
Script to tweet with multiples of 3 and numbers with 3 !!
Concept of Bayesian reasoning (2) ... Bayesian estimation and probability distribution
Implementation of TRIE tree with Python and LOUDS
Wavelet transform of images with PyWavelets and OpenCV
Consideration of propensity score and effect estimation accuracy
Portfolio optimization with Python (Markowitz's mean variance model)
Continuation of multi-platform development with Electron and Python
Sequential calculation of mean value with online algorithm
Example of reading and writing CSV with Python