0
$\begingroup$

I have designed a convolutional neural network using tensorflow which looks as follows

#Define a convolutional neural network function
def conv_net(x, weights, biases):  
    conv1 = conv2d(x, weights['wc1'], biases['bc1'])
    conv1 = maxpool2d(conv1, k=2)

    conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
    conv2 = maxpool2d(conv2, k=2)

    conv3 = conv2d(conv2, weights['wc3'], biases['bc3'])
    conv3 = maxpool2d(conv3, k=2)

    conv4 = conv2d(conv3, weights['wc4'], biases['bc4'])
    conv4 = maxpool2d(conv4, k=2)


    # Fully connected layer
    # Reshape conv2 output to fit fully connected layer input
    fc1 = tf.reshape(conv4, [-1, weights['wd1'].get_shape().as_list()[0]])
    fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
    fc1 = tf.nn.relu(fc1)
    # Output, class prediction 
    out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
    return out

The code I'm using to train and test the model is as follows :

#Train and Test the Model
with tf.Session() as sess:
    sess.run(init) 
    train_loss = []
    test_loss = []
    train_accuracy = []
    test_accuracy = []
    summary_writer = tf.summary.FileWriter('./Output', sess.graph)
    for i in range(training_iters):
        for batch in range(len(X_train)//batch_size):
            batch_x = X_train[batch*batch_size:min((batch+1)*batch_size,len(X_train))]
            batch_y = y_train[batch*batch_size:min((batch+1)*batch_size,len(y_train))] 
            # Run optimization op and Calculate batch loss and accuracy
            opt = sess.run(optimizer, feed_dict={x: batch_x,
                                                 y: batch_y})
            loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
                                                              y: batch_y})
        print("Iter " + str(i) + ", Loss= " + \
                      "{:.6f}".format(loss) + ", Training Accuracy= " + \
                      "{:.5f}".format(acc))
        print("Optimization Completed")

        # Calculate accuracy for all test data
        test_acc,valid_loss = sess.run([accuracy,cost], feed_dict={x: X_test,y : y_test})
        train_loss.append(loss)
        test_loss.append(valid_loss)
        train_accuracy.append(acc)
        test_accuracy.append(test_acc)
        print("Testing Accuracy:","{:.5f}".format(test_acc))
        summary_writer.close()

This prints the accuracy but not the predictions. How can I print the predictions?

p.s new to neural network

$\endgroup$
1
$\begingroup$

You can replace these lines:

opt = sess.run(optimizer, feed_dict={x: batch_x,
                                     y: batch_y})
loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
                                                  y: batch_y})

by:

opt_, loss_, acc_, out_ = sess.run([optimizer, cost, accuracy, out], feed_dict={x:batch_x,
                                                  y: batch_y})

In this way, you can evaluate all the parameters given in the list (optimizer, cost, accuracy, out) in a single sess.run(). After, if it is classification task, you will probably need to apply softmax and argmin to out_

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.