'str' object has no attribute 'cpu'

Hi everyone,

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?

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('@'):
        elif word[-3:] in ['com', 'org']:
        elif word.startswith('pic') or word.startswith('http') or word.startswith('www'):
            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):
for element in tqdm(test):

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

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

AttributeError: 'str' object has no attribute 'cpu'


It looks like the result of your bert model is a string and not a Tensor. You might want to check why that changed

1 Like

Dear @albanD 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.