楼主: Lisrelchen
2331 12

Aboleth: Quick Start Guide [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2017-9-10 20:47:07 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容

Quick Start Guide — Aboleth 0.5.pdf (885.51 KB)

  1. Quick Start Guide
  2. In Aboleth we use function composition to compose machine learning models. These models are callable python classes that when called return a TensorFlow computational graph (really a tf.Tensor). We can best demonstrate this with a few examples.
复制代码

本帖隐藏的内容

http://aboleth.readthedocs.io/en/stable/



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Statistics statistic Bayesian Statist Statis

本帖被以下文库推荐

沙发
Lisrelchen 发表于 2017-9-10 20:47:45

Logistic Classification using Python aboleth

  1. Logistic Classification
  2. For our first example, lets make a simple logistic classifier with L2L2 regularisation on the model weights:

  3. import tensorflow as tf
  4. import aboleth as ab

  5. layers = (
  6.     ab.InputLayer(name="X") >>
  7.     ab.DenseMap(output_dim=1, l1_reg=0, l2_reg=.05) >>
  8.     ab.Activation(tf.nn.sigmoid)
  9. )
复制代码

藤椅
Lisrelchen 发表于 2017-9-10 20:51:37

Bayesian Logistic Classification using Python aboleth

  1. Bayesian Logistic Classification

  2. import numpy as np
  3. import tensorflow as tf
  4. import aboleth as ab

  5. layers = (
  6.     ab.InputLayer(name="X", n_samples=5) >>
  7.     ab.DenseVariational(output_dim=1, var=1., full=True) >>
  8.     ab.Activation(tf.nn.sigmoid)
  9. )

  10. #Model specification:

  11. likelihood = ab.likelihoods.Bernoulli()
  12. net, kl = layers(X=X_)
  13. loss = ab.elbo(net, Y_, N=10000, KL=kl, likelihood=likelihood)

  14. #Train this model in exactly the same way as the logistic classifier

  15. probabilities = net.eval(feed_dict={X_: X_query})
  16. #Take the mean of these samples for the predicted class probability

  17. expected_p = np.mean(probabilities, axis=0)
  18. #Generate more samples to get a more accurate expected probabilities (again with the TensorFlow session, sess),

  19. probabilities = ab.predict_samples(net, feed_dict={X_: X_query},
  20.                                    n_groups=10, session=sess)
复制代码

板凳
Lisrelchen 发表于 2017-9-10 20:54:41

Approximate Gaussian Processes using Python aboleth

  1. Approximate Gaussian Processes

  2. import tensorflow as tf
  3. import aboleth as ab

  4. lenscale = ab.pos(tf.Variable(1.))  # learn isotropic length scale
  5. kern = ab.RBF(lenscale=lenscale)

  6. layers = (
  7.     ab.InputLayer(name="X", n_samples=5) >>
  8.     ab.RandomFourier(n_features=100, kernel=kern) >>
  9.     ab.DenseVariational(output_dim=1, full=True)
  10. )

  11. var = ab.pos(tf.Variable(1.))  # learn likelihood variance

  12. likelihood = ab.likelihoods.Normal(var=var)
  13. net, kl = layers(X=X_)
  14. loss = ab.elbo(net, Y_, kl, N=10000, likelihood=likelihood)
复制代码

报纸
duoduoduo 在职认证  发表于 2017-9-10 20:57:45
bayes好东西

地板
Lisrelchen 发表于 2017-9-10 20:58:59
Regression

This is a simple demo that draws a random, non linear function from a Gaussian process with a specified kernel and length scale. We then use Aboleth (in Gaussian process approximation mode) to try to learn this function given only a few noisy observations of it. This script also demonstrates how we can divide the data into mini-batches using utilities in the tf.train module, and how we can use tf.train.MonitoredTrainingSession to log the learning progress.

This demo can be used to generate figures like the following:

  1. #! /usr/bin/env python3
  2. """This demo uses Aboleth for approximate Gaussian process regression."""
  3. import logging

  4. import numpy as np
  5. import bokeh.plotting as bk
  6. import bokeh.palettes as bp
  7. import tensorflow as tf
  8. # from sklearn.gaussian_process.kernels import Matern as kern
  9. from sklearn.gaussian_process.kernels import RBF as kern

  10. import aboleth as ab
  11. from aboleth.likelihoods import Normal
  12. from aboleth.datasets import gp_draws

  13. # Set up a python logger so we can see the output of MonitoredTrainingSession
  14. logger = logging.getLogger()
  15. logger.setLevel(logging.INFO)

  16. # Set up a consistent random seed in Aboleth so we get repeatable, but random
  17. # results
  18. RSEED = 666
  19. ab.set_hyperseed(RSEED)

  20. # Data settings
  21. N = 1000  # Number of training points to generate
  22. Ns = 400  # Number of testing points to generate
  23. kernel = kern(length_scale=0.5)  # Kernel to use for making a random GP draw
  24. true_noise = 0.1  # Add noise to the GP draws, to make things a little harder

  25. # Model settings
  26. n_samples = 5  # Number of random samples to get from an Aboleth net
  27. n_pred_samples = 10  # This will give n_samples by n_pred_samples predictions
  28. n_epochs = 200  # how many times to see the data for training
  29. batch_size = 10  # mini batch size for stochastric gradients
  30. config = tf.ConfigProto(device_count={'GPU': 0})  # Use GPU? 0 is no

  31. # Model initialisation
  32. variance = tf.Variable(1.)  # Likelihood variance initialisation, and learning
  33. reg = 1.  # Initial weight prior variance, this is optimised later

  34. # Random Fourier Features
  35. # lenscale = tf.Variable(1.)  # learn the length scale
  36. # kern = ab.RBF(lenscale=ab.pos(lenscale))  # keep the length scale positive

  37. # Variational Fourier Features -- length-scale setting here is the "prior", we
  38. # can choose to optimise this or not
  39. lenscale = 1.
  40. kern = ab.RBFVariational(lenscale=lenscale)  # This is VAR-FIXED kernel from
  41. # Cutjar et. al. 2017

  42. # This is how we make the "latent function" of a Gaussian process, here
  43. # n_features controls how many random basis functions we use in the
  44. # approximation. The more of these, the more accurate, but more costly
  45. # computationally. "full" indicates we want a full-covariance matrix Gaussian
  46. # posterior of the model weights. This is optional, but it does greatly improve
  47. # the model uncertainty away from the data.
  48. net = (
  49.     ab.InputLayer(name="X", n_samples=n_samples) >>
  50.     ab.RandomFourier(n_features=100, kernel=kern) >>
  51.     ab.DenseVariational(output_dim=1, var=reg, full=True)
  52. )


  53. def main():
  54.     """Run the demo."""
  55.     n_iters = int(round(n_epochs * N / batch_size))
  56.     print("Iterations = {}".format(n_iters))

  57.     # Get training and testing data
  58.     Xr, Yr, Xs, Ys = gp_draws(N, Ns, kern=kernel, noise=true_noise)

  59.     # Prediction points
  60.     Xq = np.linspace(-20, 20, Ns).astype(np.float32)[:, np.newaxis]
  61.     Yq = np.linspace(-4, 4, Ns).astype(np.float32)[:, np.newaxis]

  62.     # Set up the probability image query points
  63.     Xi, Yi = np.meshgrid(Xq, Yq)
  64.     Xi = Xi.astype(np.float32).reshape(-1, 1)
  65.     Yi = Yi.astype(np.float32).reshape(-1, 1)

  66.     _, D = Xr.shape

  67.     # Name the "data" parts of the graph
  68.     with tf.name_scope("Input"):
  69.         # This function will make a TensorFlow queue for shuffling and batching
  70.         # the data, and will run through n_epochs of the data.
  71.         Xb, Yb = batch_training(Xr, Yr, n_epochs=n_epochs,
  72.                                 batch_size=batch_size)
  73.         X_ = tf.placeholder_with_default(Xb, shape=(None, D))
  74.         Y_ = tf.placeholder_with_default(Yb, shape=(None, 1))

  75.     with tf.name_scope("Likelihood"):
  76.         lkhood = Normal(variance=ab.pos(variance))  # Keep the var positive

  77.     # This is where we build the actual GP model
  78.     with tf.name_scope("Deepnet"):
  79.         Phi, kl = net(X=X_)
  80.         loss = ab.elbo(Phi, Y_, N, kl, lkhood)

  81.     # Set up the trainig graph
  82.     with tf.name_scope("Train"):
  83.         optimizer = tf.train.AdamOptimizer()
  84.         global_step = tf.train.create_global_step()
  85.         train = optimizer.minimize(loss, global_step=global_step)

  86.     # This is used for building the predictive density image
  87.     with tf.name_scope("Predict"):
  88.         logprob = lkhood(Y_, Phi)

  89.     # Logging learning progress
  90.     log = tf.train.LoggingTensorHook(
  91.         {'step': global_step, 'loss': loss},
  92.         every_n_iter=1000
  93.     )

  94.     # This is the main training "loop"
  95.     with tf.train.MonitoredTrainingSession(
  96.             config=config,
  97.             save_summaries_steps=None,
  98.             save_checkpoint_secs=None,
  99.             hooks=[log]
  100.     ) as sess:
  101.         try:
  102.             while not sess.should_stop():
  103.                 sess.run(train)
  104.         except tf.errors.OutOfRangeError:
  105.             print('Input queues have been exhausted!')
  106.             pass

  107.         # Prediction
  108.         Ey = ab.predict_samples(Phi, feed_dict={X_: Xq, Y_: np.zeros_like(Yq)},
  109.                                 n_groups=n_pred_samples, session=sess)
  110.         logPY = ab.predict_expected(logprob, feed_dict={Y_: Yi, X_: Xi},
  111.                                     n_groups=n_pred_samples, session=sess)

  112.     Eymean = Ey.mean(axis=0)  # Average samples to get mean predicted funtion
  113.     Py = np.exp(logPY.reshape(Ns, Ns))  # Turn log-prob into prob

  114.     # Plot
  115.     im_min = np.amin(Py)
  116.     im_size = np.amax(Py) - im_min
  117.     img = (Py - im_min) / im_size
  118.     f = bk.figure(tools='pan,box_zoom,reset', sizing_mode='stretch_both')
  119.     f.image(image=[img], x=-20., y=-4., dw=40., dh=8,
  120.             palette=bp.Plasma256)
  121.     f.circle(Xr.flatten(), Yr.flatten(), fill_color='blue', legend='Training')
  122.     f.line(Xs.flatten(), Ys.flatten(), line_color='blue', legend='Truth')
  123.     for y in Ey:
  124.         f.line(Xq.flatten(), y.flatten(), line_color='red', legend='Samples',
  125.                alpha=0.2)
  126.     f.line(Xq.flatten(), Eymean.flatten(), line_color='green', legend='Mean')
  127.     bk.show(f)


  128. def batch_training(X, Y, batch_size, n_epochs):
  129.     """Batch training queue convenience function."""
  130.     X = tf.train.limit_epochs(X, n_epochs, name="X_lim")
  131.     Y = tf.train.limit_epochs(Y, n_epochs, name="Y_lim")
  132.     X_batch, Y_batch = tf.train.shuffle_batch([X, Y], batch_size, 100, 1,
  133.                                               enqueue_many=True, seed=RSEED)
  134.     return X_batch, Y_batch


  135. if __name__ == "__main__":
  136.     main()
复制代码


7
Lisrelchen 发表于 2017-9-10 21:00:25
Bayesian Classification with Dropout

Here we demonstrate a slightly different take on Bayesian deep learning. Yarin Gal in his thesis and associate publications demonstrates that we can view regular neural networks with dropout as a form of variational inference with specific prior and posterior distributions on the weights.

In this demo we implement this elegant idea with maximum a-posteriori weight and dropout layers in a classifier (see ab.layers). We leave these layers as stochastic in the prediction step, and draw samples from the network’s predictive distribution, as we would in variational networks.

We test the classifier against a random forest classifier on the breast cancer dataset with 5-fold cross validation, and get quite good and robust performance.

  1. #! /usr/bin/env python3
  2. """This script demonstrates an alternative way of making a Bayesian Neural Net.

  3. This is based on Yarin Gal's work on interpreting dropout networks as a special
  4. case of Bayesian neural nets, see http://mlg.eng.cam.ac.uk/yarin/blog_2248.html
  5. """
  6. import tensorflow as tf
  7. import numpy as np

  8. from sklearn.datasets import load_breast_cancer
  9. from sklearn.model_selection import KFold
  10. from sklearn.metrics import accuracy_score, log_loss
  11. from sklearn.ensemble import RandomForestClassifier
  12. from sklearn.preprocessing import StandardScaler

  13. import aboleth as ab


  14. FOLDS = 5
  15. RSEED = 100
  16. ab.set_hyperseed(RSEED)

  17. # Optimization
  18. NITER = 20000  # Training iterations per fold
  19. BSIZE = 10  # mini-batch size
  20. CONFIG = tf.ConfigProto(device_count={'GPU': 0})  # Use GPU ?
  21. LSAMPLES = 1  # We're only using 1 dropout "sample" for learning to be more
  22. # like a MAP network
  23. PSAMPLES = 50  # This will give LSAMPLES * PSAMPLES predictions
  24. REG = 0.001  # weight regularizer

  25. # Network structure
  26. net = ab.stack(
  27.     ab.InputLayer(name='X', n_samples=LSAMPLES),
  28.     ab.DropOut(0.95),
  29.     ab.DenseMAP(output_dim=64, l1_reg=0., l2_reg=REG),
  30.     ab.Activation(h=tf.nn.relu),
  31.     ab.DropOut(0.5),
  32.     ab.DenseMAP(output_dim=64, l1_reg=0., l2_reg=REG),
  33.     ab.Activation(h=tf.nn.relu),
  34.     ab.DropOut(0.5),
  35.     ab.DenseMAP(output_dim=1, l1_reg=0., l2_reg=REG),
  36.     ab.Activation(h=tf.nn.sigmoid)
  37. )


  38. def main():
  39.     """Run the demo."""
  40.     data = load_breast_cancer()
  41.     X = data.data.astype(np.float32)
  42.     y = data.target.astype(np.float32)[:, np.newaxis]
  43.     X = StandardScaler().fit_transform(X).astype(np.float32)
  44.     N, D = X.shape

  45.     # Benchmark classifier
  46.     bcl = RandomForestClassifier(random_state=RSEED)

  47.     # Data
  48.     with tf.name_scope("Input"):
  49.         X_ = tf.placeholder(dtype=tf.float32, shape=(None, D))
  50.         Y_ = tf.placeholder(dtype=tf.float32, shape=(None, 1))

  51.     with tf.name_scope("Likelihood"):
  52.         lkhood = ab.likelihoods.Bernoulli()

  53.     with tf.name_scope("Deepnet"):
  54.         Phi, reg = net(X=X_)
  55.         loss = ab.max_posterior(Phi, Y_, reg, lkhood, first_axis_is_obs=False)

  56.     with tf.name_scope("Train"):
  57.         optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
  58.         train = optimizer.minimize(loss)

  59.     kfold = KFold(n_splits=FOLDS, shuffle=True, random_state=RSEED)

  60.     # Launch the graph.
  61.     acc, acc_o, ll, ll_o = [], [], [], []
  62.     init = tf.global_variables_initializer()

  63.     with tf.Session(config=CONFIG):

  64.         for k, (r_ind, s_ind) in enumerate(kfold.split(X)):
  65.             init.run()

  66.             Xr, Yr = X[r_ind], y[r_ind]
  67.             Xs, Ys = X[s_ind], y[s_ind]

  68.             batches = ab.batch(
  69.                 {X_: Xr, Y_: Yr},
  70.                 batch_size=BSIZE,
  71.                 n_iter=NITER)
  72.             for i, data in enumerate(batches):
  73.                 train.run(feed_dict=data)
  74.                 if i % 1000 == 0:
  75.                     loss_val = loss.eval(feed_dict=data)
  76.                     print("Iteration {}, loss = {}".format(i, loss_val))

  77.             # Predict
  78.             Ey = ab.predict_expected(Phi, {X_: Xs}, PSAMPLES)

  79.             print("Fold {}:".format(k))
  80.             Ep = np.hstack((1. - Ey, Ey))

  81.             print_k_result(Ys, Ep, ll, acc, "BNN")

  82.             bcl.fit(Xr, Yr.flatten())
  83.             Ep_o = bcl.predict_proba(Xs)
  84.             print_k_result(Ys, Ep_o, ll_o, acc_o, "RF")
  85.             print("-----")

  86.         print_final_result(acc, ll, "BNN")
  87.         print_final_result(acc_o, ll_o, "RF")


  88. def print_k_result(ys, Ep, ll, acc, name):
  89.     acc.append(accuracy_score(ys, Ep.argmax(axis=1)))
  90.     ll.append(log_loss(ys, Ep))
  91.     print("{}: accuracy = {:.4g}, log-loss = {:.4g}"
  92.           .format(name, acc[-1], ll[-1]))


  93. def print_final_result(acc, ll, name):
  94.     print("{} final: accuracy = {:.4g} ({:.4g}), log-loss = {:.4g} ({:.4g})"
  95.           .format(name, np.mean(acc), np.std(acc), np.mean(ll), np.std(ll)))


  96. if __name__ == "__main__":
  97.     main()
复制代码


8
MouJack007 发表于 2017-9-10 21:03:04
谢谢楼主分享!

9
MouJack007 发表于 2017-9-10 21:03:21

10
军旗飞扬 发表于 2017-9-10 21:06:45
谢谢楼主分享!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-24 00:44