Forum

Notifications
Clear all

TypeError: Tensor is unhashable. Instead, use tensor.ref() as the key.  

   RSS

0

I encountered the above mentioned error while building my CNN model and trying to add my output layer. The line that resulted in the error is in red. Why am I getting this error? Copying and pasting the code from the Google Colab workbook also gave me the exact same error.

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

 in ()
      5     Dense(128, activation='relu'),
      6     Dropout(0.2),
----> 7     Dense(10, activation = 'softmax')
      8 
      9     ])


13 frames


/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in __hash__(self)
    724 
    725     *N.B.* Before invoking `Tensor.eval()`, its graph must have been
--> 726     launched in a session, and either a default session must be
    727     available, or `session` must be specified explicitly.
    728 


TypeError: Tensor is unhashable. Instead, use tensor.ref() as the key.





# Install TensorFlow
!pip install -q tensorflow-gpu==2.0.0-beta1
import tensorflow as tf
print(tf.__version__)

# Additional imports
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Input, Conv2D, Dense, Flatten, Dropout
from tensorflow.keras.models import Model



####excluded other code that I assumed did not contribute to the error###


modelCNN = Sequential(

[Conv2D(32, activation='relu', kernel_size=(3, 3), input_shape=(reshaped_x_train.shape[1], reshaped_x_train.shape[2], reshaped_x_train.shape[3])),

Flatten(),

Dense(128, activation='relu'),

Dropout(0.2),

Dense(10, activation = 'softmax')




])


3 Answers
0

Which module or quiz is this issue about? Any kind of reference would be useful in assisting you.

Hi @siowy,
This was from reading the Google Colab code walkthrough on the MNIST dataset given in DPL-4: https://drive.google.com/file/d/10vw2c099P6AOHnIBwExRJBicOVQKJwTU/view

I wanted to build the model from scratch after reading this to better understand the code.

0

Hi @siowy,

Not sure why my reply to you was not posted. Here it is again.

This was from reading the Google Colab code walkthrough on the MNIST dataset given in DPL-4: https://drive.google.com/file/d/10vw2c099P6AOHnIBwExRJBicOVQKJwTU/view
/a>

I wanted to build the model from scratch after reading this to better understand the code. 

 

@audiophilic
I see. I'm looking at this and will try to get back to you on it.

@audiophilic
It seems like your libraries could be outdated. Have you tried updating your libraries? If that doesn't work, you can follow another tutorial and build a CNN from scratch following the tutorial and modify it from there.

0

@siowy

Thanks, I'll give that a try

Share:

Delete your account