Search code examples
pythontensorflowdeep-learningartificial-intelligence

ann_visualizer - AttributeError: The layer has never been called and thus has no defined input shape


I'm just getting started on deep-learning with tensorflow. I came across ann_visualizer and wanted to try it out. I followed the guide to install and use it. Ive also installed the graphviz package and is in my source folder.

import numpy as np
import pandas as pd
import tensorflow as tf
import keras as keras

data = pd.read_csv('DataSets/Churn_Modelling.csv')
x = data.iloc[: , 3:-1].values
y = data.iloc[: , -1].values

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
x[: , 2] = le.fit_transform(x[: , 2])

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[( 'encoder' , OneHotEncoder() , [1])] , remainder='passthrough')
x = ct.fit_transform(x)

from sklearn.model_selection import train_test_split
x_train , x_test , y_train , y_test = train_test_split( x,y , test_size=0.2 , random_state=0)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

from ann_visualizer.visualize import ann_viz

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(units=10 , activation='relu'))
model.add(tf.keras.layers.Dense(units=10 , activation='relu'))
model.add(tf.keras.layers.Dense(units=1 , activation='sigmoid'))

ann_viz(model)

When I execute the code, i get the following:

PS D:\ANN> & C:/Users/hamza/miniconda3/python.exe d:/ANN/ann_model.py
2020-09-26 21:54:07.455336: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-09-26 21:54:07.456078: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-09-26 21:54:09.320246: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library nvcuda.dll
2020-09-26 21:54:09.553112: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties: 
pciBusID: 0000:02:00.0 name: GeForce MX130 computeCapability: 5.0
coreClock: 1.189GHz coreCount: 3 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 37.33GiB/s
2020-09-26 21:54:09.554645: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-09-26 21:54:09.562721: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found
2020-09-26 21:54:09.564579: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2020-09-26 21:54:09.566905: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
2020-09-26 21:54:09.568410: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found
2020-09-26 21:54:09.578061: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found
2020-09-26 21:54:09.579892: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2020-09-26 21:54:09.581202: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1753] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2020-09-26 21:54:09.582994: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-09-26 21:54:09.595543: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1fad94fed90 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-09-26 21:54:09.596453: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-09-26 21:54:09.597665: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-09-26 21:54:09.598924: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263]
Traceback (most recent call last):
  File "d:/ANN/ann_model.py", line 39, in <module>
    ann_viz(model)
  File "C:\Users\hamza\miniconda3\lib\site-packages\ann_visualizer\visualize.py", line 42, in ann_viz
    input_layer = int(str(layer.input_shape).split(",")[1][1:-1]);
  File "C:\Users\hamza\miniconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 2126, in input_shape   
    raise AttributeError('The layer has never been called '
AttributeError: The layer has never been called and thus has no defined input shape.

I have looked everywhere and found no solution. I'm using VS Code with Anaconda python

Thank you!


Solution

  • Your Dense layer has 10 units, but the actual shape of the kernel (i.e. the weights matrix) is not determined until the input shape is determined. For example, if the inputs had shape (batch size, 20) the weights matrix would have shape (20, 10), so the Dense layer has 200 weight parameters. But until the first time the model is called, the number of parameters the layer has is still undetermined. So just call the model once to fix the problem.

    inputs = tf.convert_to_tensor(numpy.random.rand(1,20), dtype='float32')
    model(inputs)
    ann_viz(model)
    

    Of course since you haven't trained the model in your code snippet you're just going to be looking at randomly initialized weights.