8000 [Question]: Shape must be rank 1 but is rank 0 · Issue #1203 · SciSharp/TensorFlow.NET · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
meAloex opened this issue Oct 27, 2023 · 5 comments · Fixed by #1205
Closed

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

meAloex opened this issue Oct 27, 2023 · 5 comments · Fixed by #1205
Labels
bug Something isn't working

Comments

@meAloex
Copy link
meAloex commented Oct 27, 2023

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

@meAloex meAloex changed the title [Question]: [Question]: Shape must be rank 1 but is rank 0 Oct 27, 2023
@AsakusaRinne
Copy link
Collaborator

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

@Wanglongzhi2001
Copy link
Collaborator

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

Certainly.

@meAloex
Copy link
Author
meAloex commented Oct 29, 2023

@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
Copy link
Collaborator
Wanglongzhi2001 commented Oct 29, 2023

@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.

[TestMethod, Ignore]
public void boolean_mask()
{
var tensor = new[] { 0, 1, 2, 3 };
var mask = np.array(new[] { true, false, true, false });
var masked = tf.boolean_mask(tensor, mask);
var sess = tf.Session();
var result = sess.run(masked);
Assert.IsTrue(Enumerable.SequenceEqual(new int[] { 0, 2 }, masked.ToArray<int>()));
}

8000

@Wanglongzhi2001 Wanglongzhi2001 added the bug Something isn't working label Oct 29, 2023
@Wanglongzhi2001
Copy link
Collaborator
Wanglongzhi2001 commented Nov 4, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
0