I have a piece of code that uses tf.nn.softmax to predict whether does a image belongs to either class 0, 1, 2... etc.

However, I want to edit the code to using sigmoid as the activation function and outputing all the probabilities, and setting those with probabilities >0.5 as one of the classes identified in the image.

This is the code which I am trying to run: https://github.com/satyenrajpal/Concrete-Crack-Detection

I believe this is the code snippet where my edit should be made:

            for counter,image in enumerate(test_images):
                #break up images into 128*128
                broken_image,h,w,h_no,w_no = break_image(image,128)

                output_image = np.zeros((h_no*128,w_no*128,3),dtype = np.uint8)

                feed_dict = {x: broken_image}
                batch_predictions = sess.run(predictions, feed_dict = feed_dict)

                print("here is one loop")


#                file = open("test.txt","w")
#                bpred_str = batch_predictions.astype('str')
#                file.write(bpred_str)
#                file.write(" ")
#                file.close()

                results=np.concatenate((results, batch_predictions))

                matrix_pred = batch_predictions.reshape((h_no,w_no))
                #Concentrate after this for post processing
                for i in range(0,h_no):
                    for j in range(0,w_no):
                        a = matrix_pred[i,j]
                        output_image[128*i:128*(i+1),128*j:128*(j+1),:] = 1-a

                cropped_image = image[0:h_no*128,0:w_no*128,:]                    
                pred_image = np.multiply(output_image,cropped_image)

I tried to print batch_predictions, but instead it prints out something like:

[1 1 0 1 1 0]

Another snippet is:

#Predict the class
y_pred = tf.nn.sigmoid(layer_fc2)
print("a: %s",y_pred)
#print("Class Probability: %s"%(sess.run(y_pred)))
self.y_pred_cls = tf.argmax(y_pred, dimension=1,name="predictions")

#Cost Function
cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(logits=layer_fc2, labels=self.y_true)
print("b: %s",cross_entropy)
cost = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)

correct_prediction = tf.equal(self.y_pred_cls, self.y_true_cls)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Class Probabilities: %s", probabilities)
return optimizer, accuracy

I also tried to print the output of tf.nn.sigmoid(y_pred), but it gave:

Class Probabilities: %s Tensor("Sigmoid_1:0", shape=(?, 2), dtype=float32)

I need help on how to print out the individual probabilities of the classes when I run the model on a unlabelled data. Thank you in advance!

  • $\begingroup$ what is the output of "y_pred = tf.nn.sigmoid(layer_fc2) print("a: %s",y_pred)" $\endgroup$ Feb 7, 2019 at 2:13
  • $\begingroup$ It prints "a: %s Tensor("Sigmoid:0", shape=(?, 2), dtype=float32)" $\endgroup$ Feb 7, 2019 at 3:46
  • $\begingroup$ Whearas b gives me "b: %s Tensor("logistic_loss:0", shape=(?, 2), dtype=float32)" $\endgroup$ Feb 7, 2019 at 4:04

1 Answer 1


You need to print output of sigmoid before tf.argmax is applied. It can be done with "tf.print", tf.print is a network node that does modify values.

y_pred = tf.nn.sigmoid(layer_fc2)
y_pred = tf.print(y_pred ,[y_pred ])
self.y_pred_cls = tf.argmax(y_pred, dimension=1,name="predictions")

Docs : https://www.tensorflow.org/api_docs/python/tf/print

  • $\begingroup$ hi thanks for your reply. However, it seems to hit me with this error: "TypeError: Can't convert Operation 'PrintV2' to Tensor (target dtype=None, name='input', as_ref=False)" $\endgroup$ Feb 7, 2019 at 6:45
  • $\begingroup$ then i followed the docs for tf.print and proceeded to use "tf.print(y_pred ,[y_pred ])" instead, there isnt any error, but it didnt print out anything. $\endgroup$ Feb 7, 2019 at 6:47
  • $\begingroup$ then i followed the docs for tf.print and proceeded to use "tf.print(y_pred ,[y_pred ])" instead, there isnt any error, but it didnt print out anything. $\endgroup$ Feb 7, 2019 at 6:47
  • $\begingroup$ Docs mention "with tf.control_dependencies([print_op]):" does that help ? Basically print is only enabled for certain scenarios. $\endgroup$ Feb 7, 2019 at 7:16
  • $\begingroup$ it didnt print anything either =( $\endgroup$ Feb 7, 2019 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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