Quanter
Quanter

Reputation: 85

tf.boolean_mask got Number of mask dimensions must be specified

Tensor shape become (?,) after >= 80. Which caused error on boolean_mask. Following code are to filter out those accuracy above 80%.

outputs, final_state = tf.contrib.rnn.static_rnn(cell, lstm_in, dtype=tf.float32,
                                                 initial_state = initial_state)
logits = tf.layers.dense(outputs[-1], n_classes, name='logits')
np_logits_b = tf.reduce_max(outputs[-1], axis=1) >= 80
labels_filtered = tf.boolean_mask(labels_, np_logits_b)

    <ipython-input-13-2615e31971df> in <module>()
     23     # Accuracy on >80%
     24     np_logits_b = tf.reduce_max(logits, axis=1) >= 80
---> 25     labels_filtered = tf.boolean_mask(labels_, np_logits_b)
     26     correct_pred_filtered = tf.equal(tf.argmax(logits_filtered, 1), tf.argmax(labels_filtered, 1))
     27     accuracy_filtered = tf.reduce_mean(tf.cast(correct_pred_filtered, tf.float32), name='accuracy_filtered')

~\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py in boolean_mask(tensor, mask, name)
   1119     if ndims_mask is None:
   1120       raise ValueError(
-> 1121           "Number of mask dimensions must be specified, even if some dimensions"
   1122           " are None.  E.g. shape=[None] is ok, but shape=None is not.")
   1123     shape_tensor[:ndims_mask].assert_is_compatible_with(shape_mask)

ValueError: Number of mask dimensions must be specified, even if some dimensions are None.  E.g. shape=[None] is ok, but shape=None is not.


print(tf.shape(np_logits_b))
Tensor("Shape_4:0", shape=(?,), dtype=int32)

Upvotes: 4

Views: 540

Answers (1)

huangbiubiu
huangbiubiu

Reputation: 1271

As the error says: Number of mask dimensions must be specified, the np_logits_b.shape must NOT be None.

Since TensorFlow can not infer the shape of Tensor np_logits_b, we can specify the shape manually before using the tf.boolean_mask:

np_logits_b = tf.reduce_max(outputs[-1], axis=1)
np_logits_b_shape = np_logits_b.shape  # remember the shape
np_logits_b = np_logits_b >= 80
np_logits_b.set_shape(np_logits_b_shape)  # set shape manually
labels_filtered = tf.boolean_mask(labels_, np_logits_b)

Upvotes: 1

Related Questions