浅析贝叶斯神经网络

class VariationalDense:
    
    def __init__(self, n_in, n_out):
        
        self.W_mu = tf.Variable(tf.truncated_normal([n_in, n_out], stddev=stddev_var))
        self.W_logsigma = tf.Variable(tf.truncated_normal([n_in, n_out], mean = 0., stddev=stddev_var))
        self.b_mu = tf.Variable(tf.zeros([n_out]))
        self.b_logsigma = tf.Variable(tf.zeros([n_out]))
        
        self.epsilon_w = self.get_random([n_in,n_out],mu=0., std_dev=epsilon_prior)
        self.epsilon_b = self.get_random([n_out], mu=0., std_dev =epsilon_prior)
        
        self.W = self.W_mu + tf.multiply(tf.log(1. + tf.exp(self.W_logsigma)), self.epsilon_w)
        self.b = self.b_mu + tf.multiply(tf.log(1. + tf.exp(self.b_logsigma)), self.epsilon_b)
        
    def __call__(self, x, activation=tf.identity):
        output = activation(tf.matmul(x,self.W) + self.b)
        output = tf.squeeze(output)
        return output
    
    
    def log_gaussian(self, x, mu, sigma):
        return -0.5*tf.log(2*np.pi)-tf.log(sigma)-(x-mu)**2/(2*sigma**2)
 
    def get_random(self, shape, mu, std_dev):
        return tf.random_normal(shape, mean=mu, stddev=std_dev)
    
    def regularization(self):
        
        sample_log_pw, sample_log_qw= 0. , 0.
        
        sample_log_pw += tf.reduce_sum(self.log_gaussian(self.W, 0., sigma_prior))
        sample_log_pw += tf.reduce_sum(self.log_gaussian(self.b, 0., sigma_prior))
        
        sample_log_qw += tf.reduce_sum(self.log_gaussian(self.W, self.W_mu, tf.log(1. + tf.exp(self.W_logsigma))))
        sample_log_qw += tf.reduce_sum(self.log_gaussian(self.b, self.b_mu, tf.log(1. + tf.exp(self.b_logsigma))))
        
        regulizer = tf.reduce_sum((sample_log_qw-sample_log_pw))
        
        return regulizer
 
# Create the Model
n_sample = 20
X = np.random.normal(size=(n_sample,1))
y = np.random.normal(np.cos(5.*X) / (np.abs(X) + 1.), 0.1).ravel()
X_pred = np.atleast_2d(np.linspace(-3.,3.,num=100)).T
 
feature_size = X.shape[1]
n_hidden = 100
 
# Place holder of Network inputs and outputs
model_x = tf.placeholder(tf.float32, shape=[None, feature_size])
model_y = tf.placeholder(tf.float32, shape=[None])
 
#Define neural network
net_Layer_input = VariationalDense(feature_size, n_hidden)
net_Layer_hidden = VariationalDense(n_hidden, n_hidden)
net_Layer_output = VariationalDense(n_hidden, 1)
 
Input_Layer_output = net_Layer_input(model_x, tf.nn.relu)
Hidden_Layer_output = net_Layer_hidden(Input_Layer_output, tf.nn.relu)
net_pred =  net_Layer_output(Hidden_Layer_output)
 
# Define ELBO
sample_log_Likelihood = tf.reduce_sum(net_Layer_output.log_gaussian(model_y, net_pred, sigma_prior))
 
regularization_term = net_Layer_input.regularization() + net_Layer_hidden.regularization() + net_Layer_output.regularization()
 
elbo = -sample_log_Likelihood + regularization_term / n_hidden
 
train_step = tf.train.AdamOptimizer(lr).minimize(elbo)
 
# Mean Square Error (Network Performance)
model_mse = tf.reduce_sum(tf.square(model_y - net_pred))/n_sample

转载:https://blog.csdn.net/qq_20195745/article/details/82453589

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
最新的贝叶斯神经网络文献包括以下几篇: - "独立多元高斯分布与Variational inference in Bayesian neural networks" 这篇文章探讨了贝叶斯神经网络的独立多元高斯分布和变分推断方法。 - "浅析贝叶斯神经网络(Based on Variational Bayesian)" 这篇文章通过变分贝叶斯的方法,对贝叶斯神经网络进行了深入的分析。 - "Bayesian Neural Networks: 贝叶斯神经网络" 这篇文章对贝叶斯神经网络的基本概念和应用进行了介绍。 - "A Short Introduction to Bayesian Neural Networks" 这篇文章提供了关于贝叶斯神经网络的简要介绍,包括其与贝叶斯理论的结合以及解决神经网络挑战的能力。 这些文献都深入探讨了贝叶斯神经网络的方法和应用,对于了解最新的贝叶斯神经网络研究有很大的帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [深度学习——贝叶斯神经网络](https://blog.csdn.net/dhaiuda/article/details/106383465)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [MindSpore实践:神经网络贝叶斯神经网络](https://blog.csdn.net/Kenji_Shinji/article/details/126623955)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值