Description
Description
Problem with the boolean_mask() method. Here is the error:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> Tensorflow.InvalidArgumentError: Shape must be rank 1 but is rank 0 for '{{node boolean_mask/concat}} = ConcatV2[N=3, T=DT_INT32, Tidx=DT_INT32](boolean_mask/strided_slice_1, boolean_mask/Prod, boolean_mask/strided_slice_2, boolean_mask/concat/axis)' with input shapes: [0], [], [1], [].
The code I'm trying to migrate from python to c#:
`
def get_type_fc_and_mask(self, input, unit_categoy_batch, unit_attack_mask, type_constant, scope, layer_size):
feature_bool_mask = tf.reshape(tf.math.equal(unit_categoy_batch, tf.constant(type_constant)), [-1])
feature_raw = tf.boolean_mask(input, feature_bool_mask) //Normal works in python
mask = tf.boolean_mask(unit_attack_mask, feature_bool_mask)
mask = tf.cast(mask, dtype=tf.int32)
feature_raw = self.fc_layer(feature_raw, layer_size, scope, ln=True, activation=tf.nn.relu)
return feature_raw, mask
`
The input receives:
feature_unit_all_feature_2 = Tensor("all_shared_part/feature_unit_global_tran_2/Relu:0", shape=(?, 100), dtype=float32)
Rank of feature_unit_all_feature_2: 2
unit_categoy_batch = Tensor("all_shared_part/Reshape:0", shape=(?, 1), dtype=int32)
type_constant = 4
My c# code:
public (Tensor, Tensor) get_type_fc_and_mask(Tensor input, Tensor unit_categoy_batch, Tensor unit_attack_mask, int type_constant, string scope, int layer_size)
{
var equal_tensor = tf.equal(unit_categoy_batch, tf.constant(type_constant));
var feature_bool_mask = tf.reshape(equal_tensor, new Shape(-1));
var feature_raw = tf.boolean_mask(input, feature_bool_mask); //Error
var mask = tf.boolean_mask(unit_attack_mask, feature_bool_mask);
mask = tf.cast(mask, dtype: tf.int32);
feature_raw = FC_layer(feature_raw, layer_size, scope, true, tf.keras.activations.Relu);
return (feature_raw, mask);
}
My logs:
input shape (None, 100) input rank 2
feature_bool_mask shape (None,) feature_bool_mask rank 1
I roughly understand what my problem is, but I don't know how to solve it correctly. I didn't have any errors in python with the same passed values. Did I understand correctly that due to the fact that I have a Tensor of rank 2, not 1. Yes, the error signals the presence of a Tensor of rank 0, but I don't seem to have such.
l will appreciate any help!
Alternatives
No response