How can I get class labels for every input feature?

username: Monjurul_Karim
cooked: <p>I have an array of features. I want to have output class labels for each of the features<br>
inside the array.<br>
The example features array is as below:<br>
features_arr = array([feature1], [feature2], [feature3])<br>
In this example each of the features inside the features_arr (eg. feature1,etc.) is of<br>
dimension (1x256). How can I get outputs in such a way that features_arr[0] belongs to<br>
a specific class. (eg. feature1 belongs to class 1, feature 2 belongs to other class).<br>
It should be mentioned that there are some spatial relationships among the features. Thats<br>
why I want to pass features_arr to a neural network to learn the spatial relationships<br>
to output the class labels for each of the features.<br>
output example = array(0,1,0)<br>
Thanks in advance.</p>

username: ptrblck
cooked: <p>If I understand your use case correctly, you would like to use sequential data where each timestep belongs to one specific class?<br>
If so, then you could indeed pass the input data as <code>[batch_size, seq_len, nb_features]</code> to a model (the actual shape depends on the used modules) and use a corresponding target in the shape <code>[batch_size, seq_len]</code>.<br>
Here is a small code snippet overfitting a random dataset in this shape:</p>
<pre><code class="lang-python">batch_size, seq_len, nb_features = 10, 20, 256
features_arr = torch.randn(batch_size, seq_len, nb_features)

nb_classes = 5
target = torch.randint(0, nb_classes, (batch_size, seq_len))

model = nn.Linear(nb_features, nb_classes)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
criterion = torch.nn.CrossEntropyLoss()

for epoch in range(1000):
    optimizer.zero_grad()
    output = model(features_arr)
    output = output.permute(0, 2, 1)
    loss = criterion(output, target)
    loss.backward()
    optimizer.step()
    print('epoch {}, loss {:.3f}'.format(epoch, loss.item()))
    
print('predictions {}\ntarget {}'.format(torch.argmax(output, dim=1), target))
</code></pre>

username: Monjurul_Karim
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  Thank you for you response.<br>
My use case is not exactly a sequential data.<br>
I have features corresponding to different detected objects in an image. I want to classify each of the detected objects based on the features and also want to take into account the spatial relationships of different objects to get the classification decision.<br>
I tried to explain my need in the following figure:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/c/bc0deae808ade879d81340a525cd5b0ca2715f39.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/bc0deae808ade879d81340a525cd5b0ca2715f39" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/c/bc0deae808ade879d81340a525cd5b0ca2715f39_2_690x208.jpeg" alt="image" data-base62-sha1="qPBA8aKKxhts1GaMrLgbTlhrz3r" width="690" height="208" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/c/bc0deae808ade879d81340a525cd5b0ca2715f39_2_690x208.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/b/c/bc0deae808ade879d81340a525cd5b0ca2715f39_2_1035x312.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/c/bc0deae808ade879d81340a525cd5b0ca2715f39.jpeg 2x" data-dominant-color="BDC2BB"><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">image</span><span class="informations">1352×408 107 KB</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>
<p>Again many thanks.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Monjurul_Karim" data-post="3" data-topic="147637">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/monjurul_karim/48/48003_2.png" class="avatar"> Monjurul_Karim:</div>
<blockquote>
<p>I have features corresponding to different detected objects in an image.</p>
</blockquote>
</aside>
<p>I think this use case would also work using my code snippet as the “sequence” would be the number of objects. Note however that my code snippet treats each object (or time step) independently, which doesn’t seem to fit:</p>
<aside class="quote no-group" data-username="Monjurul_Karim" data-post="3" data-topic="147637">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/monjurul_karim/48/48003_2.png" class="avatar"> Monjurul_Karim:</div>
<blockquote>
<p>and also want to take into account the spatial relationships of different objects to get the classification decision.</p>
</blockquote>
</aside>
<p>I don’t know what the best approach for this would be.</p>

username: Monjurul_Karim
cooked: <p>Thank you very much!</p>

username: Andrei_Cristea
cooked: <p>With regards to taking into account the spatial relationships of different objects, you could embed the center coordinates of each bounding box as additional channels in each corresponding feature vector, and then hope that you find a network design that can learn to handle these channels properly in conjunction with traditional feature channels.</p>

username: Monjurul_Karim
cooked: <p><a class="mention" href="/u/andrei_cristea">@Andrei_Cristea</a> Thank you for your suggestion. Intuitively, your suggested method could be a reasonable solution.</p>