Hi dustin. Now I can save and restore model parameters, but I meet some difficulty in reconstructing the inference method. In the code below, when I try to run a reconstructed inference, there is an error:

ValueError: cannot add op with name optimizer/Variable_1/Adam as that name is already used

It seems that when I save the model, some inference object are samed, but how can I make a new inference and run it?

import tensorflow as tf

import edward as ed

import numpy as np

from edward.models import Normal

def build_toy_dataset(N, w, noise_std=0.1):

D = len(w)

x = np.random.randn(N, D)

y = np.dot(x, w) + np.random.normal(0, noise_std, size=N)

return x, y

##################code to save a linear regression model###########################

fname=‘E:\PythonCode\TestEdward\TestEdward\InferencesFiles_ED\MyInferences’

w_true =np.array([0.4,0.3,-0.1]) # np.random.randn(D)

N = 40 # number of data points

D = 3 # number of features

X_train, y_train = build_toy_dataset(N, w_true)

X_test, y_test = build_toy_dataset(N, w_true)

X = tf.placeholder(tf.float32, [N, D],name=‘X’)

w = Normal(loc=tf.zeros(D), scale=tf.ones(D),name=‘w’)

b = Normal(loc=tf.zeros(1), scale=tf.ones(1),name=‘b’)

y = Normal(loc=ed.dot(X, w) + b, scale=tf.ones(N),name=‘y’)

qw = Normal(loc=tf.Variable(tf.random_normal([D])),

scale=tf.nn.softplus(tf.Variable(tf.random_normal([D]))),name=‘qw’)

qb = Normal(loc=tf.Variable(tf.random_normal([1])),

scale=tf.nn.softplus(tf.Variable(tf.random_normal([1]))),name=‘qb’)

inference = ed.KLqp({w: qw, b: qb}, data={X: X_train, y: y_train})

inference.run(n_samples=5, n_iter=250)

saver = tf.train.Saver()

sess = ed.get_session()

save_path = saver.save(sess, fname)

####code to reload a model and run a new inference with new data########################

sess=tf.Session()

loader = tf.train.import_meta_graph(fname+’.meta’)

loader.restore(sess,fname)

graph = tf.get_default_graph()

X =graph.get_tensor_by_name(“X:0”)

w = Normal(loc=graph.get_tensor_by_name(“w/loc:0”),

scale=graph.get_tensor_by_name(“w/scale:0”),

value=graph.get_tensor_by_name(“w/sample/Reshape:0”))

b = Normal(loc=graph.get_tensor_by_name(“b/loc:0”),

scale=graph.get_tensor_by_name(“b/scale:0”),

value=graph.get_tensor_by_name(“b/sample/Reshape:0”))

qw = Normal(loc=graph.get_tensor_by_name(“qw/loc:0”),

scale=graph.get_tensor_by_name(“qw/scale:0”),

value=graph.get_tensor_by_name(“qw/sample/Reshape:0”))

qb = Normal(loc=graph.get_tensor_by_name(“qb/loc:0”),

scale=graph.get_tensor_by_name(“qb/scale:0”),

value=graph.get_tensor_by_name(“qb/sample/Reshape:0”))

print(‘qw’,qw.mean().eval(session=sess))

y = Normal(loc=graph.get_tensor_by_name(“y/loc:0”),

scale=graph.get_tensor_by_name(“y/scale:0”),

value=graph.get_tensor_by_name(“y/sample/Reshape:0”))

N = 40 # number of data points

D = 3 # number of features

X_test, y_test = build_toy_dataset(N, w_true)

inference = ed.KLqp({w: qw, b: qb}, data={X: X_test, y: y_test})

inference.run(n_samples=5, n_iter=50) ###The value error occurs here