First Edward program

Hi All

I am experimenting with Edward using code extarcted from http://edwardlib.org/getting-started.

I have copied the following code into Spyder:

import numpy as np

x_train = np.linspace(-3, 3, num=50)
y_train = np.cos(x_train) + np.random.normal(0, 0.1, size=50)
x_train = x_train.astype(np.float32).reshape((50, 1))
y_train = y_train.astype(np.float32).reshape((50, 1))

import tensorflow as tf
from edward.models import Normal

W_0 = Normal(loc=tf.zeros([1, 2]), scale=tf.ones([1, 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_train
y = Normal(loc=tf.matmul(tf.tanh(tf.matmul(x, W_0) + b_0), W_1) + b_1,
scale=0.1)

qW_0 = Normal(loc=tf.get_variable(“qW_0/loc”, [1, 2]), scale=tf.nn.softplus(tf.get_variable(“qW_0/scale”, [1, 2])))
qW_1 = Normal(loc=tf.get_variable(“qW_1/loc”, [2, 1]), scale=tf.nn.softplus(tf.get_variable(“qW_1/scale”, [2, 1])))
qb_0 = Normal(loc=tf.get_variable(“qb_0/loc”, [2]), scale=tf.nn.softplus(tf.get_variable(“qb_0/scale”, [2])))
qb_1 = Normal(loc=tf.get_variable(“qb_1/loc”, [1]), scale=tf.nn.softplus(tf.get_variable(“qb_1/scale”, [1])))

import edward as ed

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)

When I try running the code I get the following error from line 27 (qW_0 = Normal(loc=tf.get_variable(“qW_0/loc”, [1, 2]), scale=tf.nn.softplus(tf.get_variable(“qW_0/scale”, [1, 2])))):

ValueError: Variable qW_0/loc already exists, disallowed.

Can anyone please point out what I am doing wrong.

Many thanks in advance

Ross

I have solved it. Th problem stems from me being a nupty nooby: I was re-running in Spyder without stopping the kernel, so Spyder was seeing the variable defined by a previous run.

Thanks for your attention.

Ross

1 Like