594 KiB
594 KiB
In [1]:
import numpy as np
import scipy.sparse as sp
np.random.seed(13)
import warnings
# Comment this to turn on warnings
warnings.filterwarnings('ignore')
### define Ising model aprams
# system size
L=40
# create 10000 random Ising states
states=np.random.choice([-1, 1], size=(1400,L))
def ising_energies(states_, plot_true=False):
"""
This function calculates the energies of the states in the nn Ising Hamiltonian
"""
L = states.shape[1]
J = np.zeros((L, L),)
for i in range(L):
J[i,(i+1)%L]=-0.5 # interaction between nearest-neighbors
J[(i+1)%L,i]=-0.5
# compute energies
E = np.einsum('...i,ij,...j->...',states_,J,states_)
if plot_true:
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.heatmap(J)
plt.title("True Hamiltonian")
plt.show()
return E
# calculate Ising energies
energies=ising_energies(states,plot_true=True)
# Adding noise:
noise_variance = 2.5
energies += np.random.normal(0,scale=np.sqrt(noise_variance), size=energies.shape)
In [10]:
new_states = np.einsum('bi,bo->bio',states,states)
new_states = new_states.reshape(new_states.shape[0],-1)In [11]:
import time
from sys import exit
t0 = time.time()
n = new_states.shape[0] # number of data
D = new_states.shape[1] # data dimension
# Prior:
variance = 2.5
w0 = np.zeros(D)
tau = 1 # 1 means unitary gaussian, determines the strength of the prior
V0 = tau**2*np.identity(D) # precision matrix of prior
V0_inv = np.linalg.inv(V0)
mean_x = np.mean(new_states,axis=0,keepdims=True)
X = new_states #- mean_x # data matrix with data as rows, centered
y = energies - np.mean(energies)
VN_inv = V0_inv + np.dot(X.T,X) / variance
VN = np.linalg.inv(VN_inv)
wN = np.dot(np.dot(VN,V0_inv),w0) + np.dot(np.dot(VN,X.T),y) / variance
t1 = time.time()-t0
In [12]:
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.heatmap(wN.reshape(L,L))
plt.title("Estimated Hamiltonian")
plt.show()In [13]:
dw = 0.001
w_range = np.arange(-1.,1., dw)
#print(w_range)
def Pw(index1,index2):
index = index1*L + index2
vec = wN.copy()
logs = np.zeros(len(w_range))
for k in range(len(w_range)):
w = w_range[k]
vec[index] = w
logs[k] = -0.5 * np.dot(np.dot((vec - wN).T, VN_inv),vec - wN)
logs -= np.max(logs)
P = np.exp(logs)
return P
def plot_w_distribution(ax, index1,index2,show=False,grid=True):
P = Pw(index1,index2)
ax.plot(w_range,P, label="$P(w_{%.i,%.i}|D)$" % (index1,index2))
ax.legend()
ax.grid() if grid else None
if show:
plt.show()
fig, axes = plt.subplots(2,2,sharex=False, sharey=True)
fig.set_size_inches(18.5*0.75, 10.5*0.7)
plot_w_distribution(axes[0,0], 0,0)
plot_w_distribution(axes[0,1],0,1)
plot_w_distribution(axes[1,0],1,0)
plot_w_distribution(axes[1,1],1,1)
plt.show()
In [14]:
def credible_interval(ax, index1, index2):
P_ = Pw(index1,index2)
# normalize
P_normed = P_ / np.sum(P_)
############################
# Water filling algorithm: #
############################
#points = np.zeros_like(P_normed, dtype=np.int)
points_taken= []
points = []
done = False
t = 0
while not done:
best=0
bestindex=0
for i in range(len(P_normed)-1):
if i not in points_taken:
val = P_normed[i]
if val > best:
best = val
bestindex = i
points_taken.append(bestindex)
points.append(best)
if np.sum(points) >= 0.95:
done=True
points_taken = np.array(points_taken, dtype=np.int)
argsorted = np.argsort(points_taken)
points_taken = points_taken[argsorted]
plot_w_distribution(ax, index1,index2,show=False,grid=False)
first_lastw = [w_range[points_taken[0]], w_range[points_taken[-1]]]
first_lastP = [P_[points_taken[0]], P_[points_taken[-1]]]
fill = np.zeros(len(points_taken)+2)
fill[1:-1] = P_[points_taken]
w_range_fill = np.zeros_like(fill)
w_range_fill[1:-1] = w_range[points_taken]
w_range_fill[0] = w_range_fill[1]
w_range_fill[-1] = w_range_fill[-2]
ax.fill(w_range_fill,fill,facecolor="red",alpha=0.5)
line = [P_[points_taken[0]],P_[points_taken[-1]]]
line = np.ones(2)*P_[points_taken[0]] # looks better, but not actually totally correct
ax.plot(first_lastw,line, "k", alpha=0.5)
fig, axes = plt.subplots(2,2,sharex=False, sharey=True)
fig.set_size_inches(18.5*0.75, 10.5*0.75)
credible_interval(axes[0,0], 0,0)
credible_interval(axes[0,1],0,1)
credible_interval(axes[1,0],1,0)
credible_interval(axes[1,1],1,1)
plt.suptitle("95 % Credible Interval")
plt.show()
In [15]:
test_states=np.random.choice([-1, 1], size=(1000,L))
# calculate Ising test energies
test_energies=ising_energies(test_states)
# remapping states:
test_states = np.einsum('bi,bo->bio',test_states,test_states)
test_states = test_states.reshape(test_states.shape[0],-1)
predicted_energies = np.dot(test_states, wN)
### R^2 - coefficient of determination
y_true_avg = np.mean(test_energies)
residuals = predicted_energies - test_energies
u = np.dot(residuals,residuals)
v = test_energies - y_true_avg
v = np.dot(v,v)
R_squared = 1 - u/v
print(R_squared)
0.92679016596241
In [ ]:
"""Trains a Bayesian neural network to classify data from the 2D Ising model.
The architecture is LeNet-5 [1].
#### References
[1]: Yann LeCun, Leon Bottou, Yoshua Bengio, and Patrick Haffner.
Gradient-based learning applied to document recognition.
_Proceedings of the IEEE_, 1998.
http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import warnings
# Dependency imports
from absl import flags
import matplotlib
matplotlib.use("Agg")
from matplotlib import figure # pylint: disable=g-import-not-at-top
from matplotlib.backends import backend_agg
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
import matplotlib.pyplot as plt
%matplotlib inline
from tensorflow.contrib.learn.python.learn.datasets import mnist
# TODO(b/78137893): Integration tests currently fail with seaborn imports.
warnings.simplefilter(action="ignore")
try:
import seaborn as sns # pylint: disable=g-import-not-at-top
HAS_SEABORN = True
except ImportError:
HAS_SEABORN = False
tfd = tfp.distributions
ISING = True
IMAGE_SHAPE = [40,40,1] if ISING else [28, 28, 1]
flags.DEFINE_float("learning_rate",
default=0.001,
help="Initial learning rate.")
flags.DEFINE_integer("max_steps",
default=6000,
help="Number of training steps to run.")
flags.DEFINE_integer("batch_size",
default=128,
help="Batch size.")
flags.DEFINE_string("data_dir",
default=os.path.join(os.getenv("TEST_TMPDIR", "/tmp"),
"bayesian_neural_network/data"),
help="Directory where data is stored (if using real data).")
flags.DEFINE_string(
"model_dir",
default=os.path.join(os.getenv("TEST_TMPDIR", "/tmp"),
"bayesian_neural_network/"),
help="Directory to put the model's fit.")
flags.DEFINE_integer("viz_steps",
default=400,
help="Frequency at which save visualizations.")
flags.DEFINE_integer("num_monte_carlo",
default=10,
help="Network draws to compute predictive probabilities.")
flags.DEFINE_bool("fake_data",
default=None,
help="If true, uses fake data. Defaults to real data.")
FLAGS = flags.FLAGS
def plot_weight_posteriors(names, qm_vals, qs_vals, fname):
"""Save a PNG plot with histograms of weight means and stddevs.
Args:
names: A Python `iterable` of `str` variable names.
qm_vals: A Python `iterable`, the same length as `names`,
whose elements are Numpy `array`s, of any shape, containing
posterior means of weight varibles.
qs_vals: A Python `iterable`, the same length as `names`,
whose elements are Numpy `array`s, of any shape, containing
posterior standard deviations of weight varibles.
fname: Python `str` filename to save the plot to.
"""
fig = figure.Figure(figsize=(6, 3))
canvas = backend_agg.FigureCanvasAgg(fig)
ax = fig.add_subplot(1, 2, 1)
for n, qm in zip(names, qm_vals):
sns.distplot(qm.flatten(), ax=ax, label=n)
ax.set_title("weight means")
ax.set_xlim([-1.5, 1.5])
ax.legend()
ax = fig.add_subplot(1, 2, 2)
for n, qs in zip(names, qs_vals):
sns.distplot(qs.flatten(), ax=ax)
ax.set_title("weight stddevs")
ax.set_xlim([0, 1.])
fig.tight_layout()
canvas.print_figure(fname, format="png")
print("saved {}".format(fname))
def plot_heldout_prediction(input_vals, label_vals, probs,
fname, n=10, title=""):
"""Save a PNG plot visualizing posterior uncertainty on heldout data.
Args:
input_vals: A `float`-like Numpy `array` of shape
`[num_heldout] + IMAGE_SHAPE`, containing heldout input images.
probs: A `float`-like Numpy array of shape `[num_monte_carlo,
num_heldout, num_classes]` containing Monte Carlo samples of
class probabilities for each heldout sample.
fname: Python `str` filename to save the plot to.
n: Python `int` number of datapoints to vizualize.
title: Python `str` title for the plot.
"""
fig = figure.Figure(figsize=(9, 3*n))
canvas = backend_agg.FigureCanvasAgg(fig)
indices = np.random.randint(low=0,high=input_vals.shape[0],size=n)
for i in range(n):
ax = fig.add_subplot(n, 3, 3*i + 1)
ax.imshow(input_vals[indices[i], :].reshape(IMAGE_SHAPE[:-1]), interpolation="None")
ax = fig.add_subplot(n, 3, 3*i + 2)
for prob_sample in probs:
sns.barplot(np.arange(2) if ISING else np.arange(10), prob_sample[indices[i], :], alpha=0.5 if ISING else 0.1, ax=ax)
ax.set_ylim([0, 1])
ax.set_title("posterior samples")
ax = fig.add_subplot(n, 3, 3*i + 3)
sns.barplot(np.arange(2) if ISING else np.arange(10), np.mean(probs[:, indices[i], :], axis=0), ax=ax)
ax.set_ylim([0, 1])
ax.set_title("predictive probs, correct=%.i" % label_vals[indices[i]] )
fig.suptitle(title)
fig.tight_layout()
canvas.print_figure(fname, format="png")
print("saved {}".format(fname))
def plot_test_prediction(input_vals, probs,
fname, n=10, title=""):
"""Save a PNG plot visualizing posterior uncertainty on heldout data.
Args:
input_vals: A `float`-like Numpy `array` of shape
`[num_heldout] + IMAGE_SHAPE`, containing heldout input images.
probs: A `float`-like Numpy array of shape `[num_monte_carlo,
num_heldout, num_classes]` containing Monte Carlo samples of
class probabilities for each heldout sample.
fname: Python `str` filename to save the plot to.
n: Python `int` number of datapoints to vizualize.
title: Python `str` title for the plot.
"""
fig = figure.Figure(figsize=(9, 3*n))
canvas = backend_agg.FigureCanvasAgg(fig)
indices = np.random.randint(low=0,high=input_vals.shape[0],size=n)
for i in range(n):
ax = fig.add_subplot(n, 3, 3*i + 1)
ax.imshow(input_vals[indices[i], :].reshape(IMAGE_SHAPE[:-1]), interpolation="None")
ax = fig.add_subplot(n, 3, 3*i + 2)
for prob_sample in probs:
sns.barplot(np.arange(2) if ISING else np.arange(10), prob_sample[indices[i], :], alpha=0.5 if ISING else 0.1, ax=ax)
ax.set_ylim([0, 1])
ax.set_title("posterior samples")
ax = fig.add_subplot(n, 3, 3*i + 3)
sns.barplot(np.arange(2) if ISING else np.arange(10), np.mean(probs[:, indices[i], :], axis=0), ax=ax)
ax.set_ylim([0, 1])
ax.set_title("predictive probs, test set")
fig.suptitle(title)
fig.tight_layout()
canvas.print_figure(fname, format="png")
print("saved {}".format(fname))
def build_input_pipeline(mnist_data, batch_size, heldout_size):
"""Build an Iterator switching between train and heldout data."""
# Build an iterator over training batches.
training_dataset = tf.data.Dataset.from_tensor_slices(
(mnist_data.train.images, np.int32(mnist_data.train.labels)))
print(mnist_data.train.images.shape)
training_batches = training_dataset.shuffle(
50000, reshuffle_each_iteration=True).repeat().batch(batch_size)
training_iterator = tf.compat.v1.data.make_one_shot_iterator(training_batches)
# Build a iterator over the heldout set with batch_size=heldout_size,
# i.e., return the entire heldout set as a constant.
heldout_dataset = tf.data.Dataset.from_tensor_slices(
(mnist_data.validation.images,
np.int32(mnist_data.validation.labels)))
heldout_frozen = (heldout_dataset.take(heldout_size).
repeat().batch(heldout_size))
heldout_iterator = tf.compat.v1.data.make_one_shot_iterator(heldout_frozen)
test_dataset = tf.data.Dataset.from_tensor_slices(
(mnist_data.test.images,
np.int32(mnist_data.test.labels)))
test_frozen = (test_dataset.take(heldout_size).
repeat().batch(heldout_size))
test_iterator = tf.compat.v1.data.make_one_shot_iterator(test_frozen)
# Combine these into a feedable iterator that can switch between training
# and validation inputs.
handle = tf.compat.v1.placeholder(tf.string, shape=[])
feedable_iterator = tf.compat.v1.data.Iterator.from_string_handle(
handle, training_batches.output_types, training_batches.output_shapes)
images, labels = feedable_iterator.get_next()
return images, labels, handle, training_iterator, heldout_iterator, test_iterator
def test_data_pipeline(mnist_data, batch_size):
"""Build an Iterator switching between train and heldout data."""
# Build a iterator over the heldout set with batch_size=heldout_size,
# i.e., return the entire heldout set as a constant.
heldout_dataset = tf.data.Dataset.from_tensor_slices(
(mnist_data.test.images))
heldout_frozen = (heldout_dataset.take(batch_size).
repeat().batch(batch_size))
test_iterator = tf.compat.v1.data.make_one_shot_iterator(heldout_frozen)
# Combine these into a feedable iterator that can switch between training
# and test inputs.
handle = tf.compat.v1.placeholder(tf.string, shape=[])
feedable_iterator = tf.compat.v1.data.Iterator.from_string_handle(
handle, heldout_dataset.output_types, heldout_dataset.output_shapes)
images = feedable_iterator.get_next()
return images, handle, test_iterator
def Get_ising_data():
import pickle
def read_t(t,root="/home/samknu/MyRepos/MLProjectIsingModel/data/IsingData/"):
data = pickle.load(open(root+'Ising2DFM_reSample_L40_T=%.2f.pkl'%t,'rb'))
return np.unpackbits(data).astype(int).reshape(-1,1600)
temperatures = np.arange(0.25, 4., step=0.25)
ordered = np.zeros(shape=(np.sum(temperatures<2.0),10000,1600))
disordered = np.zeros(shape=(np.sum(temperatures>2.5),10000,1600))
critical = np.zeros(shape=(np.sum((temperatures>=2.0)*(temperatures<=2.5)),10000,1600))
ordered_index = 0
disordered_index = 0
crit_index = 0
for i in range(len(temperatures)):
T = temperatures[i]
if T < 2.0:
ordered[ordered_index] = read_t(T)
ordered_index += 1
elif T > 2.5:
disordered[disordered_index] = read_t(T)
disordered_index += 1
else:
critical[crit_index] = read_t(T)
crit_index += 1
ordered = ordered.reshape(-1,1600) # 70000
disordered = disordered.reshape(-1,1600) # 50000
critical = critical.reshape(-1,1600) # 30000
# Shuffling before separating into training, validation and test set
np.random.shuffle(ordered)
np.random.shuffle(disordered)
np.random.shuffle(critical)
training_data = np.zeros((6000*12,1600))
validation_data = np.zeros((2000*12,1600))
test_data = np.zeros((2000*12 + 10000*3,1600))
training_data[:round(0.6*70000)] = ordered[:round(0.6*70000)]
training_data[round(0.6*70000):] = disordered[:round(0.6*50000)]
validation_data[:round(0.2*70000)] = ordered[round(0.6*70000):round(0.6*70000)+round(0.2*70000)]
validation_data[round(0.2*70000):] = disordered[round(0.6*50000):round(0.6*50000)+round(0.2*50000)]
test_data[:round(0.2*70000)] = ordered[round(0.6*70000)+round(0.2*70000):round(0.6*70000)+2*round(0.2*70000)]
test_data[round(0.2*70000):round(0.2*70000)+round(0.2*50000)] = disordered[round(0.6*50000)+round(0.2*50000):round(0.6*50000)+2*round(0.2*50000)]
test_data[round(0.2*70000)+round(0.2*50000):] = critical
training_labels = np.zeros(6000*12)
training_labels[round(0.6*70000):] = np.ones(round(0.6*50000))
validation_labels = np.zeros(2000*12)
validation_labels[round(0.2*70000):] = np.ones(round(0.2*50000))
# Class 0 is ordered, class 1 is disordered
############################################################
# Reshaping since we want them as matrices for convolution #
############################################################
training_data = training_data.reshape(-1,40,40)
training_data = training_data[:,:,:,np.newaxis]
validation_data = validation_data.reshape(-1,40,40)
validation_data = validation_data[:,:,:,np.newaxis]
test_data = test_data.reshape(-1,40,40)
test_data = test_data[:,:,:,np.newaxis]
del ordered
del disordered
del critical
del temperatures
#############################
# Shuffling data and labels #
#############################
indices = np.random.permutation(np.arange(training_data.shape[0]))
training_data = training_data[indices]
training_labels = training_labels[indices]
indices = np.random.permutation(np.arange(validation_data.shape[0]))
validation_data = validation_data[indices]
validation_labels = validation_labels[indices]
indices = np.random.permutation(np.arange(test_data.shape[0]))
test_data = test_data[indices]
#test_labels = test_labels[indices]
cut_train = 20000
cut_val = 5000
cut_test = 1000
training_data = training_data[:cut_train]
training_labels = training_labels[:cut_train]
validation_data = validation_data[:cut_val]
validation_labels = validation_labels[:cut_val]
test_data = test_data[:cut_test]
class Dummy(object):
pass
ising_data = Dummy()
ising_data.train=Dummy()
ising_data.train.images = training_data
ising_data.train.labels = training_labels
ising_data.train.num_examples = training_data.shape[0]
ising_data.validation=Dummy()
ising_data.validation.images = validation_data
ising_data.validation.labels = validation_labels
ising_data.validation.num_examples = validation_data.shape[0]
ising_data.test=Dummy()
ising_data.test.images = test_data
ising_data.test.labels = np.zeros(test_data.shape[0]) # dummy labels
ising_data.test.num_examples = test_data.shape[0]
return ising_data
def main(argv):
del argv # unused
if tf.io.gfile.exists(FLAGS.model_dir):
tf.compat.v1.logging.warning(
"Warning: deleting old log directory at {}".format(FLAGS.model_dir))
tf.io.gfile.rmtree(FLAGS.model_dir)
tf.io.gfile.makedirs(FLAGS.model_dir)
if ISING:
the_data = Get_ising_data()
else:
the_data = mnist.read_data_sets(FLAGS.data_dir, reshape=False)
(images, labels, handle, training_iterator, heldout_iterator, test_iterator) = build_input_pipeline(
the_data, FLAGS.batch_size, the_data.validation.num_examples)
# Build a Bayesian LeNet5 network. We use the Flipout Monte Carlo estimator
# for the convolution and fully-connected layers: this enables lower
# variance stochastic gradients than naive reparameterization.
with tf.compat.v1.name_scope("bayesian_neural_net", values=[images]):
neural_net = tf.keras.Sequential([
tfp.layers.Convolution2DFlipout(6,
kernel_size=5,
padding="SAME",
activation=tf.nn.relu),
tf.keras.layers.MaxPooling2D(pool_size=[2, 2],
strides=[2, 2],
padding="SAME"),
tfp.layers.Convolution2DFlipout(16,
kernel_size=5,
padding="SAME",
activation=tf.nn.relu),
tf.keras.layers.MaxPooling2D(pool_size=[2, 2],
strides=[2, 2],
padding="SAME"),
tfp.layers.Convolution2DFlipout(120,
kernel_size=5,
padding="SAME",
activation=tf.nn.relu),
tf.keras.layers.Flatten(),
tfp.layers.DenseFlipout(84, activation=tf.nn.relu),
tfp.layers.DenseFlipout(2) if ISING else tfp.layers.DenseFlipout(10)
])
logits = neural_net(images)
labels_distribution = tfd.Categorical(logits=logits)
# Compute the -ELBO as the loss, averaged over the batch size.
neg_log_likelihood = -tf.reduce_mean(
input_tensor=labels_distribution.log_prob(labels))
kl = sum(neural_net.losses) / the_data.train.num_examples # 72000 is the size of the training set
elbo_loss = neg_log_likelihood + kl
# Build metrics for validation. Predictions are formed from a single forward
# pass of the probabilistic layers. They are cheap but noisy predictions.
predictions = tf.argmax(input=logits, axis=1)
accuracy, accuracy_update_op = tf.compat.v1.metrics.accuracy(
labels=labels, predictions=predictions)
# Extract weight posterior statistics for layers with weight distributions
# for later visualization.
names = []
qmeans = []
qstds = []
for i, layer in enumerate(neural_net.layers):
try:
q = layer.kernel_posterior
except AttributeError:
continue
names.append("Layer {}".format(i))
qmeans.append(q.mean())
qstds.append(q.stddev())
with tf.compat.v1.name_scope("train"):
optimizer = tf.compat.v1.train.AdamOptimizer(
learning_rate=FLAGS.learning_rate)
train_op = optimizer.minimize(elbo_loss)
init_op = tf.group(tf.compat.v1.global_variables_initializer(),
tf.compat.v1.local_variables_initializer())
with tf.compat.v1.Session() as sess:
sess.run(init_op)
# Run the training loop.
train_handle = sess.run(training_iterator.string_handle())
heldout_handle = sess.run(heldout_iterator.string_handle())
test_handle = sess.run(test_iterator.string_handle())
for step in range(FLAGS.max_steps):
#for step in range(0):
_ = sess.run([train_op, accuracy_update_op],
feed_dict={handle: train_handle})
if step % 100 == 0:
loss_value, accuracy_value = sess.run(
[elbo_loss, accuracy], feed_dict={handle: train_handle})
print("Step: {:>3d} Loss: {:.3f} Accuracy: {:.3f}".format(
step, loss_value, accuracy_value))
if (step+1) % FLAGS.viz_steps == 0:
# Compute log prob of heldout set by averaging draws from the model:
# p(heldout | train) = int_model p(heldout|model) p(model|train)
# ~= 1/n * sum_{i=1}^n p(heldout | model_i)
# where model_i is a draw from the posterior p(model|train).
probs = np.asarray([sess.run((labels_distribution.probs),
feed_dict={handle: heldout_handle})
for _ in range(FLAGS.num_monte_carlo)])
mean_probs = np.mean(probs, axis=0)
image_vals, label_vals = sess.run((images, labels),
feed_dict={handle: heldout_handle})
probs_test = np.asarray([sess.run((labels_distribution.probs),
feed_dict={handle: test_handle})
for _ in range(FLAGS.num_monte_carlo)])
mean_probs_test = np.mean(probs_test, axis=0)
image_vals_test = sess.run((images),
feed_dict={handle: test_handle})
heldout_lp = np.mean(np.log(mean_probs[np.arange(mean_probs.shape[0]),
label_vals.flatten()]))
print(" ... Held-out nats: {:.3f}".format(heldout_lp))
qm_vals, qs_vals = sess.run((qmeans, qstds))
if HAS_SEABORN:
plot_weight_posteriors(names, qm_vals, qs_vals,
fname=os.path.join(
FLAGS.model_dir,
"step{:05d}_weights.png".format(step)))
plot_heldout_prediction(image_vals, label_vals, probs,
fname=os.path.join(
FLAGS.model_dir,
"step{:05d}_pred.png".format(step)),
title="mean heldout logprob {:.2f}"
.format(heldout_lp))
plot_test_prediction(image_vals_test, probs_test,
fname=os.path.join(
FLAGS.model_dir,
"step{:05d}_test_pred.png".format(step)))
if __name__ == "__main__":
tf.compat.v1.app.run() # this thing will run the main(argv) function with sys.argv as argumentW0816 18:16:38.151205 140047177033536 <ipython-input-1-ccf9659f8260>:403] Warning: deleting old log directory at /tmp/bayesian_neural_network/
(20000, 40, 40, 1)
W0816 18:16:45.300953 140047177033536 deprecation.py:323] From <ipython-input-1-ccf9659f8260>:236: DatasetV1.output_types (from tensorflow.python.data.ops.dataset_ops) is deprecated and will be removed in a future version. Instructions for updating: Use `tf.compat.v1.data.get_output_types(dataset)`. W0816 18:16:45.301640 140047177033536 deprecation.py:323] From <ipython-input-1-ccf9659f8260>:236: DatasetV1.output_shapes (from tensorflow.python.data.ops.dataset_ops) is deprecated and will be removed in a future version. Instructions for updating: Use `tf.compat.v1.data.get_output_shapes(dataset)`.
Step: 0 Loss: 137.559 Accuracy: 0.508 Step: 100 Loss: 129.428 Accuracy: 0.955 Step: 200 Loss: 124.614 Accuracy: 0.975 Step: 300 Loss: 119.730 Accuracy: 0.983 ... Held-out nats: -0.004 saved /tmp/bayesian_neural_network/step00399_weights.png saved /tmp/bayesian_neural_network/step00399_pred.png saved /tmp/bayesian_neural_network/step00399_test_pred.png Step: 400 Loss: 114.894 Accuracy: 0.987 Step: 500 Loss: 109.927 Accuracy: 0.989 Step: 600 Loss: 105.041 Accuracy: 0.991 Step: 700 Loss: 100.179 Accuracy: 0.992 ... Held-out nats: -0.001 saved /tmp/bayesian_neural_network/step00799_weights.png saved /tmp/bayesian_neural_network/step00799_pred.png saved /tmp/bayesian_neural_network/step00799_test_pred.png Step: 800 Loss: 95.349 Accuracy: 0.993 Step: 900 Loss: 90.562 Accuracy: 0.994 Step: 1000 Loss: 85.854 Accuracy: 0.994 Step: 1100 Loss: 81.265 Accuracy: 0.994 ... Held-out nats: -0.000 saved /tmp/bayesian_neural_network/step01199_weights.png saved /tmp/bayesian_neural_network/step01199_pred.png saved /tmp/bayesian_neural_network/step01199_test_pred.png Step: 1200 Loss: 76.613 Accuracy: 0.995 Step: 1300 Loss: 72.092 Accuracy: 0.995 Step: 1400 Loss: 67.628 Accuracy: 0.995 Step: 1500 Loss: 63.252 Accuracy: 0.996 ... Held-out nats: -0.000 saved /tmp/bayesian_neural_network/step01599_weights.png saved /tmp/bayesian_neural_network/step01599_pred.png saved /tmp/bayesian_neural_network/step01599_test_pred.png Step: 1600 Loss: 58.971 Accuracy: 0.996 Step: 1700 Loss: 54.797 Accuracy: 0.996 Step: 1800 Loss: 50.719 Accuracy: 0.996 Step: 1900 Loss: 46.775 Accuracy: 0.996 ... Held-out nats: -0.000 saved /tmp/bayesian_neural_network/step01999_weights.png saved /tmp/bayesian_neural_network/step01999_pred.png saved /tmp/bayesian_neural_network/step01999_test_pred.png Step: 2000 Loss: 42.951 Accuracy: 0.997 Step: 2100 Loss: 39.284 Accuracy: 0.997 Step: 2200 Loss: 35.774 Accuracy: 0.997 Step: 2300 Loss: 32.496 Accuracy: 0.997 ... Held-out nats: -0.000 saved /tmp/bayesian_neural_network/step02399_weights.png saved /tmp/bayesian_neural_network/step02399_pred.png saved /tmp/bayesian_neural_network/step02399_test_pred.png Step: 2400 Loss: 29.387 Accuracy: 0.997 Step: 2500 Loss: 26.434 Accuracy: 0.997 Step: 2600 Loss: 23.643 Accuracy: 0.997 Step: 2700 Loss: 21.023 Accuracy: 0.997 ... Held-out nats: -0.000 saved /tmp/bayesian_neural_network/step02799_weights.png saved /tmp/bayesian_neural_network/step02799_pred.png saved /tmp/bayesian_neural_network/step02799_test_pred.png Step: 2800 Loss: 18.600 Accuracy: 0.997 Step: 2900 Loss: 16.375 Accuracy: 0.998 Step: 3000 Loss: 14.435 Accuracy: 0.998 Step: 3100 Loss: 12.780 Accuracy: 0.997 ... Held-out nats: -0.000 saved /tmp/bayesian_neural_network/step03199_weights.png saved /tmp/bayesian_neural_network/step03199_pred.png saved /tmp/bayesian_neural_network/step03199_test_pred.png Step: 3200 Loss: 11.394 Accuracy: 0.998 Step: 3300 Loss: 10.121 Accuracy: 0.998 Step: 3400 Loss: 8.961 Accuracy: 0.998 Step: 3500 Loss: 7.911 Accuracy: 0.998 ... Held-out nats: -0.000 saved /tmp/bayesian_neural_network/step03599_weights.png saved /tmp/bayesian_neural_network/step03599_pred.png saved /tmp/bayesian_neural_network/step03599_test_pred.png Step: 3600 Loss: 6.966 Accuracy: 0.998 Step: 3700 Loss: 6.122 Accuracy: 0.998 Step: 3800 Loss: 5.373 Accuracy: 0.998 Step: 3900 Loss: 4.718 Accuracy: 0.998 ... Held-out nats: -0.000 saved /tmp/bayesian_neural_network/step03999_weights.png saved /tmp/bayesian_neural_network/step03999_pred.png saved /tmp/bayesian_neural_network/step03999_test_pred.png Step: 4000 Loss: 4.154 Accuracy: 0.998 Step: 4100 Loss: 3.666 Accuracy: 0.998 Step: 4200 Loss: 3.266 Accuracy: 0.998 Step: 4300 Loss: 2.917 Accuracy: 0.998 ... Held-out nats: -0.000 saved /tmp/bayesian_neural_network/step04399_weights.png saved /tmp/bayesian_neural_network/step04399_pred.png saved /tmp/bayesian_neural_network/step04399_test_pred.png Step: 4400 Loss: 2.624 Accuracy: 0.998 Step: 4500 Loss: 2.372 Accuracy: 0.998 Step: 4600 Loss: 2.153 Accuracy: 0.998 Step: 4700 Loss: 1.963 Accuracy: 0.998 ... Held-out nats: -0.000 saved /tmp/bayesian_neural_network/step04799_weights.png saved /tmp/bayesian_neural_network/step04799_pred.png saved /tmp/bayesian_neural_network/step04799_test_pred.png Step: 4800 Loss: 1.797 Accuracy: 0.998 Step: 4900 Loss: 1.674 Accuracy: 0.998 Step: 5000 Loss: 1.562 Accuracy: 0.998 Step: 5100 Loss: 1.463 Accuracy: 0.998





