'str' object has no attribute 'cpu'

burakoglakci: <p>Hi everyone,</p>
<p>With Python 3, I use an sentiment analysis model based on Google BERT that can be trained. The model was working very stably, but for a week in the ‘mapping’ part of the model; I get an error in the form of ‘str’ object has no attribute ‘cpu’ and I couldn’t find a solution. Where could the problem come from?</p>
<pre><code class="lang-auto">import json
import random
import warnings
from datetime import datetime
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import torch
from keras.callbacks import EarlyStopping
from keras.layers import Dense, Dropout
from keras.models import Sequential
from keras.optimizers import Adagrad
from sklearn.decomposition import PCA
from sklearn.metrics import classification_report, f1_score
from sklearn.utils import class_weight
from tqdm import tqdm
from transformers import AutoModel, AutoTokenizer


def filter(text):
    final_text = ''
    for word in text.split():
        if word.startswith('@'):
            continue
        elif word[-3:] in ['com', 'org']:
            continue
        elif word.startswith('pic') or word.startswith('http') or word.startswith('www'):
            continue
        else:
            final_text += word+' '
    return final_text


tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-base-turkish-128k-uncased")
bert = AutoModel.from_pretrained("dbmdz/bert-base-turkish-128k-uncased").to('cuda')

def feature_extraction(text):
    x = tokenizer.encode(filter(text))
    with torch.no_grad():
        x, _ = bert(torch.stack([torch.tensor(x)]).to('cuda'))
        return list(x[0][0].cpu().numpy())


with open(train_path, 'r') as f:
    train = json.load(f)
with open(val_path, 'r') as f:
    val = json.load(f)
with open(test_path, 'r') as f:
    test = json.load(f)


mapping = {'negative':0, 'neutral':1, 'positive':2}
X_train = []
y_train = []
X_test = []
y_test = []
for element in tqdm(train):
    X_train.append(feature_extraction(element['sentence']))
    y_train.append(mapping[element['value']])
for element in tqdm(test):
    X_test.append(feature_extraction(element['sentence']))
    y_test.append(mapping[element['value']])

  0%|          | 0/1070 [00:00&lt;?, ?it/s]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-7-74243ebcdd12&gt; in &lt;module&gt;()
      5 y_test = []
      6 for element in tqdm(train):
----&gt; 7     X_train.append(feature_extraction(element['sentence']))
      8     y_train.append(mapping[element['value']])
      9 for element in tqdm(test):

&lt;ipython-input-5-8e259a97cc89&gt; in feature_extraction(text)
      6     with torch.no_grad():
      7         x, _ = bert(torch.stack([torch.tensor(x)]).to('cuda'))
----&gt; 8         return list(x[0][0].cpu().numpy())

AttributeError: 'str' object has no attribute 'cpu'
</code></pre>

albanD: <p>Hi,</p>
<p>It looks like the result of your bert model is a string and not a Tensor. You might want to check why that changed</p>

burakoglakci: <p>Dear <a class="mention" href="/u/alband">@albanD</a> thanks for the answer. The error was caused by updating the transformers library I used. The problem was resolved when I set the transformers version to 3.0.2.</p>