Skip to content
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

Ubuntu Tensor indexer null exception #1220

Closed
yorkshireandrew opened this issue Dec 24, 2023 · 4 comments
Closed

Ubuntu Tensor indexer null exception #1220

yorkshireandrew opened this issue Dec 24, 2023 · 4 comments
Assignees

Comments

@yorkshireandrew
Copy link

Description

My system is:

Unix 5.15.0.88
64Bit Operating System: True
.NET CLR: 6.0.25
TensorFlow Binary v2.15.0-rc1
TensorFlow.NET v0.150.0.0
TensorFlow.Keras v0.15.0.0
Ubuntu 20.04.6 LTS

My code amounts to this:

using Tensorflow;
using Tensorflow.Keras.Metrics;
using static Tensorflow.Binding;

namespace TensorFlowNET.Examples
{
    class Program
    {
        static void Main(string[] args){
			var gtArray = new float[4]{1.0F, 3.0F, 1.0F, 3.0F};
			var gtTensor = new Tensor(gtArray);
			var a = gtTensor.ToArray<float>();
			var h1 = gtTensor[1];
		}
	}
}

While a produces the expected float array, h1 produces the exception below.

Exception has occurred: CLR/System.NullReferenceException
An unhandled exception of type 'System.NullReferenceException' occurred in Tensorflow.Binding.dll: 'Object reference not set to an instance of an object.'
at Tensorflow.Operation.op_Implicit(Operation op)
at Tensorflow.Tensor._as_tf_output()
at Tensorflow.ops.create_c_op(Graph graph, NodeDef node_def, Tensor[] inputs, Operation[] control_inputs, OpDef op_def)
at Tensorflow.Operation..ctor(NodeDef node_def, Graph g, Tensor[] inputs, TF_DataType[] output_types, ITensorOrOperation[] control_inputs, TF_DataType[] input_types, String original_op, OpDef op_def)
at Tensorflow.Graph.create_op(String op_type, Tensor[] inputs, TF_DataType[] dtypes, TF_DataType[] input_types, String name, Dictionary2 attrs, OpDef op_def, Boolean compute_device) at Tensorflow.OpDefLibrary._apply_op_helper(String op_type_name, String name, Dictionary2 keywords)
at Tensorflow.Contexts.Context.ExecGraphAction(String OpType, String Name, ExecuteOpArgs args)
at Tensorflow.Contexts.Context.ExecuteOp(String opType, String name, ExecuteOpArgs args)
at Tensorflow.array_ops.strided_slice(Tensor input
, Tensor begin, Tensor end, Tensor strides, Int32 begin_mask, Int32 end_mask, Int32 ellipsis_mask, Int32 new_axis_mask, Int32 shrink_axis_mask, String name)
at Tensorflow.Tensor.<>c__DisplayClass146_0.b__0(NameScope scope)
at Tensorflow.Binding.tf_with[TIn,TOut](TIn py, Func`2 action)
at Tensorflow.Tensor.slice(Int32 start)
at Tensorflow.Tensor.get_Item(Int32 idx)

Am I missing part of what is required to make TensorFlow.NET work. This for example gives me no results, but I don't think it is required is it? pip show tensorflow

Reproduction Steps

No response

Known Workarounds

No response

Configuration and Other Information

No response

@yorkshireandrew
Copy link
Author

Here is a breaking test file seems to error for
Release v0.150.0 based on tensorflowv v2.15.0.


using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tensorflow.NumPy;
using System.Linq;
using Tensorflow;
using static Tensorflow.Binding;

namespace TensorFlowNET.UnitTest.Basics
{	
    [TestClass]
    public class TensorTest
    {
		 [TestMethod]
        public void TensorIndexingTest()
        {
			var elements = new int[3]{1, 2 , 3};
			var tensor = new Tensor(elements);
			var firstElement = (int) tensor[0];
			var secondElement = (int) tensor[1];
                        Assert.AreEqual(1, firstElement);
			Assert.AreEqual(2, secondElement);
        }
    }
}

@Wanglongzhi2001
Copy link
Collaborator

Wanglongzhi2001 commented Dec 27, 2023

Hello, please create tensor from NDArray in your case. For more information about how to create a tensor, you can refer to the source code.
https://github.com/SciSharp/TensorFlow.NET/blob/master/src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs

@yorkshireandrew
Copy link
Author

I was writing unit tests around a method that takes a 1D Tensor then extracts each element from it (each one still as a Tensor) I think the code above fails because the Tensor has not yet been calculated in a graph.

If I make this replacement in the test above it then passes:

var tensor = tf.constant(elements);

@yorkshireandrew
Copy link
Author

@Wanglongzhi2001 your right constructing the Tensor from NDArray also works fine, thankyou.

	var raw_elements = new int[3]{1, 2 , 3};
        var elements = np.array(raw_elements);
        var tensor = new Tensor(elements);

I'll close the issue. What a great project. I'm finding using BARD rather than ChatGPT to help with coding works better as the code examples it generates are more up to date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants