gpt4 book ai didi

python - 为什么我会收到此图表已断开连接的错误?

转载 作者:行者123 更新时间:2023-12-03 02:21:05 25 4
gpt4 key购买 nike

我正在尝试创建一个密集网络,但是当我尝试编译模型时,我收到此错误消息。这是我的模型:

from tensorflow import keras
from keras.utils import plot_model
dropoutRate = 0.2

def globalAvgPooling(x):
height = np.shape(x)[2]
width = np.shape(x)[1]
poolSize = [width, height]
return tf.keras.layers.AveragePooling2D(pool_size=poolSize, strides=1)(x)

def concatenation(layers):
return tf.keras.layers.concatenate(layers, axis=3)

class DenseNet():
def __init__(self, filters, numBlocks, numClasses, training):
self.filters = filters
self.numBlocks = numBlocks
self.training = training
self.numClasses = numClasses
self.model = self.denseNet()

def bottleneckLayer(self, inputX):
x = tf.keras.layers.BatchNormalization()(inputs=inputX, training=self.training)
x = tf.keras.activations.relu(x)
x = tf.keras.layers.Conv2D(use_bias=False, filters=self.filters, kernel_size=1, strides=1, padding='same')(x)
x = tf.layers.dropout(inputs=x, rate=dropoutRate, training=self.training)
x = tf.keras.layers.BatchNormalization()(inputs=inputX, training=self.training)
x = tf.keras.activations.relu(x)
x = tf.keras.layers.Conv2D(use_bias=False, filters=self.filters, kernel_size=3, strides=1, padding='same')(x)
x = tf.layers.dropout(inputs=x, rate=dropoutRate, training=self.training)
return x


def denseBlock(self, inputX, numLayers):
concatLayers = list()
concatLayers.append(inputX)
x = self.bottleneckLayer(inputX=inputX)
concatLayers.append(x)
for i in range(self.numBlocks - 1):
x = concatenation(concatLayers)
x = self.bottleneckLayer(inputX=x)
concatLayers.append(x)

x = concatenation(concatLayers)
return x


def transitionLayer(self, inputX):
x = tf.keras.layers.BatchNormalization()(inputs=inputX, training=self.training)
x = tf.keras.activations.relu(x)
x = tf.keras.layers.Conv2D(use_bias=False, filters=self.filters, kernel_size=1, strides=1, padding='same')(x)
x = tf.layers.dropout(inputs=x, rate=dropoutRate, training=self.training)
x = tf.keras.layers.AveragePooling2D(pool_size=[2,2], strides=2, padding='valid')(x)
return x

def denseNet(self):
inputs = keras.Input(shape=(32,32,3))
x = tf.keras.layers.Conv2D(use_bias=False, filters=self.filters, kernel_size=7, strides=1, padding='same')(inputs)
x = self.denseBlock(inputX=x, numLayers=6) #Dense block 1 with 6 layers
x = self.transitionLayer(inputX=x)
x = self.denseBlock(inputX=x, numLayers=12) #Dense block 2 with 12 layers
x = self.transitionLayer(inputX=x)
x = self.denseBlock(inputX=x, numLayers= 48) #Dense block 3 with 48 layers
x = self.transitionLayer(inputX=x)
x = self.denseBlock(inputX=x, numLayers=32) #Dense block 4 with 32 layers (final block)
x = globalAvgPooling(x=x)
x = tf.keras.layers.Softmax()(x)
outputs = tf.keras.layers.Dense(units=self.numClasses)(x)
model = keras.Model(inputs=inputs, outputs=outputs)
return x
tf.compat.v1.disable_eager_execution()
growthK = 24
numBlock = 2

cameraModel = DenseNet(filters=growthK, numBlocks=numBlock, numClasses=4, training=True).model

这是我收到的错误消息:

ValueError: Graph disconnected: cannot obtain value for tensor 
Tensor("dropout_109/dropout/mul_1:0", shape=(?, 32, 32, 24), dtype=float32)
at layer "concatenate_48". The following previous layers were accessed without issue:
['input_6', 'conv2d_120', 'batch_normalization_115', 'tf_op_layer_Relu_115', 'conv2d_122', 'dropout_108']

我做错了什么?

最佳答案

错误来自以下行:

x = tf.keras.layers.BatchNormalization()(inputs=inputX, training=self.training)

因为我认为你有一个错误的inputs参数。它应该看起来像:

x = tf.keras.layers.BatchNormalization()(inputs=x, training=self.training)

这就是为什么重视断开连接的图表。

关于python - 为什么我会收到此图表已断开连接的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60004482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com