ann_visualizer – AttributeError: 层从未被调用,因此没有定义的输入形状

我刚开始学习使用TensorFlow进行深度学习。我发现了ann_visualizer,想尝试使用它。我按照指南安装并使用了它。我还安装了graphviz包,并且它位于我的源文件夹中。

import numpy as npimport pandas as pdimport tensorflow as tfimport keras as kerasdata = pd.read_csv('DataSets/Churn_Modelling.csv')x = data.iloc[: , 3:-1].valuesy = data.iloc[: , -1].valuesfrom sklearn.preprocessing import LabelEncoderle = LabelEncoder()x[: , 2] = le.fit_transform(x[: , 2])from sklearn.compose import ColumnTransformerfrom sklearn.preprocessing import OneHotEncoderct = ColumnTransformer(transformers=[( 'encoder' , OneHotEncoder() , [1])] , remainder='passthrough')x = ct.fit_transform(x)from sklearn.model_selection import train_test_splitx_train , x_test , y_train , y_test = train_test_split( x,y , test_size=0.2 , random_state=0)from sklearn.preprocessing import StandardScalersc = StandardScaler()x_train = sc.fit_transform(x_train)x_test = sc.transform(x_test)from ann_visualizer.visualize import ann_vizmodel = 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.py2020-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 found2020-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.dll2020-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.0coreClock: 1.189GHz coreCount: 3 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 37.33GiB/s2020-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 found2020-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 found2020-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 found2020-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 found2020-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 found2020-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 found2020-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 found2020-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:  AVX2To 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 Version2020-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

谢谢你!


回答:

你的Dense层有10个单元,但实际的内核形状(即权重矩阵)在输入形状确定之前是不会确定的。例如,如果输入的形状是(批量大小,20),那么权重矩阵的形状将是(20,10),因此Dense层将有200个权重参数。但在模型首次被调用之前,层的参数数量仍然是不确定的。所以只需调用模型一次即可解决这个问题。

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

当然,由于你没有在代码片段中训练模型,你只是在查看随机初始化的权重。

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注