0

In my code, I am trying to practice to use tr.train.batch function. In sess.run([optimizer]) line, it doesnt return anything and it is just frozen. Can you please find my mistake?

tensors = tf.convert_to_tensor(x_train, dtype=tf.float32)
tensors = tf.reshape(tensors, shape=x_train.shape)
batch = tf.train.batch([tensors], batch_size=BATCH_SIZE, enqueue_many=True)

# Weights and biases to hidden layer
Wh = tf.Variable(tf.random_normal([COLUMN-2, UNITS_OF_HIDDEN_LAYER],  mean=0.0, stddev=0.05))
bh = tf.Variable(tf.zeros([UNITS_OF_HIDDEN_LAYER]))
h = tf.nn.tanh(tf.matmul(batch, Wh) + bh)
# Weights and biases to output layer
Wo = tf.transpose(Wh) # tied weights
bo = tf.Variable(tf.zeros([COLUMN-2]))
y = tf.nn.tanh(tf.matmul(h, Wo) + bo)

# Objective functions
mean_sqr = tf.reduce_mean(tf.pow(batch - y, 2))
optimizer = tf.train.AdamOptimizer(LEARNING_RATE).minimize(mean_sqr)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for j in range(TRAINING_EPOCHS):
    sess.run([optimizer])
    print("optimizer: ")
user3104352
  • 1,100
  • 1
  • 16
  • 34

1 Answers1

0

The tf.train.batch is a queue, so you need to start the queues in the session by using tf.train.start_queue_runners. You can learn about this in tensorflow's threading and Queues guide.

Make the following changes:

with tf.Session() as sess:
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)  
try:
    # Training loop
    for j in range(TRAINING_EPOCHS):
        if coord.should_stop():
            break
        sess.run([optimizer])
        print("optimizer: ")

except Exception, e:
    # When done, ask the threads to stop.
    coord.request_stop(e)

finally:
    coord.request_stop()
   # Wait for threads to finish.
coord.join(threads)
Vijay Mariappan
  • 16,921
  • 3
  • 40
  • 59