SciSharp / TensorFlow.NET

.NET Standard bindings for Google's TensorFlow for developing, training and deploying Machine Learning models in C# and F#.
https://scisharp.github.io/tensorflow-net-docs
Apache License 2.0
3.17k stars 506 forks source link

[Question]: Shape must be rank 1 but is rank 0 #1203

Closed meAloex closed 8 months ago

meAloex commented 8 months ago

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

AsakusaRinne commented 8 months ago

@Wanglongzhi2001 Could you please take a look at this issue? It seems the codes are equivalent.

Wanglongzhi2001 commented 8 months ago

@Wanglongzhi2001 Could you please take a look at this issue? It seems the codes are equivalent.

Certainly.

meAloex commented 8 months ago

@Wanglongzhi2001 This python project uses tensorflow.compat.v1. I don't know how important it is, it seems like they are the same, but just in case

Wanglongzhi2001 commented 8 months ago

@Wanglongzhi2001 This python project uses tensorflow.compat.v1. I don't know how important it is, it seems like they are the same, but just in case

I'm sorry, it looks like there exists some problem with the implementation of the tf.boolean_mask in TensorFlow.NET, this test can not be passed, and it will throw the same wrong message with yours. And I will fix it. https://github.com/SciSharp/TensorFlow.NET/blob/079b9a334be2a81c511c6148cd2788f165bc7a6d/test/TensorFlowNET.Graph.UnitTest/Basics/TensorTest.cs#L63-L72

Wanglongzhi2001 commented 8 months ago

Hello, I have fixed this bug in #1205 . But for now, please use this API in eager mode.