Boolean array expected for the condition, not float

Rexedoziem: <p>I got this error when dealing with multiple targets in multioutput classification problem.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/b/1bd366a2ef312613f8379e940af7b6b746e84d50.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/1bd366a2ef312613f8379e940af7b6b746e84d50" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/b/1bd366a2ef312613f8379e940af7b6b746e84d50_2_375x500.jpeg" alt="image" data-base62-sha1="3Y9OB3Dea9GDDLrEgLDmGYKzoBO" width="375" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/b/1bd366a2ef312613f8379e940af7b6b746e84d50_2_375x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/1/b/1bd366a2ef312613f8379e940af7b6b746e84d50_2_562x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/b/1bd366a2ef312613f8379e940af7b6b746e84d50_2_750x1000.jpeg 2x" data-dominant-color="A99C8D"><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">1920×2560 814 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><br>
I don’t know if I need to turn it to Boolean first for the targets=train_df[targets]?</p>

ptrblck: <p>I’m not sure which line of code is rasing the error but it seems you might have narrowed it down to the indexing of the <code>pandas.DataFrame</code>? Are you trying to pass floating point values as the index to it?</p>

Rexedoziem: <p>This line targets=train_df[targets].values is the problem, since I tried passing in float numbers from the dataframe as targets which contains 6 target variables of all float numbers, so calling “ targets=train_df[targets].values ” gave the error?</p>

Rexedoziem: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/4/64dc64d82e38f143912d4cbd7072e8a237967762.png" data-download-href="https://discuss.pytorch.org/uploads/default/64dc64d82e38f143912d4cbd7072e8a237967762" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/4/64dc64d82e38f143912d4cbd7072e8a237967762_2_690x387.png" alt="image" data-base62-sha1="eofWcVUtN9rN9UQuKrj7qGsnvl8" width="690" height="387" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/4/64dc64d82e38f143912d4cbd7072e8a237967762_2_690x387.png, https://discuss.pytorch.org/uploads/default/optimized/3X/6/4/64dc64d82e38f143912d4cbd7072e8a237967762_2_1035x580.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/4/64dc64d82e38f143912d4cbd7072e8a237967762.png 2x" data-dominant-color="CFD1D3"><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">1366×768 129 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><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/2/82d7f6cdff32cbe943486de35c01e44db1789ecf.png" data-download-href="https://discuss.pytorch.org/uploads/default/82d7f6cdff32cbe943486de35c01e44db1789ecf" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/2/82d7f6cdff32cbe943486de35c01e44db1789ecf_2_690x387.png" alt="image" data-base62-sha1="iFuLRPiafOw97dMWfjba8n3dZkr" width="690" height="387" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/2/82d7f6cdff32cbe943486de35c01e44db1789ecf_2_690x387.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/2/82d7f6cdff32cbe943486de35c01e44db1789ecf_2_1035x580.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/2/82d7f6cdff32cbe943486de35c01e44db1789ecf.png 2x" data-dominant-color="D0D2D4"><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">1366×768 127 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><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/9/6987c7c2871d469f20d5258d998fa85a9ae53e15.png" data-download-href="https://discuss.pytorch.org/uploads/default/6987c7c2871d469f20d5258d998fa85a9ae53e15" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/6987c7c2871d469f20d5258d998fa85a9ae53e15_2_690x387.png" alt="image" data-base62-sha1="f3z2CSU6hFdFIfMJJbLwJIlyho1" width="690" height="387" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/6987c7c2871d469f20d5258d998fa85a9ae53e15_2_690x387.png, https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/6987c7c2871d469f20d5258d998fa85a9ae53e15_2_1035x580.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/9/6987c7c2871d469f20d5258d998fa85a9ae53e15.png 2x" data-dominant-color="D3CBCC"><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">1366×768 121 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><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/8/b8379e9575584a28b1348dcd7d76058b66d6a690.png" data-download-href="https://discuss.pytorch.org/uploads/default/b8379e9575584a28b1348dcd7d76058b66d6a690" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/8/b8379e9575584a28b1348dcd7d76058b66d6a690_2_690x387.png" alt="image" data-base62-sha1="qhF19YixqF5eYzdOSiSQmmRxTji" width="690" height="387" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/8/b8379e9575584a28b1348dcd7d76058b66d6a690_2_690x387.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/8/b8379e9575584a28b1348dcd7d76058b66d6a690_2_1035x580.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/8/b8379e9575584a28b1348dcd7d76058b66d6a690.png 2x" data-dominant-color="D4C8C9"><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">1366×768 116 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>

