{
  "id": 221406,
  "title": "Understanding nn.Identity",
  "url": "/competitions/ranzcr-clip-catheter-line-classification/discussion/221406",
  "author_name": "",
  "post_date": "2021-02-22T16:02:51.736284400Z",
  "votes": null,
  "comment_count": 2,
  "views": 0,
  "content": "<p>In the below code could any one explain the use of <strong>nn.Identity</strong></p>\n<p>`class ResNet200D(nn.Module):</p>\n<pre><code> def __init__(self, model_name='resnet200d_320'):\n    super().__init__()\n    self.model = timm.create_model(model_name, pretrained=False)\n    n_features = self.model.fc.in_features\n    self.model.global_pool = nn.Identity()\n    self.model.fc = nn.Identity()\n    self.pooling = nn.AdaptiveAvgPool2d(1)\n    self.fc = nn.Linear(n_features, 11)\n\ndef forward(self, x):\n    bs = x.size(0)\n    features = self.model(x)\n    pooled_features = self.pooling(features).view(bs, -1)\n    output = self.fc(pooled_features)\n    return output`\n</code></pre>",
  "messages": [
    {
      "id": "1214136",
      "postDate": "02/22/2021 16:02:51",
      "content": "<p>In the below code could any one explain the use of <strong>nn.Identity</strong></p>\n<p>`class ResNet200D(nn.Module):</p>\n<pre><code> def __init__(self, model_name='resnet200d_320'):\n    super().__init__()\n    self.model = timm.create_model(model_name, pretrained=False)\n    n_features = self.model.fc.in_features\n    self.model.global_pool = nn.Identity()\n    self.model.fc = nn.Identity()\n    self.pooling = nn.AdaptiveAvgPool2d(1)\n    self.fc = nn.Linear(n_features, 11)\n\ndef forward(self, x):\n    bs = x.size(0)\n    features = self.model(x)\n    pooled_features = self.pooling(features).view(bs, -1)\n    output = self.fc(pooled_features)\n    return output`\n</code></pre>",
      "rawMarkdown": "In the below code could any one explain the use of **nn.Identity**\n\n\n`class ResNet200D(nn.Module):\n    \n     def __init__(self, model_name='resnet200d_320'):\n        super().__init__()\n        self.model = timm.create_model(model_name, pretrained=False)\n        n_features = self.model.fc.in_features\n        self.model.global_pool = nn.Identity()\n        self.model.fc = nn.Identity()\n        self.pooling = nn.AdaptiveAvgPool2d(1)\n        self.fc = nn.Linear(n_features, 11)\n\n    def forward(self, x):\n        bs = x.size(0)\n        features = self.model(x)\n        pooled_features = self.pooling(features).view(bs, -1)\n        output = self.fc(pooled_features)\n        return output`",
      "votes": null
    },
    {
      "id": "1214376",
      "postDate": "02/22/2021 19:43:29",
      "content": "<p><code>nn.Identity</code> is basically an empty placeholder. It's used here to disable model built-in global pooling layer, in order to use custom defined pooling <code>self.pooling = nn.AdaptiveAvgPool2d(1)</code>. It's also used in a same way to redefine <code>self.fc</code> layer.</p>",
      "rawMarkdown": "`nn.Identity` is basically an empty placeholder. It's used here to disable model built-in global pooling layer, in order to use custom defined pooling `self.pooling = nn.AdaptiveAvgPool2d(1)`. It's also used in a same way to redefine `self.fc` layer.",
      "votes": null
    },
    {
      "id": "1216610",
      "postDate": "02/24/2021 11:12:57",
      "content": "<p>Identity : f(x) = x </p>",
      "rawMarkdown": "Identity : f(x) = x",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1214376,
      "author_name": "atamazian",
      "author_url": "",
      "post_date": "02/22/2021 19:43:29",
      "content": "<p><code>nn.Identity</code> is basically an empty placeholder. It's used here to disable model built-in global pooling layer, in order to use custom defined pooling <code>self.pooling = nn.AdaptiveAvgPool2d(1)</code>. It's also used in a same way to redefine <code>self.fc</code> layer.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1216610,
      "author_name": "analokamus",
      "author_url": "",
      "post_date": "02/24/2021 11:12:57",
      "content": "<p>Identity : f(x) = x </p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1214136": "In the below code could any one explain the use of **nn.Identity**\n\n\n`class ResNet200D(nn.Module):\n    \n     def __init__(self, model_name='resnet200d_320'):\n        super().__init__()\n        self.model = timm.create_model(model_name, pretrained=False)\n        n_features = self.model.fc.in_features\n        self.model.global_pool = nn.Identity()\n        self.model.fc = nn.Identity()\n        self.pooling = nn.AdaptiveAvgPool2d(1)\n        self.fc = nn.Linear(n_features, 11)\n\n    def forward(self, x):\n        bs = x.size(0)\n        features = self.model(x)\n        pooled_features = self.pooling(features).view(bs, -1)\n        output = self.fc(pooled_features)\n        return output`",
    "1214376": "`nn.Identity` is basically an empty placeholder. It's used here to disable model built-in global pooling layer, in order to use custom defined pooling `self.pooling = nn.AdaptiveAvgPool2d(1)`. It's also used in a same way to redefine `self.fc` layer.",
    "1216610": "Identity : f(x) = x"
  },
  "source": "meta"
}