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

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日

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序