Skip to content

Instantly share code, notes, and snippets.

@huseinzol05
Last active January 14, 2018 03:22
Show Gist options
  • Save huseinzol05/d980e4f4c5198df1c03db5c53cddeaec to your computer and use it in GitHub Desktop.
Save huseinzol05/d980e4f4c5198df1c03db5c53cddeaec to your computer and use it in GitHub Desktop.
initiate 2 graphs and sessions in Tensorflow
import numpy as np
import tensorflow as tf
class Feed_forward:
def __init__(self, input_dimension):
self.X = tf.placeholder(tf.float32, [None, input_dimension])
self.Y = tf.placeholder(tf.float32, [None, 1])
hidden_layer = tf.Variable(tf.random_uniform([input_dimension, 1], -1.0, 1.0))
logits = tf.matmul(self.X, hidden_layer)
self.cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = logits, labels = self.Y))
self.optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.1).minimize(self.cost)
X = np.random.normal(size=(100, 10))
Y = np.random.randint(0, 2, size=(100,1))
epoch = 10
first_graph = tf.Graph()
with first_graph.as_default():
first_model = Feed_forward(X.shape[1])
first_sess = tf.InteractiveSession()
first_sess.run(tf.global_variables_initializer())
second_graph = tf.Graph()
with second_graph.as_default():
second_model = Feed_forward(X.shape[1])
second_sess = tf.InteractiveSession()
second_sess.run(tf.global_variables_initializer())
for i in range(epoch):
loss, _ = first_sess.run([first_model.cost, first_model.optimizer], feed_dict={first_model.X:X, first_model.Y:Y})
print('epoch:', i, ',loss from first model:', loss)
loss, _ = second_sess.run([second_model.cost, second_model.optimizer], feed_dict={second_model.X:X, second_model.Y:Y})
print('epoch:', i, ',loss from second model:', loss)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment