首页 > 解决方案 > 为什么我会收到“IndentationError:预期有缩进块”

问题描述

我为 CNN 定义了一个类,如下所示。然后我执行代码并获取IndentationError: expected an indented block. 你能详细说明我哪里错了吗?

class Lenet_like:
    """
    Lenet like architecture.
    """
    def __init__(self, width, depth, drop, n_classes):
    """
    Architecture settings.

    Arguments:
      - width: int, first layer number of convolution filters.
      - depth: int, number of convolution layer in the network.
      - drop: float, dropout rate.
      - n_classes: int, number of classes in the dataset.
    """
        self.width = width
        self.depth = depth
        self.drop = drop
        self.n_classes = n_classes

    def __call__(self, X):
    """
    Call classifier layers on the inputs.
    """

        for k in range(self.depth):
            # Apply successive convolutions to the input !
            # Use the functional API to do so
            X = Conv2D(filters = self.width / (2 ** k), activation = 'relu')(X)
            X = MaxPooling2D(strides = (2, 2))(X)
            X = Dropout(self.drop)(X)

        # Perceptron
        # This is the classification head of the classifier
        X = Flatten(X)
        Y = Dense(units = self.n_classes, activation = 'softmax')(X)

        return Y

错误信息:

  File "<ipython-input-1-b8f76520d2cf>", line 16
    """
    ^
IndentationError: expected an indented block

标签: pythonpython-3.xconv-neural-networkindentation

解决方案


从解析器的角度来看,文档字符串不是注释。这是一个普通的表达式语句,因此必须像def语句体的任何其他部分一样缩进。


推荐阅读