question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

After tf.Tensor creation, its value can be changed by input TypedArray (not immutable)

See original GitHub issue

Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js): yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOS 11.2.3
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: N/A
  • TensorFlow.js installed from (npm or script link): <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@3.2.0"> </script>
  • TensorFlow.js version (use command below): 3.2.0
  • Browser version: Google Chrome | 89.0.4389.114
  • Tensorflow.js Converter Version: N/A

Describe the current behavior

const array = new Float32Array([1, 2, 3, 4]);
const tensor = tf.tensor(array, [2, 2]);
tensor.print();
// Tensor
//    [[1, 2],
//     [3, 4]]
array[0] = 2;
tensor.print();
// Tensor
//    [[2, 2],
//     [3, 4]]

Describe the expected behavior As the doc says, tf.Tensor should be immutable. So change array should not change the value of tensor. It is expected that:

const array = new Float32Array([1, 2, 3, 4]);
const tensor = tf.tensor(array, [2, 2]);
tensor.print();
// Tensor
//    [[1, 2],
//     [3, 4]]
array[0] = 2;
tensor.print();
// Tensor
//    [[1, 2],
//     [3, 4]]

Standalone code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem. If possible, please share a link to Colab/CodePen/any notebook.

Other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pyu10055commented, Apr 8, 2021

@huningxin Thank you for raising this issue. I agree, we should document this limitation properly.

0reactions
google-ml-butler[bot]commented, Apr 9, 2021

Are you satisfied with the resolution of your issue? Yes No

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does the Tensor's value change? - Stack Overflow
When x += 2, and a new ID is created for x, we lost the handle to the original 'x', which is equivalent...
Read more >
tfjs/tensor.ts at master · tensorflow/tfjs - GitHub
A WebGL accelerated JavaScript library for training and deploying ML models. - tfjs/tensor.ts at master · tensorflow/tfjs.
Read more >
Creates a tf.Tensor with the provided values, shape and dtype.
Can be nested array of numbers, or a flat array, or a TypedArray. shape ([number, number]) The shape of the tensor. If not...
Read more >
Everything You Need to Know About Tensors - KDnuggets
A tensor can be assigned value only once and cannot be updated. The tensors, like python numbers and strings, are immutable and can...
Read more >
TensorFlow Basics: Tensor, Shape, Type, Sessions & Operators
You begin with the creation of a tensor with one dimension, namely a scalar. To create a tensor, you can use tf.constant() as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found