Can I run SGHMC with float64?

I am trying to run SGHMC with float64 as in the code below, but there is an error in the step inference.run():ValueError: Incompatible type conversion requested to type ‘float64’ for variabl
e of type ‘float32_ref’.

The code can run with KLqp() method. Then how can I run SGHMC with float64?
Further is it possible to set float64 as the default float type in Edward and tensorflow?

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import edward as ed
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import tensorflow as tf

from edward.models import Normal, Empirical
import time

def build_toy_dataset(N, noise_std=0.5):
  X = np.concatenate([np.linspace(0, 2, num=N / 2),
                      np.linspace(6, 8, num=N / 2)])
  y = 2.0 * X +3.+ 2. * np.random.normal(0, noise_std, size=N)
  X = X.reshape((N, 1))
  return np.float64(X), np.float64(y)

#from utils import ConfigureTFGPU
#ConfigureTFGPU()
#with True:
#with tf.device('/gpu:0'):
time_start=time.clock()

ed.set_seed(42)

N = 40  # number of data points
D = 1  # number of features

# DATA
X_train, y_train = build_toy_dataset(N)
X_test, y_test = build_toy_dataset(N)

# MODEL
X = tf.placeholder(tf.float64, [N, D])
w = Normal(loc=tf.zeros(D,tf.float64), scale=tf.ones(D,tf.float64))
b = Normal(loc=tf.zeros(1,tf.float64), scale=tf.ones(1,tf.float64))
y = Normal(loc=ed.dot(X, w) + b, scale=tf.ones(1,tf.float64))
#y = tf.Variable(ed.dot(X, w) + b)


# INFERENCE
T = 1000                        # Number of samples.
nburn = 100                     # Number of burn-in samples.
stride = 10                    # Frequency with which to plot samples.

qw = Empirical(params=tf.Variable(tf.truncated_normal([T, D],dtype=tf.float64),dtype=tf.float64))
qb = Empirical(params=tf.Variable(tf.truncated_normal([T, 1],dtype=tf.float64),dtype=tf.float64))
inference = ed.SGHMC({w: qw, b: qb}, data={X: X_train, y: y_train})
inference.run(step_size=1e-3,n_iter=10,logdir='E:\\PythonCode\\TestEdward\\mylog')

"""
qw = Normal(loc=tf.Variable(tf.truncated_normal([D],dtype=tf.float64)),
            scale=tf.nn.softplus(tf.Variable(tf.truncated_normal([D],dtype=tf.float64))))
qb = Normal(loc=tf.Variable(tf.truncated_normal([1],dtype=tf.float64)),
            scale=tf.nn.softplus(tf.Variable(tf.truncated_normal([1],dtype=tf.float64))))
inference = ed.KLqp({w: qw, b: qb}, data={X: X_train, y: y_train})
inference.run(n_samples=5, n_iter=1000)
"""

print('w_mean:',qw.mean().eval())
print('w_std:',qw.stddev().eval())

Thanks for spotting this. ed.HMC, ed.SGHMC, and ed.SGLD are currently limited to float32. I added a fix to this here (https://github.com/blei-lab/edward/pull/757).

2 Likes