Skip to content

tf.batch_matmul gives "Attempting to capture an EagerTensor without building a function" #755

Closed

Description

@LeeReid1

Hi,

I'm getting an exception with Tensorflow.Binding.tf.batch_matmul the first time, and only the first time, it is called.

Packages:
SciSharp.TensorFlow.Redist 2.4.1
TensorFlow.Keras 0.4.0
TensorFlow.NET 0.33.0

Consider the following example:

    NDArray ta = new NDArray(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new Shape(5, 2));
    NDArray tb = new NDArray(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new Shape(2, 5));
    Tensor result2 = tf.batch_matmul(ta, tb); // this will throw an exception

The exception thrown is:

"Attempting to capture an EagerTensor without building a function." | Tensorflow.RuntimeError

stacktrace:
   at Tensorflow.ops.convert_to_tensor(Object value, TF_DataType dtype, String name, Boolean as_ref, TF_DataType preferred_dtype, Context ctx)
   at Tensorflow.OpDefLibrary.<>c__DisplayClass1_0.<_apply_op_helper>b__0(NameScope scope)
   at Tensorflow.Binding.tf_with[TIn,TOut](TIn py, Func`2 action)
   at Tensorflow.OpDefLibrary._apply_op_helper(String op_type_name, String name, Dictionary`2 keywords)
   at Tensorflow.OpDefLibrary._apply_op_helper(String op_type_name, String name, Object args)
   at Tensorflow.gen_math_ops.batch_mat_mul(Tensor x, Tensor y, Boolean adj_x, Boolean adj_y, String name)
   at Tensorflow.tensorflow.batch_matmul(Tensor x, Tensor y)
   at VPG.TF.Test2.LinearRegression2() in C:\somepath\TF\Test2.cs:line 240
   at VPG.Program.Main(String[] args) in C:\somepath\Program.cs:line 14

Now, if I run it first in a try-catch statement, the second time it is called it works:

    NDArray ta = new NDArray(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new Shape(5, 2));
    NDArray tb = new NDArray(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new Shape(2, 5));
    try
    {
        Tensor result = tf.batch_matmul(ta, tb); // this will throw an exception
    }
    catch
    {

    }
    Tensor result2 = tf.batch_matmul(ta, tb); // this works as expected

I'm new to this library, but so far I don't have this issue with other method calls. For example, I can run tf.matmul without an exception here.

I thought perhaps I misunderstood how to set things up, but then I still get this error if it is run this within a using for a GradientTape, or after I run a different function (such as a simple multiplication or tf.matmul). Any help you can offer would be much appreciated.

Lee

Activity

  1. Oceania2018 commented on Feb 19, 2021

    @Oceania2018
    Member

    @LeeReidCSIRO Will be fixed for next release.
    image

  2. added a commit that references this issue on Feb 20, 2021
  3. LeeReid1 commented on Feb 23, 2021

    @LeeReid1
    ContributorAuthor

    Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions