keras 属性错误:该图层从未被调用过,因此没有定义的输出形状

piztneat  于 2022-11-13  发布在  其他
关注(0)|答案(4)|浏览(167)

我正在尝试定义一个模型happyModel()

# GRADED FUNCTION: happyModel

def happyModel():
    """
    Implements the forward propagation for the binary classification model:
    ZEROPAD2D -> CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> FLATTEN -> DENSE

Note that for simplicity and grading purposes, you'll hard-code all the values
such as the stride and kernel (filter) sizes. 
Normally, functions should take these values as function parameters.

Arguments:
None

Returns:
model -- TF Keras model (object containing the information for the entire training process) 
"""
model = tf.keras.Sequential(
    [
        ## ZeroPadding2D with padding 3, input shape of 64 x 64 x 3
        tf.keras.layers.ZeroPadding2D(padding=(3,3), data_format=(64,64,3)),
    
        ## Conv2D with 32 7x7 filters and stride of 1            
        tf.keras.layers.Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0'),
        
        ## BatchNormalization for axis 3
        
        tf.keras.layers.BatchNormalization(axis = 3, name = 'bn0'),
        
        ## ReLU            
        tf.keras.layers.Activation('relu'),
        
        ## Max Pooling 2D with default parameters            
        tf.keras.layers.MaxPooling2D((2, 2), name='max_pool0'),
    
        ## Flatten layer            
        tf.keras.layers.Flatten(),
    
        ## Dense layer with 1 unit for output & 'sigmoid' activation            
        tf.keras.layers.Dense(1, activation='sigmoid', name='fc'),
        
        # YOUR CODE STARTS HERE
        
        
        # YOUR CODE ENDS HERE
    ]
)

return model

下面的代码用于创建上面定义的该模型的对象:

happy_model = happyModel()
# Print a summary for each layer
for layer in summary(happy_model):
    print(layer)
    
output = [['ZeroPadding2D', (None, 70, 70, 3), 0, ((3, 3), (3, 3))],
            ['Conv2D', (None, 64, 64, 32), 4736, 'valid', 'linear', 'GlorotUniform'],
            ['BatchNormalization', (None, 64, 64, 32), 128],
            ['ReLU', (None, 64, 64, 32), 0],
            ['MaxPooling2D', (None, 32, 32, 32), 0, (2, 2), (2, 2), 'valid'],
            ['Flatten', (None, 32768), 0],
            ['Dense', (None, 1), 32769, 'sigmoid']]
    
comparator(summary(happy_model), output)

出现以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-67-f33284fd82fe> in <module>
      1 happy_model = happyModel()
      2 # Print a summary for each layer
----> 3 for layer in summary(happy_model):
      4     print(layer)
      5 

~/work/release/W1A2/test_utils.py in summary(model)
     30     result = []
     31     for layer in model.layers:
---> 32         descriptors = [layer.__class__.__name__, layer.output_shape, layer.count_params()]
     33         if (type(layer) == Conv2D):
     34             descriptors.append(layer.padding)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in output_shape(self)
   2177     """
   2178     if not self._inbound_nodes:
-> 2179       raise AttributeError('The layer has never been called '
   2180                            'and thus has no defined output shape.')
   2181     all_output_shapes = set(

AttributeError: The layer has never been called and thus has no defined output shape.

我怀疑我对ZeroPadding2D()的调用不正确。该项目似乎要求ZeroPadding2D()的输入形状为64X64X3。我尝试了许多格式,但无法修复这个问题。有人能给予一个指针吗?非常感谢。

q8l4jmvw

q8l4jmvw1#

在您的模型定义中,以下图层存在问题:

tf.keras.layers.ZeroPadding2D(padding=(3,3), data_format=(64,64,3)),

首先,你还没有定义任何输入层,其中data_format是一个字符串,channels_last(缺省值)或channels_first之一,source.定义上述模型的正确方法如下:

def happyModel():
    model = tf.keras.Sequential(
        [
            ## ZeroPadding2D with padding 3, input shape of 64 x 64 x 3
            tf.keras.layers.ZeroPadding2D(padding=(3,3), 
                         input_shape=(64, 64, 3), data_format="channels_last"),
           ....
           ....

happy_model = happyModel()
happy_model.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
zero_padding2d_4 (ZeroPaddin (None, 70, 70, 3)         0         
_________________________________________________________________
conv0 (Conv2D)               (None, 64, 64, 32)        4736      
_________________________________________________________________
bn0 (BatchNormalization)     (None, 64, 64, 32)        128       
_________________________________________________________________
activation_2 (Activation)    (None, 64, 64, 32)        0         
_________________________________________________________________
max_pool0 (MaxPooling2D)     (None, 32, 32, 32)        0         
_________________________________________________________________
flatten_16 (Flatten)         (None, 32768)             0         
_________________________________________________________________
fc (Dense)                   (None, 1)                 32769     
=================================================================
Total params: 37,633
Trainable params: 37,569
Non-trainable params: 64
tzxcd3kk

tzxcd3kk2#

根据tf.keras.Sequential()(https://www.tensorflow.org/api_docs/python/tf/keras/Sequential)的文档:
或者,第一层可以接收input_shape参数
因此,如果要指定输入形状,则应使用tf.keras.layers.ZeroPadding2D(padding=(3,3), input_shape=(64,64,3)),而不是tf.keras.layers.ZeroPadding2D(padding=(3,3), data_format=(64,64,3))

pftdvrlh

pftdvrlh3#

model = tf.keras.Sequential([
            # YOUR CODE STARTS HERE
            tf.keras.layers.ZeroPadding2D(padding=(3, 3), input_shape=(64,64,3), data_format="channels_last"),
            tf.keras.layers.Conv2D(32, (7, 7), strides = (1, 1)),
            tf.keras.layers.BatchNormalization(axis=3),
            tf.keras.layers.ReLU(),
            tf.keras.layers.MaxPooling2D(),
            tf.keras.layers.Flatten(),
            tf.keras.layers.Dense(1, activation='sigmoid'),
            # YOUR CODE ENDS HERE
        ])
    
    return model

试试看它能不能正常工作......

d4so4syb

d4so4syb4#

model = tf.keras.Sequential(
[
    ## ZeroPadding2D with padding 3, input shape of 64 x 64 x 3
    ## Conv2D with 32 7x7 filters and stride of 1 
    ## BatchNormalization for axis 3
    ## ReLU            
    ## Max Pooling 2D with default parameters            
    ## Flatten layer            
    ## Dense layer with 1 unit for output & 'sigmoid' activation
    # YOUR CODE STARTS HERE
    
    tfl.ZeroPadding2D(padding=(3,3), input_shape=(64,64,3),data_format="channels_last"),
    tfl.Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0'),
    tfl.BatchNormalization(axis = 3, name = 'bn0'),
    tfl.ReLU(),
    tfl.MaxPooling2D((2, 2), name='max_pool0'),
    tfl.Flatten(),
    tfl.Dense(1, activation='sigmoid', name='fc'),
    
    # YOUR CODE ENDS HERE
])

你可以试试看。

相关问题