初识Edward——一个简单的变分Bayesian网络
来源:互联网 发布:怎么举报网络诈骗 编辑:程序博客网 时间:2024/06/07 18:20
import numpy as npimport tensorflow as tffrom edward.models import Normalimport edward as edimport matplotlib.pyplot as pltdef build_dataset(N=50, noise_std=0.1): x = np.linspace(-3, 3, num=N) y = np.cos(x) + np.random.normal(0, noise_std, size=N) x = x.astype(np.float32).reshape((N, 1)) y = y.astype(np.float32) return x, ydef neural_network(x, W_0, W_1, b_0, b_1): h = tf.tanh(tf.matmul(x, W_0) + b_0) h = tf.matmul(h, W_1) + b_1 return tf.reshape(h, [-1])ed.set_seed(42)'''生成数据集'''N = 50 # 样本数D = 1 # 特征数(样本维度)x_train, y_train = build_dataset(N)fig1 = plt.figure()plt.plot(x_train, y_train, 'ks', alpha=0.5, label='(x, y)')plt.xlim([-5, 5])plt.ylim([-2, 2])plt.legend()plt.title('Samples')plt.show()'''定义一个2层Bayesian网络,使用tanh非线性'''W_0 = Normal(loc=tf.zeros([D, 2]), scale=tf.ones([D, 2]))W_1 = Normal(loc=tf.zeros([2, 1]), scale=tf.ones([2, 1]))b_0 = Normal(loc=tf.zeros(2), scale=tf.ones(2))b_1 = Normal(loc=tf.zeros(1), scale=tf.ones(1))x = x_trainy = Normal(loc=neural_network(x, W_0, W_1, b_0, b_1), scale=0.1 * tf.ones(N))'''使用变分推断,从数据中推断模型'''# 指定权值与偏置的正态逼近,变分因子的参数随机初始化,其中,softplus函数强制标准差参数大于0qW_0 = Normal(loc=tf.Variable(tf.random_normal([D, 2])), scale=tf.nn.softplus(tf.Variable(tf.random_normal([D, 2]))))qW_1 = Normal(loc=tf.Variable(tf.random_normal([2, 1])), scale=tf.nn.softplus(tf.Variable(tf.random_normal([2, 1]))))qb_0 = Normal(loc=tf.Variable(tf.random_normal([2])), scale=tf.nn.softplus(tf.Variable(tf.random_normal([2]))))qb_1 = Normal(loc=tf.Variable(tf.random_normal([1])), scale=tf.nn.softplus(tf.Variable(tf.random_normal([1]))))# 从变分模型中采样函数rs = np.random.RandomState(0)inputs = np.linspace(-5, 5, num=400, dtype=np.float32)x = tf.expand_dims(inputs, 1)mus = tf.stack([neural_network(x, qW_0.sample(), qW_1.sample(), qb_0.sample(), qb_1.sample()) for _ in range(10)])# 可视化先验sess = ed.get_session()tf.global_variables_initializer().run()outputs = mus.eval()fig = plt.figure(figsize=(10, 6))ax = fig.add_subplot(111)ax.set_title("Iteration: 0")ax.plot(x_train, y_train, 'ks', alpha=0.5, label='(x, y)')ax.plot(inputs, outputs[0].T, 'r', lw=2, alpha=0.5, label='prior draws')ax.plot(inputs, outputs[1:].T, 'r', lw=2, alpha=0.5)ax.set_xlim([-5, 5])ax.set_ylim([-2, 2])ax.legend()plt.show()# 使用KL散度进行变分推断,以从数据中推断出模型的隐变量inference = ed.KLqp({W_0: qW_0, b_0: qb_0, W_1: qW_1, b_1: qb_1}, data={y: y_train})inference.run(n_iter=1000)# 可视化后验outputs = mus.eval()fig = plt.figure(figsize=(10, 6))ax = fig.add_subplot(111)ax.set_title("Iteration: 1000")ax.plot(x_train, y_train, 'ks', alpha=0.5, label='(x, y)')ax.plot(inputs, outputs[0].T, 'r', lw=2, alpha=0.5, label='posterior draws')ax.plot(inputs, outputs[1:].T, 'r', lw=2, alpha=0.5)ax.set_xlim([-5, 5])ax.set_ylim([-2, 2])ax.legend()plt.show()
阅读全文
1 0
- 初识Edward——一个简单的变分Bayesian网络
- 【UNet】初识Unity Network,制作一个简单的网络聊天室
- 基于变分自编码器的生成对抗网络
- Edward‘Wonderland的开始
- 马尔可夫链的扩展 贝叶斯网络 (Bayesian Networks
- 马尔可夫链的扩展 贝叶斯网络 (Bayesian Networks)
- Hibernate初识---------------一个简单的hibernate开发
- 初识kotlin,实现一个简单的列表
- 一个简单的网络应用——面部拍照软件
- python初学者——一个简单的网络爬虫
- 分形技术的一个简单示例——雪花图案(AS3实现)
- LDA的变分推断
- lda的变分推理
- Retrofit + RxJava + OkHttp 让网络请求变的简单
- 算法杂货铺——分类算法之贝叶斯网络(Bayesian networks)
- 算法杂货铺——分类算法之贝叶斯网络(Bayesian networks)
- 算法杂货铺——分类算法之贝叶斯网络(Bayesian networks)
- 算法杂货铺——分类算法之贝叶斯网络(Bayesian networks)
- javascript基础知识总结
- java中的流
- WannaCry 2.0 已能通过 Wine 感染 Linux 系统
- jdbc简介与使用md
- 分销系统_三级分销_掌上商城推广新渠道-泉州市丰泽区尚创网络科技有限公司
- 初识Edward——一个简单的变分Bayesian网络
- caffe绘制训练过程的loss和accuracy曲线
- 【LeetCode】55. Jump Game
- Sqlserver取整
- FLIP 动画实现多维网格的过渡
- 核桃的数目
- java基础技术知识点总结——容器、迭代器
- 使用Jquery+EasyUI 进行框架项目开发案例讲解之一 员工管理源码分享
- thinkphp 出现Class 'PDO' not found 错误