1
$\begingroup$
# After loading input(features) and output(label) datasets, ...
EPOCHS = 1200

BATCH_SIZE = 5
batch_size = tf.placeholder(tf.int64)
n_batches = int(len(X_transformed)/BATCH_SIZE)


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

dataset = tf.data.Dataset.from_tensor_slices((a, b)).batch(BATCH_SIZE).repeat()  # creating dataset for iterating
iter = dataset.make_initializable_iterator()
features, labels = iter.get_next()
print(features)
print(labels)
# Creating an initializable iterator
# saver = tf.train.Saver({"weights": weights})
# -------------------------------------------------------------------------- 

prob = tf.placeholder_with_default(1.0, shape=())  # default prob = 1 for 
dropout so that we can test
# layer = tf.nn.dropout(layer, prob)
regularizer = tf.contrib.layers.l2_regularizer(0.001)
hidden1 = tf.layers.dense(inputs=features, units=len(X_transformed[0]), 
name='first', activation=tf.nn.relu,
                      kernel_regularizer=regularizer)
dropout = tf.layers.dropout(inputs=hidden1, rate=prob)

net = tf.layers.dense(hidden4, 40, name='net1', activation=tf.nn.relu, 
kernel_regularizer=regularizer)

prediction = tf.layers.dense(net, 8, activation=None, 
kernel_regularizer=regularizer)
# regularizer = tf.nn.l2_loss(weights)
# loss = tf.reduce_mean(loss + beta * regularizer)
loss = tf.losses.mean_squared_error(prediction, labels) + 
tf.losses.get_regularization_loss()

test_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.4})
    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: {:.2f}".format(i, tot_loss/n_batches))

# ----------------------------------------------------------------------
    # initialise iterator with test data
    dataset_test = tf.data.Dataset.from_tensor_slices((a, b)).batch(1).repeat()  
    # creating dataset for iterating
    iter_test = dataset_test.make_initializable_iterator()
    sess.run(iter_test.initializer, feed_dict={a: test_data[0], b: test_data[1], batch_size: BATCH_SIZE})
    for i in range(len(test_data)):                        # put batch_size: test_data[0].shape[0] again in above line
        # creating dataset for iterating test data
        plt.scatter(test_data[1][i][:4], test_data[1][i][4:8])  # plot actual test image coordinates
        # plt.show()
        print('Test Loss: {:4f}'.format(sess.run(test_loss)))
        _, predicted = sess.run([train_op, prediction])
        print(predicted, len(predicted[:4]), len(predicted[4:8]))
        plt.scatter(predicted[0][:4], predicted[0][4: 8])  # plot predicted coordinates
        plt.show()

I think that I have created a new iterator after the 3rd (#-------) in the above code for test data that takes batches of size 1 but I still get batches of size 5. I want to plot test data values and predicted test data values one by one.

$\endgroup$

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.