Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a tensor is empty in Tensorflow

I have part of my code as following:

class_label = tf.placeholder(tf.float32, [None], name="condition_checking")
row_index = tf.where(class_label > 0)

I want to check when row_index is empty to write the following

loss_f_G_filtered = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
    logits=y1_filterred, labels=y__filtered), name="filtered_reg")

if row_index == []:
  loss_f_G_filtered = tf.constant(0, tf.float32)

However, I do not know how to check if row_index is an empty tensor.

like image 296
amina mollaysa Avatar asked May 25 '18 12:05

amina mollaysa


2 Answers

is_empty = tf.equal(tf.size(row_index), 0)
like image 79
Siyuan Ren Avatar answered Sep 18 '22 00:09

Siyuan Ren


You can use tf.cond:

idx0 = tf.shape(row_index)[0]
loss_f_G_filtered = tf.cond(idx0 == 0,
                            lambda: tf.constant(0, tf.float32),
                            lambda: ...another function...)
like image 37
walkerlala Avatar answered Sep 19 '22 00:09

walkerlala