1
$\begingroup$

In the following code, I first declare a, b as tensorflow placeholders for initialising iterator iter with batch size 5. Later in the code, I want to see the predicted output by network one by one on test data, so I change batch size to 1 and use iter2 with placeholders c, d, and print len(predicted), it still comes out to be 5

import tensorflow as tf
import matplotlib.pyplot as plt

EPOCHS = 500

# to reproduce error, load X as a list of input features

BATCH_SIZE = 5
batch_size = tf.placeholder(tf.int64)
n_batches = int(len(X_train) / BATCH_SIZE)
# ---------------------------------------------------------------------------------------------------------------------

a, b = tf.placeholder(tf.float32, shape=[None, len(X[0])]), tf.placeholder(tf.float32, shape=[None, 8])

dataset = tf.data.Dataset.from_tensor_slices((a, b)).batch(batch_size=BATCH_SIZE).repeat()  # creating dataset for iterating
iter = dataset.make_initializable_iterator()
features, labels = iter.get_next()

c, d = tf.placeholder(tf.float32, shape=[None, len(X[0])]), tf.placeholder(tf.float32, shape=[None, 8])

        dataset2 = tf.data.Dataset.from_tensor_slices((c, d)).batch(batch_size=BATCH_SIZE).repeat()  # creating dataset for iterating
        iter2 = dataset2.make_initializable_iterator()
        features_test, labels_test = iter2.get_next()

hidden1 = tf.layers.dense(inputs=features, units=len(X[0]), name='first', activation=tf.nn.relu)
# .... -----------------------------------------------------------------------------------------------------

# Some network architecture

# .... -----------------------------------------------------------------------------------------------------
net = tf.layers.dense(hidden1, 40, name='net1', activation=tf.nn.relu)
prediction = tf.layers.dense(net, 8, activation=None)
loss = tf.losses.mean_squared_error(prediction, labels) + tf.losses.get_regularization_loss()

reg_less_loss = tf.losses.mean_squared_error(prediction, labels)

train_op = tf.train.AdamOptimizer().minimize(loss)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    sess.run(iter.initializer,
             feed_dict={a: train_data[0], b: train_data[1], batch_size: BATCH_SIZE, prob: 0.5})  # prob: 1.0
    print('Training...')
    for i in range(EPOCHS):
        tot_loss = 0
        for j in range(n_batches):
            _, loss_value = sess.run([train_op, loss])
            tot_loss += loss_value
        print("Iteration: {}, Loss: {:.5f}".format(i, tot_loss / n_batches))


    sess.run(iter2.initializer, feed_dict={c: test_data[0], d: test_data[1], batch_size: 1})
    for i in range(len(test_data[1])):
        test_loss, predicted = sess.run([reg_less_loss, prediction])
        print('Test Loss: {:4f}'.format(test_loss))
        print(predicted, len(predicted))
$\endgroup$
3
  • 1
    $\begingroup$ in your dataset2 definition, you still give BATCH_SIZE as a parameter, which is set to 5. $\endgroup$ – n1k31t4 Jun 25 '18 at 12:20
  • $\begingroup$ @n1k31t4 Okay that was a mistake but even when I replace BATCH_SIZE with 1 I still get len(predicted) = 5 and predicted is a list of 5 output samples $\endgroup$ – ab123 Jun 26 '18 at 6:03
  • $\begingroup$ @n1k31t4 I think the error is because prediction uses net->... ->hidden1->features to calculate output and features is made from a, b. How can this be solved? Should I declare a separate variable prediction_test which is made from a dataset of some placeholders d, e and batch size 1? $\endgroup$ – ab123 Jun 26 '18 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.