问题遇到的现象和发生背景
我安装了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