0

I want to exchange Tensor a to a NumPy two-dimensional array for getting a heat map.

(Tensor(1, 64, 64, 1) -> numpy (64, 64))

I tried x.numpy(), but it doesn't work. The error message is this:

AttributeError: in user code: :187 call * x_np=x.numpy() /usr/local/lib/python3.7/dist packages/tensorflow/python/framework/ops.py:401 getattr self.getattribute(name) AttributeError: 'Tensor' object has no attribute 'numpy'

This is the tensor information and type of the tensor:

Tensor("ExpandDims:0", shape=(1, 64, 64, 1), dtype=float32)
<class 'tensorflow.python.framework.ops.Tensor'>

and I don't know what ExpandDims:0 means (what is this argv?).

Tensorflow:2.6.0
Numpy:1.21.2

I made the test code that has the same Tensor type, shape, and env, and it works.

What should I do?

import tensorflow as tf
import numpy
print(tf.__version__)
print(numpy.__version__)

a = tf.constant([[[[0.1]*1]*64]*64])
print(a)
print(a.numpy())
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
seawavve
  • 41
  • 4
  • Does this answer your question? [AttributeError: 'Tensor' object has no attribute 'numpy'](https://stackoverflow.com/questions/52357542/attributeerror-tensor-object-has-no-attribute-numpy) – ashraful16 Sep 08 '21 at 05:06
  • Did you try updating/ reinstalling latest `tensorflow`? – Teshan Shanuka J Sep 08 '21 at 05:06
  • @Teshan Shanuka J yes but it didnt work... I run in Colab so you don't have to worry about library obsolescence – seawavve Sep 08 '21 at 05:15
  • 1
    @ashraful16 i tried all of those solutons.. but it does not work and get the same error. `AttributeError: 'Tensor' object has no attribute 'numpy'` – seawavve Sep 08 '21 at 05:22
  • can you send code? how create tensor array? – I'mahdi Sep 08 '21 at 05:22
  • send here line that create tensor i can help you – I'mahdi Sep 08 '21 at 05:27
  • @user1740577 this is the RepNet tutorial code and I want to get self-similarity matrix. The link is https://colab.research.google.com/github/google-research/google-research/blob/master/repnet/repnet_colab.ipynb#scrollTo=Y0iMnWR_nk9E. In SetUp's first block, i want to get numpy array of this x. `x = get_sims(x, self.temperature) ` – seawavve Sep 08 '21 at 05:28
  • why are you send all code? I say give me example like this : (Tensor(1,64,64,1)) then I can change this to `numpy(64,64)` – I'mahdi Sep 08 '21 at 05:39
  • @user1740577 hmm I made the test code that has the same Tensor type,shape, and env. and it works..... idk what to do.. can you advise? – seawavve Sep 08 '21 at 05:56
  • now you learn never to run all code and first split code to small pieces. have good time. do you have any question? – I'mahdi Sep 08 '21 at 06:12
  • 1
    I solved!! It wwas the problem of declaration. I declared twice like 'import tensorflow.compat.v2 as tf \n import tensorflow as tf ' They conflicted and i solved by changing ` import tensorflow as tf` to `import tensorflow as originTF` !! – seawavve Sep 08 '21 at 06:14
  • please send your answer to your own question. then the future users can learn from you :)) – I'mahdi Sep 08 '21 at 06:17

0 Answers0