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

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

我刚刚开始使用 tensorflow 进行深度学习。我遇到了 ann_visualizer 并想尝试一下。我按照指南安装和使用它。我还安装了 graphviz 包,它在我的源文件夹中。

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)

当我执行代码时,我得到以下信息:

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.

我到处都找遍了,没有找到解决办法。 我在 Anaconda 中使用 VS Code python

谢谢!

您的 Dense 层有 10 个单元,但内核的实际形状(即权重矩阵)只有在输入形状确定后才能确定。例如,如果输入的形状为 (batch size, 20),则权重矩阵的形状为 (20, 10),因此 Dense 层有 200 个权重参数。但是直到第一次调用模型时,该层具有的参数数量仍未确定。所以只需调用模型一次即可解决问题。

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

当然,由于您尚未在代码片段中训练模型,因此您将只查看随机初始化的权重。