微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ann_visualizer-AttributeError:该图层从未调用过,因此没有定义的输入形状

如何解决ann_visualizer-AttributeError:该图层从未调用过,因此没有定义的输入形状

我刚刚开始使用tensorflow进行深度学习。我遇到了an_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.

我无所不在,没有找到解决方法。 我正在将VS Code与Anaconda python配合使用

谢谢!

解决方法

您的密集层有10个单位,但是直到确定输入形状后才确定内核的实际形状(即权重矩阵)。例如,如果输入具有形状(批大小20),则权重矩阵将具有形状(20、10),因此Dense层具有200个权重参数。但是直到第一次调用该模型之前,该层所具有的参数数量仍不确定。因此,只需调用模型一次即可解决问题。

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。