ptrblck: <p>Based on your screenshot it is indeed an error in the indexing of the <code>pandas.DataFrame</code>, so make sure you are passing valid indices to it.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ``` instead of screenshots which generally makes debugging easier.<br>
If you get stuck, feel free to post a minimal, executable code snippet which would reproduce the error.</p>

Rexedoziem: <p>import torch<br>
import torch.nn as nn</p>
<p>class BertDataset:<br>
def <strong>init</strong>(self, df, texts, targets):<br>
self.df = df<br>
self.targets = df[‘targets’]<br>
self.max_len = config.max_len<br>
self.texts = df[‘full_text’]<br>
self.tokenizer = config.TOKENIZER</p>
<pre><code>def __len__(self):
    return len(self.texts)

def __getitem__(self, index):
    texts = ' '.join(str(self.texts[index]).split())

    inputs = self.tokenizer.encode(
        texts,
        None,
        max_length = self.max_len,
        padding = 'max_length',
        truncation = True,
        add_special_tokens = True

    )

    resp = {
        'ids': torch.tensor(inputs['input_ids'], dtype=torch.long),
        'mask': torch.tensor(inputs['attention_mask'], dtype=torch.long),
        'token_type_ids': torch.tensor(inputs['token_type_ids'], dtype=torch.long),
        'target': torch.tensor(self.targets[index], dtype=torch.long)
    }

    return resp
</code></pre>
<p>I passed in the correct index in the dataframe and it’s still giving issues? It says none of the index are in the column?</p>

ptrblck: <p>Your code is not executable so I cannot debug it, but your error seems to be pandas-specific and unrelated to PyTorch. If you could create a minimal and executable code snippet which we could copy/paste to debug, I’m happy to help.  Otherwise, I can only refer to the pandas docs to check how <code>DataFrame</code>s should be indexed.</p>

Rexedoziem: <p>import pandas as pd</p>
<p>df = pd.read_csv(‘train_folds.csv’)<br>
train_df = df[df.kfold != fold].reset_index(drop=True)<br>
valid_df = df[df.kfold == fold].reset_index(drop=True)</p>
<p>train_dataset = DeBERTadataset(<br>
train_df, texts = train_df.full_text.values,<br>
target = train_df.loc[:, targets].values)<br>
train_data_loader = torch.utils.data.DataLoader(<br>
train_dataset,<br>
batch_size = config.TRAIN_BATCH_SIZE,<br>
shuffle = True,<br>
num_workers = 2)<br>
valid_dataset = DeBERTadataset(<br>
valid_df,<br>
texts = valid_df.full_text.values,<br>
target = valid_df.loc[:, targets].values<br>
)<br>
valid_data_loader = torch.utils.data.DataLoader(<br>
valid_dataset,<br>
batch_size = config.VALID_BATCH_SIZE,<br>
shuffle = False,<br>
num_workers = 1<br>
)</p>

Rexedoziem: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/5/55a86120edd69e167210b0350fe6384571e9506b.png" data-download-href="https://discuss.pytorch.org/uploads/default/55a86120edd69e167210b0350fe6384571e9506b" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/5/55a86120edd69e167210b0350fe6384571e9506b_2_690x387.png" alt="image" data-base62-sha1="cdLkQnFQMQOyiX9cin9jTa0mDuX" width="690" height="387" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/5/55a86120edd69e167210b0350fe6384571e9506b_2_690x387.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/5/55a86120edd69e167210b0350fe6384571e9506b_2_1035x580.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/5/55a86120edd69e167210b0350fe6384571e9506b.png 2x" data-dominant-color="D5C8C9"><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">1366×768 116 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><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/7/c7267a0dee7540c5f885fa75a270302c3655d84d.png" data-download-href="https://discuss.pytorch.org/uploads/default/c7267a0dee7540c5f885fa75a270302c3655d84d" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/7/c7267a0dee7540c5f885fa75a270302c3655d84d_2_690x387.png" alt="image" data-base62-sha1="spLs4ibfJnhPzW6k58riv31PtNz" width="690" height="387" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/7/c7267a0dee7540c5f885fa75a270302c3655d84d_2_690x387.png, https://discuss.pytorch.org/uploads/default/optimized/3X/c/7/c7267a0dee7540c5f885fa75a270302c3655d84d_2_1035x580.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/7/c7267a0dee7540c5f885fa75a270302c3655d84d.png 2x" data-dominant-color="D3C6C8"><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">1366×768 122 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>This is the issue?</p>

ptrblck: <p>The new error now points to an invalid key value (the type is correct this time).</p>

Rexedoziem: <p>I don’t really know where it’s coming from but I feel it might be from the dataset class?</p>

Rexedoziem: <p>Maybe the getitem function?</p>

ptrblck: <p>No, it’s the indexing in the <code>pandas.DataFrame</code> again and unrelated to PyTorch:</p>
<pre><code class="lang-python">df = pd.DataFrame({'a': np.random.randn(10)})
df[2885]
# KeyError: 2885
</code></pre>

Rexedoziem: <p>I guess I didn’t specify the targets columns in the df=pd.read_csv(input, column=targets), since i placed in targets to be targets = [‘cohesion’, ‘syntax’, ‘vocabulary’, ‘phraseology’, ‘grammar’, ‘conventions’]………<br>
But I don’t know if it will work fine?</p>