【问题标题】:Why do I get "IndentationError: expected an indented block" [duplicate]为什么我会收到“IndentationError:预期有缩进块”[重复]
【发布时间】:2021-02-22 22:55:25
【问题描述】:

我正在为 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

【问题讨论】:

  • 即使是函数中的文档字符串也应该缩进。

标签: python python-3.x conv-neural-network indentation


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多