File size: 13,046 Bytes
b84549f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
import os
import torch
from torch.utils.data import Dataset, DataLoader
from transformers import BertTokenizer, BertForSequenceClassification
from torch.optim import Adam
from torch.nn import CrossEntropyLoss
from typing import Dict, List, Optional, Any
from utils.common.data_record import read_json
from .global_bert_tokenizer import get_tokenizer
# 自定义数据集类
class UniversalASC19DomainsDataset(Dataset):
def __init__(self, root_dir: str, split: str, transform: Any,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
assert transform is None
self.tokenizer = get_tokenizer() # 传入tokenizer对象
self.texts = []
self.labels = []
self.max_length = 512 # 设置文本的最大长度
json_file_path = os.path.join(root_dir, f'{split if split != "val" else "dev"}.json')
anns = read_json(json_file_path)
label_map = {'-': 0, '+': 1, 'negative': 0, 'positive': 1}
ignore_cls_indexes = [classes.index(c) for c in ignore_classes]
for v in anns.values():
if v['polarity'] not in label_map.keys():
continue
cls = label_map[v['polarity']]
if cls in ignore_cls_indexes:
continue
self.texts += [v['sentence']]
self.labels += [idx_map[cls] if idx_map is not None else cls]
def __len__(self):
return len(self.texts)
def __getitem__(self, idx):
text = self.texts[idx]
label = self.labels[idx]
encoded_input = self.tokenizer.encode_plus(
text, max_length=self.max_length, padding="max_length", truncation=True, return_tensors="pt"
)
x = {key: tensor.squeeze(0) for key, tensor in encoded_input.items()}
x['return_dict'] = False
return x, torch.tensor(label)
from ..ab_dataset import ABDataset
from ..registery import dataset_register
@dataset_register(
name='HL5Domains-ApexAD2600Progressive',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class HL5Domains_ApexAD2600Progressive(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='HL5Domains-CanonG3',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class HL5Domains_CanonG3(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='HL5Domains-CreativeLabsNomadJukeboxZenXtra40GB',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class HL5Domains_CreativeLabsNomadJukeboxZenXtra40GB(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='HL5Domains-NikonCoolpix4300',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class HL5Domains_NikonCoolpix4300(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='HL5Domains-Nokia6610',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class HL5Domains_Nokia6610(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='Liu3Domains-Computer',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Liu3Domains_Computer(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='Liu3Domains-Router',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Liu3Domains_Router(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='Liu3Domains-Speaker',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Liu3Domains_Speaker(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
# import os
# for domain in os.listdir('/data/zql/datasets/nlp_asc_19_domains/dat/absa/Bing9Domains/asc'):
# print(f"""
# @dataset_register(
# name='Ding9Domains-{domain}',
# classes=['negative', 'positive'],
# task_type='Sentiment Classification',
# object_type='Emotion',
# class_aliases=[],
# shift_type=None
# )
# class Ding9Domains_{domain}(ABDataset):
# def create_dataset(self, root_dir: str, split: str, transform,
# classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
# return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
# """)
@dataset_register(
name='Ding9Domains-DiaperChamp',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Ding9Domains_DiaperChamp(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='Ding9Domains-Norton',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Ding9Domains_Norton(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='Ding9Domains-LinksysRouter',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Ding9Domains_LinksysRouter(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='Ding9Domains-MicroMP3',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Ding9Domains_MicroMP3(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='Ding9Domains-Nokia6600',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Ding9Domains_Nokia6600(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='Ding9Domains-CanonPowerShotSD500',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Ding9Domains_CanonPowerShotSD500(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='Ding9Domains-ipod',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Ding9Domains_ipod(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='Ding9Domains-HitachiRouter',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Ding9Domains_HitachiRouter(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='Ding9Domains-CanonS100',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class Ding9Domains_CanonS100(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='SemEval-Laptop',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class SemEval_Laptop(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map)
@dataset_register(
name='SemEval-Rest',
classes=['negative', 'positive'],
task_type='Sentiment Classification',
object_type='Emotion',
class_aliases=[],
shift_type=None
)
class SemEval_Rest(ABDataset):
def create_dataset(self, root_dir: str, split: str, transform,
classes: List[str], ignore_classes: List[str], idx_map: Optional[Dict[int, int]]):
return UniversalASC19DomainsDataset(root_dir, split, transform, classes, ignore_classes, idx_map) |