weixin_53141261 2022-03-30 11:01 采纳率: 100%
浏览 167
已结题

TypeError: 'GPT2Tokenizer' object is not callable

问题遇到的现象和发生背景

我安装了GPU版本的torch和tensorflow,但一直出现这个问题,出问题的代码我已经加粗,请问应该怎么解决?感激不尽

问题相关代码,请勿粘贴截图

import os
import re
import math
import torch
import random
import pickle
import datetime
from rouge import rouge
from bleu import compute_bleu

class DataLoader:
def init(self, data_path, index_dir, tokenizer, seq_len):
self.user_dict = EntityDictionary()
self.item_dict = EntityDictionary()
self.max_rating = float('-inf')
self.min_rating = float('inf')
self.initialize(data_path)
self.feature_set = set()
** self.tokenizer = tokenizer**
self.seq_len = seq_len
self.train, self.valid, self.test, self.user2feature, self.item2feature = self.load_data(data_path, index_dir)

def initialize(self, data_path):
    assert os.path.exists(data_path)
    reviews = pickle.load(open(data_path, 'rb'))
    for review in reviews:
        self.user_dict.add_entity(review['user'])
        self.item_dict.add_entity(review['item'])
        rating = review['rating']
        if self.max_rating < rating:
            self.max_rating = rating
        if self.min_rating > rating:
            self.min_rating = rating

def load_data(self, data_path, index_dir):
    data = []
    reviews = pickle.load(open(data_path, 'rb'))
    for review in reviews:
        (fea, adj, tem, sco) = review['template']

** tokens = self.tokenizer(tem)['input_ids']**
text = self.tokenizer.decode(tokens[:self.seq_len]) # keep seq_len tokens at most
data.append({'user': self.user_dict.entity2idx[review['user']],
'item': self.item_dict.entity2idx[review['item']],
'rating': review['rating'],
'text': text,
'feature': fea})
self.feature_set.add(fea)

    train_index, valid_index, test_index = self.load_index(index_dir)
    train, valid, test = [], [], []
    user2feature, item2feature = {}, {}
    for idx in train_index:
        review = data[idx]
        train.append(review)
        u = review['user']
        i = review['item']
        f = review['feature']
        if u in user2feature:
            user2feature[u].append(f)
        else:
            user2feature[u] = [f]
        if i in item2feature:
            item2feature[i].append(f)
        else:
            item2feature[i] = [f]
    for idx in valid_index:
        valid.append(data[idx])
    for idx in test_index:
        test.append(data[idx])
    return train, valid, test, user2feature, item2feature
运行结果及报错内容

Traceback (most recent call last):
File "main.py", line 66, in
corpus = DataLoader(args.data_path, args.index_dir, tokenizer, args.words)
File "G:\projection\PEPLER\utils.py", line 148, in init
self.train, self.valid, self.test, self.user2feature, self.item2feature = self.load_data(data_path, index_dir)
File "G:\projection\PEPLER\utils.py", line 167, in load_data
tokens = self.tokenizer(tem)['input_ids']
TypeError: 'GPT2Tokenizer' object is not callable

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

2条回答 默认 最新

  • CSDN专家-HGJ 2022-03-30 14:18
    关注

    tokens = self.tokenizer(tem)['input_ids'],传入参数tem对tokenizer进行调用,是作为方法或函数使用,而报错提示GPT2Tokenizer类对象不是可调用对象。检查一下参数tokenizer,是否对象没有实例化就作为参数传入了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月8日
  • 已采纳回答 3月31日
  • 创建了问题 3月30日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度