title: IndexError: The shape of the mask [...] at index 0 does not match the shape of the indexed tensor [...] at index 0
body: <p>I am trying to use Torch for Label Propagation.
  I have a dataframe that looks like</p>
  <pre><code>ID   Target   Weight   Label
  1      12       0.4      1
  2      24       0.1      0
  4      13       0.5      1
  4      12       0.3      1
  12     1        0.1      1
  12     4        0.4      1
  13     4        0.2      1
  17     1        0.1      0
  </code></pre>
  <p>and so on.</p>
  <p>I built the network as follows:</p>
  <pre><code>G = nx.from_pandas_edgelist(df, source='ID', target='Target', edge_attr=['Weight']) 
  </code></pre>
  <p>and the adjacency matrix</p>
  <pre><code>adj_matrix = nx.adjacency_matrix(G).toarray()
  </code></pre>
  <p>I have two labels only, 0 and 1, and some data unlabelled. I created input tensors as follows:</p>
  <pre><code># Create input tensors
  adj_matrix_t = torch.FloatTensor(adj_matrix)
  labels_t = torch.LongTensor(df['Labels'].tolist())
  </code></pre>
  <p>Trying to run the following code</p>
  <pre><code># Learn with Label Propagation
  label_propagation = LabelPropagation(adj_matrix_t)
  label_propagation.fit(labels_t) # this is causing the error
  </code></pre>
  <p>I have got the error: <code>IndexError: The shape of the mask [196] at index 0 does not match the shape of the indexed tensor [207] at index 0</code>.
  I checked the size size of <code>adj_matrix_t.shape</code> which is currently (207,207), while labels are 196.
  Do you know how I can fix this inconsistency?</p>
  <p>Please see below the error track:</p>
  <pre><code>---------------------------------------------------------------------------
  IndexError                                Traceback (most recent call last)
  &lt;ipython-input-42-cf4f88a4bb12&gt; in &lt;module&gt;
        2 label_propagation = LabelPropagation(adj_matrix_t)
        3 print(&quot;Label Propagation: &quot;, end=&quot;&quot;)
  ----&gt; 4 label_propagation.fit(labels_t)
        5 label_propagation_output_labels = label_propagation.predict_classes()
        6 
  
  &lt;ipython-input-1-54a7dbc30bd1&gt; in fit(self, labels, max_iter, tol)
      100 
      101     def fit(self, labels, max_iter=1000, tol=1e-3):
  --&gt; 102         super().fit(labels, max_iter, tol)
      103 
      104 ## Label spreading
  
  &lt;ipython-input-1-54a7dbc30bd1&gt; in fit(self, labels, max_iter, tol)
       58             Convergence tolerance: threshold to consider the system at steady state.
       59         &quot;&quot;&quot;
  ---&gt; 60         self._one_hot_encode(labels)
       61 
       62         self.predictions = self.one_hot_labels.clone()
  
  &lt;ipython-input-1-54a7dbc30bd1&gt; in _one_hot_encode(self, labels)
       43         self.one_hot_labels = torch.zeros((self.n_nodes, self.n_classes), dtype=torch.float)
       44         self.one_hot_labels = self.one_hot_labels.scatter(1, labels.unsqueeze(1), 1)
  ---&gt; 45         self.one_hot_labels[unlabeled_mask, 0] = 0
       46 
       47         self.labeled_mask = ~unlabeled_mask
  </code></pre>
  <p>The below code is an example of what I would like to use for label propagation. It seems that the error is due to labels. There are nodes in my dataset not having labels (though in my example above I wrote for all the labels). Might it be the case that this is causing the error message?</p>
  <p>Original code (for reference: <a href="https://mybinder.org/v2/gh/thibaudmartinez/label-propagation/master?filepath=notebook.ipynb" rel="nofollow noreferrer">https://mybinder.org/v2/gh/thibaudmartinez/label-propagation/master?filepath=notebook.ipynb</a>):</p>
  <pre><code>## Testing models on synthetic data
  
  import pandas as pd
  import numpy as np
  import networkx as nx
  import matplotlib.pyplot as plt
  
  # Create caveman graph
  n_cliques = 4
  size_cliques = 5
  caveman_graph = nx.connected_caveman_graph(n_cliques, size_cliques)
  adj_matrix = nx.adjacency_matrix(caveman_graph).toarray()
  
  
  # Create labels
  labels = np.full(n_cliques * size_cliques, -1.)
  
  # Only one node per clique is labeled. Each clique belongs to a different class.
  labels[0] = 0
  labels[size_cliques] = 1
  labels[size_cliques * 2] = 2
  labels[size_cliques * 3] = 3
  
  # Create input tensors
  adj_matrix_t = torch.FloatTensor(adj_matrix)
  labels_t = torch.LongTensor(labels)
  
  # Learn with Label Propagation
  label_propagation = LabelPropagation(adj_matrix_t)
  print(&quot;Label Propagation: &quot;, end=&quot;&quot;)
  label_propagation.fit(labels_t)
  label_propagation_output_labels = label_propagation.predict_classes()
  
  # Learn with Label Spreading
  label_spreading = LabelSpreading(adj_matrix_t)
  print(&quot;Label Spreading: &quot;, end=&quot;&quot;)
  label_spreading.fit(labels_t, alpha=0.8)
  label_spreading_output_labels = label_spreading.predict_classes()
  
  # Plot graphs
  color_map = {-1: &quot;orange&quot;, 0: &quot;blue&quot;, 1: &quot;green&quot;, 2: &quot;red&quot;, 3: &quot;cyan&quot;}
  input_labels_colors = [color_map[l] for l in labels]
  lprop_labels_colors = [color_map[l] for l in label_propagation_output_labels.numpy()]
  lspread_labels_colors = [color_map[l] for l in label_spreading_output_labels.numpy()]
  
  plt.figure(figsize=(14, 6))
  ax1 = plt.subplot(1, 4, 1)
  ax2 = plt.subplot(1, 4, 2)
  ax3 = plt.subplot(1, 4, 3)
  
  ax1.title.set_text(&quot;Raw data (4 classes)&quot;)
  ax2.title.set_text(&quot;Label Propagation&quot;)
  ax3.title.set_text(&quot;Label Spreading&quot;)
  
  pos = nx.spring_layout(G)
  nx.draw(G, ax=ax1, pos=pos, node_color=input_labels_colors, node_size=50)
  nx.draw(G, ax=ax2, pos=pos, node_color=lprop_labels_colors, node_size=50)
  nx.draw(G, ax=ax3, pos=pos, node_color=lspread_labels_colors, node_size=50)
  
  # Legend
  ax4 = plt.subplot(1, 4, 4)
  ax4.axis(&quot;off&quot;)
  legend_colors = [&quot;orange&quot;, &quot;blue&quot;, &quot;green&quot;, &quot;red&quot;, &quot;cyan&quot;]
  legend_labels = [&quot;unlabeled&quot;, &quot;class 0&quot;, &quot;class 1&quot;, &quot;class 2&quot;, &quot;class 3&quot;]
  dummy_legend = [ax4.plot([], [], ls='-', c=c)[0] for c in legend_colors]
  plt.legend(dummy_legend, legend_labels)
  
  plt.show()
  </code></pre>
  <p>Of course, if my example of dataset at the top of this post should not suit the original code because of the labels, if you could give me another example in order to understand how the labels (which determine the classes of nodes) in dataset should look like (even with missing values to be predicted), it would greatly appreciated it.</p>