How to use the size of the input of GNN layers for in_channels?

CTY: <p>I am trying to use TransformerConv to train a model which takes in graphs of different sizes. This is to be used on a graph classification task. My code snippet is as follows:</p>
<pre><code class="lang-auto">self.conv1 = TransformerConv(in_channels=-1, out_channels=256)

def forward(self, x_onein, x_twoin, lambd):
        x_one, edge_index_one, batch_one = x_onein.x, x_onein.edge_index, x_onein.batch
        x_one = self.conv1(x_one, edge_index_one)
        return x_one
</code></pre>
<p>My input data come in batches of various sizes, and each graph has between tens of nodes to 1200 nodes. Because of this, I require the first input layer to be able to take in graphs of different sizes.<br>
When I run the code, I get the error:</p>
<pre><code class="lang-auto">RuntimeError: Trying to create tensor with negative dimension -1: [256, -1]
</code></pre>
<p>This is strange since I followed the documentation:<br>
<strong>in_channels</strong> (<a href="https://docs.python.org/3/library/functions.html#int" rel="noopener nofollow ugc"><em>int</em></a> <em>or</em> <a href="https://docs.python.org/3/library/stdtypes.html#tuple" rel="noopener nofollow ugc"><em>tuple</em></a>) – Size of each input sample, or <code>-1</code> to derive the size from the first input(s) to the forward method. A tuple corresponds to the sizes of source and target dimensionalities.</p>
<p>Any advice on this matter would be greatly appreciated.</p>

ptrblck: <p>I can’t reproduce the issue using:</p>
<pre><code class="lang-python">x1 = torch.randn(4, 8)
x2 = torch.randn(2, 16)
edge_index = torch.tensor([[0, 1, 2, 3], [0, 0, 1, 1]])  
conv = TransformerConv(in_channels=-1, out_channels=256)
out = conv(x1, edge_index)
</code></pre>
<p>so maybe your <code>torch_geometric</code> version is too old to support the lazy initialization of the layers (I don’t know when it was introduced)?</p>

CTY: <p>Thank you for the prompt reply.</p>
<p>My Pytorch version is 1.7.1 and my Pytorch geometric version is 1.6.3. I shall try to update them to the latest versions and try again.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/f/ffeee1ddf87037f94dd158f88fd9ced464f4be34.png" data-download-href="https://discuss.pytorch.org/uploads/default/ffeee1ddf87037f94dd158f88fd9ced464f4be34" title="BE01A2C4A5D44938A5AAFFA26E1EF9F4.png"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/f/ffeee1ddf87037f94dd158f88fd9ced464f4be34.png" alt="BE01A2C4A5D44938A5AAFFA26E1EF9F4.png" data-base62-sha1="Aw5yADkMqeQJMVwtQal9QXf5x3e" width="690" height="0" data-dominant-color="BFCDDB"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">BE01A2C4A5D44938A5AAFFA26E1EF9F4.png</span><span class="informations">708×1 83 Bytes</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>I’ve executed my code snippet with PyTorch <code>1.10.1</code> and Geometric <code>2.0.3</code>, so let’s see if updating helps. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=10" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

CTY: <p>Thank you for the help. Updating did help and I am able to run the program my desired way now. Lazy inputs seem to be a more recent functionality.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/f/ffeee1ddf87037f94dd158f88fd9ced464f4be34.png" data-download-href="https://discuss.pytorch.org/uploads/default/ffeee1ddf87037f94dd158f88fd9ced464f4be34" title="BE01A2C4A5D44938A5AAFFA26E1EF9F4.png"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/f/ffeee1ddf87037f94dd158f88fd9ced464f4be34.png" alt="BE01A2C4A5D44938A5AAFFA26E1EF9F4.png" data-base62-sha1="Aw5yADkMqeQJMVwtQal9QXf5x3e" width="690" height="0" data-dominant-color="BFCDDB"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">BE01A2C4A5D44938A5AAFFA26E1EF9F4.png</span><span class="informations">708×1 83 Bytes</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>