{
  "id": 398876,
  "title": "Leagrning GNN, how to define model using pytorch geometric?",
  "url": "/competitions/icecube-neutrinos-in-deep-ice/discussion/398876",
  "author_name": "",
  "post_date": "2023-04-01T07:44:46.695447Z",
  "votes": 9,
  "comment_count": 4,
  "views": 0,
  "content": "<p>Dear all, it is first time for me to use GNN. So to understand how it works, I am trying to create very simple own GNN using pytorch geometric with some of data from this competition. </p>\n<p>Now I prepare 10 graphs from the competition data like below.</p>\n<p>x: All sensors positions but masked if sensor does not appear in event.<br>\nedge_index: Use sensor ID and connecting based on time series.<br>\ny: simply use [azimuth, zenith]</p>\n<p>[Data(x=[5160, 3], edge_index=[2, 2967], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 36], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 58], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 998], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 32], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 55], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 62], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 86], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 66], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 36], y=[2])]</p>\n<p>Then define model like below and train it with above data. However the output of model has different shape with y 🤔</p>\n<blockquote>\n  <p>class GCN(torch.nn.Module):<br>\n      def <strong>init</strong>(self):<br>\n          super(GCN, self).<strong>init</strong>()<br>\n          self.conv1 = GCNConv(3, 16)<br>\n          self.conv2 = GCNConv(16, 32)<br>\n          self.conv3 = GCNConv(32, 2)</p>\n</blockquote>\n<pre><code>def forward(self, x, edge_index):\n    x = F.relu(self.conv1(x, edge_index))\n    x = F.relu(self.conv2(x, edge_index))\n    x = self.conv3(x, edge_index)\n    return x\n</code></pre>\n<p>Does anyone could give me an advice how should I modify it?<br>\nThe full code is in <a href=\"https://www.kaggle.com/code/hechtjp/learning-gnn-with-icecube-dataset/notebook\" target=\"_blank\">this notebook</a>.<br>\nThank you in advance!🙏</p>",
  "messages": [
    {
      "id": "2205045",
      "postDate": "04/01/2023 07:44:46",
      "content": "<p>Dear all, it is first time for me to use GNN. So to understand how it works, I am trying to create very simple own GNN using pytorch geometric with some of data from this competition. </p>\n<p>Now I prepare 10 graphs from the competition data like below.</p>\n<p>x: All sensors positions but masked if sensor does not appear in event.<br>\nedge_index: Use sensor ID and connecting based on time series.<br>\ny: simply use [azimuth, zenith]</p>\n<p>[Data(x=[5160, 3], edge_index=[2, 2967], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 36], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 58], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 998], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 32], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 55], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 62], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 86], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 66], y=[2]),<br>\n Data(x=[5160, 3], edge_index=[2, 36], y=[2])]</p>\n<p>Then define model like below and train it with above data. However the output of model has different shape with y 🤔</p>\n<blockquote>\n  <p>class GCN(torch.nn.Module):<br>\n      def <strong>init</strong>(self):<br>\n          super(GCN, self).<strong>init</strong>()<br>\n          self.conv1 = GCNConv(3, 16)<br>\n          self.conv2 = GCNConv(16, 32)<br>\n          self.conv3 = GCNConv(32, 2)</p>\n</blockquote>\n<pre><code>def forward(self, x, edge_index):\n    x = F.relu(self.conv1(x, edge_index))\n    x = F.relu(self.conv2(x, edge_index))\n    x = self.conv3(x, edge_index)\n    return x\n</code></pre>\n<p>Does anyone could give me an advice how should I modify it?<br>\nThe full code is in <a href=\"https://www.kaggle.com/code/hechtjp/learning-gnn-with-icecube-dataset/notebook\" target=\"_blank\">this notebook</a>.<br>\nThank you in advance!🙏</p>",
      "rawMarkdown": "Dear all, it is first time for me to use GNN. So to understand how it works, I am trying to create very simple own GNN using pytorch geometric with some of data from this competition. \n\nNow I prepare 10 graphs from the competition data like below.\n\nx: All sensors positions but masked if sensor does not appear in event.\nedge_index: Use sensor ID and connecting based on time series.\ny: simply use [azimuth, zenith]\n\n[Data(x=[5160, 3], edge_index=[2, 2967], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 36], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 58], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 998], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 32], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 55], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 62], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 86], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 66], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 36], y=[2])]\n\nThen define model like below and train it with above data. However the output of model has different shape with y 🤔\n\n>class GCN(torch.nn.Module):\n    def __init__(self):\n        super(GCN, self).__init__()\n        self.conv1 = GCNConv(3, 16)\n        self.conv2 = GCNConv(16, 32)\n        self.conv3 = GCNConv(32, 2)\n\n    def forward(self, x, edge_index):\n        x = F.relu(self.conv1(x, edge_index))\n        x = F.relu(self.conv2(x, edge_index))\n        x = self.conv3(x, edge_index)\n        return x\n\nDoes anyone could give me an advice how should I modify it?\nThe full code is in [this notebook](https://www.kaggle.com/code/hechtjp/learning-gnn-with-icecube-dataset/notebook).\nThank you in advance!🙏",
      "votes": null
    },
    {
      "id": "2205225",
      "postDate": "04/01/2023 10:58:34",
      "content": "<p>I am not good with coding GNN's but you can refer this playlist <a href=\"https://www.youtube.com/playlist?list=PLV8yxwGOxvvoNkzPfCx2i8an--Tkt7O8Z\" target=\"_blank\">https://www.youtube.com/playlist?list=PLV8yxwGOxvvoNkzPfCx2i8an--Tkt7O8Z</a></p>",
      "rawMarkdown": "I am not good with coding GNN's but you can refer this playlist https://www.youtube.com/playlist?list=PLV8yxwGOxvvoNkzPfCx2i8an--Tkt7O8Z",
      "votes": null
    },
    {
      "id": "2205237",
      "postDate": "04/01/2023 11:14:18",
      "content": "<p>Thanks <a href=\"https://www.kaggle.com/warcoder\" target=\"_blank\">@warcoder</a> for your comment! I am checking it 👍 </p>",
      "rawMarkdown": "Thanks @warcoder for your comment! I am checking it 👍",
      "votes": null
    },
    {
      "id": "2221286",
      "postDate": "04/14/2023 05:46:22",
      "content": "<p>One possible solution is to add a global pooling layer after the last convolution layer, such as global_mean_pool or global_max_pool, which will aggregate the node features into a single vector. Then you can use a linear layer or a fully connected layer to map that vector to the output dimension of 2. Something like this:</p>\n<pre><code> (torch.nn.Module): \n (): \n    (GCN, self).init() \n    self.conv1 = GCNConv(, ) \n    self.conv2 = GCNConv(, ) \n    self.conv3 = GCNConv(, ) \n    self.pool = global_mean_pool \n    self.fc = torch.nn.Linear(, )\n\n (): \n    x = F.relu(self.conv1(x, edge_index)) \n    x = F.relu(self.conv2(x, edge_index)) \n    x = F.relu(self.conv3(x, edge_index)) \n    x = self.pool(x, edge_index) \n    x = self.fc(x) \n     x\n</code></pre>",
      "rawMarkdown": "One possible solution is to add a global pooling layer after the last convolution layer, such as global_mean_pool or global_max_pool, which will aggregate the node features into a single vector. Then you can use a linear layer or a fully connected layer to map that vector to the output dimension of 2. Something like this:\n\n```python\nclass GCN(torch.nn.Module): \ndef init(self): \n    super(GCN, self).init() \n    self.conv1 = GCNConv(3, 16) \n    self.conv2 = GCNConv(16, 32) \n    self.conv3 = GCNConv(32, 64) \n    self.pool = global_mean_pool # or global_max_pool \n    self.fc = torch.nn.Linear(64, 2)\n\ndef forward(self, x, edge_index): \n    x = F.relu(self.conv1(x, edge_index)) \n    x = F.relu(self.conv2(x, edge_index)) \n    x = F.relu(self.conv3(x, edge_index)) \n    x = self.pool(x, edge_index) \n    x = self.fc(x) \n    return x\n```",
      "votes": null
    },
    {
      "id": "2221746",
      "postDate": "04/14/2023 14:35:31",
      "content": "<p><a href=\"https://www.kaggle.com/yus002\" target=\"_blank\">@yus002</a> , thanks for your advice! I will try it out 😉</p>",
      "rawMarkdown": "yus002 , thanks for your advice! I will try it out 😉",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2205225,
      "author_name": "warcoder",
      "author_url": "",
      "post_date": "04/01/2023 10:58:34",
      "content": "<p>I am not good with coding GNN's but you can refer this playlist <a href=\"https://www.youtube.com/playlist?list=PLV8yxwGOxvvoNkzPfCx2i8an--Tkt7O8Z\" target=\"_blank\">https://www.youtube.com/playlist?list=PLV8yxwGOxvvoNkzPfCx2i8an--Tkt7O8Z</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 2205237,
          "author_name": "hechtjp",
          "author_url": "",
          "post_date": "04/01/2023 11:14:18",
          "content": "<p>Thanks <a href=\"https://www.kaggle.com/warcoder\" target=\"_blank\">@warcoder</a> for your comment! I am checking it 👍 </p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 2221286,
      "author_name": "yus002",
      "author_url": "",
      "post_date": "04/14/2023 05:46:22",
      "content": "<p>One possible solution is to add a global pooling layer after the last convolution layer, such as global_mean_pool or global_max_pool, which will aggregate the node features into a single vector. Then you can use a linear layer or a fully connected layer to map that vector to the output dimension of 2. Something like this:</p>\n<pre><code> (torch.nn.Module): \n (): \n    (GCN, self).init() \n    self.conv1 = GCNConv(, ) \n    self.conv2 = GCNConv(, ) \n    self.conv3 = GCNConv(, ) \n    self.pool = global_mean_pool \n    self.fc = torch.nn.Linear(, )\n\n (): \n    x = F.relu(self.conv1(x, edge_index)) \n    x = F.relu(self.conv2(x, edge_index)) \n    x = F.relu(self.conv3(x, edge_index)) \n    x = self.pool(x, edge_index) \n    x = self.fc(x) \n     x\n</code></pre>",
      "votes": null,
      "replies": [
        {
          "id": 2221746,
          "author_name": "hechtjp",
          "author_url": "",
          "post_date": "04/14/2023 14:35:31",
          "content": "<p><a href=\"https://www.kaggle.com/yus002\" target=\"_blank\">@yus002</a> , thanks for your advice! I will try it out 😉</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2205045": "Dear all, it is first time for me to use GNN. So to understand how it works, I am trying to create very simple own GNN using pytorch geometric with some of data from this competition. \n\nNow I prepare 10 graphs from the competition data like below.\n\nx: All sensors positions but masked if sensor does not appear in event.\nedge_index: Use sensor ID and connecting based on time series.\ny: simply use [azimuth, zenith]\n\n[Data(x=[5160, 3], edge_index=[2, 2967], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 36], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 58], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 998], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 32], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 55], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 62], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 86], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 66], y=[2]),\n Data(x=[5160, 3], edge_index=[2, 36], y=[2])]\n\nThen define model like below and train it with above data. However the output of model has different shape with y 🤔\n\n>class GCN(torch.nn.Module):\n    def __init__(self):\n        super(GCN, self).__init__()\n        self.conv1 = GCNConv(3, 16)\n        self.conv2 = GCNConv(16, 32)\n        self.conv3 = GCNConv(32, 2)\n\n    def forward(self, x, edge_index):\n        x = F.relu(self.conv1(x, edge_index))\n        x = F.relu(self.conv2(x, edge_index))\n        x = self.conv3(x, edge_index)\n        return x\n\nDoes anyone could give me an advice how should I modify it?\nThe full code is in [this notebook](https://www.kaggle.com/code/hechtjp/learning-gnn-with-icecube-dataset/notebook).\nThank you in advance!🙏",
    "2205225": "I am not good with coding GNN's but you can refer this playlist https://www.youtube.com/playlist?list=PLV8yxwGOxvvoNkzPfCx2i8an--Tkt7O8Z",
    "2205237": "Thanks @warcoder for your comment! I am checking it 👍",
    "2221286": "One possible solution is to add a global pooling layer after the last convolution layer, such as global_mean_pool or global_max_pool, which will aggregate the node features into a single vector. Then you can use a linear layer or a fully connected layer to map that vector to the output dimension of 2. Something like this:\n\n```python\nclass GCN(torch.nn.Module): \ndef init(self): \n    super(GCN, self).init() \n    self.conv1 = GCNConv(3, 16) \n    self.conv2 = GCNConv(16, 32) \n    self.conv3 = GCNConv(32, 64) \n    self.pool = global_mean_pool # or global_max_pool \n    self.fc = torch.nn.Linear(64, 2)\n\ndef forward(self, x, edge_index): \n    x = F.relu(self.conv1(x, edge_index)) \n    x = F.relu(self.conv2(x, edge_index)) \n    x = F.relu(self.conv3(x, edge_index)) \n    x = self.pool(x, edge_index) \n    x = self.fc(x) \n    return x\n```",
    "2221746": "yus002 , thanks for your advice! I will try it out 😉"
  },
  "source": "meta"
}