RuntimeError: shape '[4, 512]' is invalid for input of size 1024 while while evaluating test data
RuntimeError: shape '[4, 512]' is invalid for input of size 1024 while while evaluating test data
我正在尝试 XLnet over Jigsaw toxic 数据集。
当我使用
训练我的数据时
input_ids = d["input_ids"].reshape(4,512).to(device) # batch size x seq length
它训练完美。
但是当我尝试用测试数据测试模型并以同样的方式重塑 input_ids 时,它会产生一个 运行 时间错误:
shape '[4, 512]' is invalid for input of size 1024
这是我用来训练的方法:
def train_epoch(model, data_loader, optimizer, device, scheduler, n_examples):
model = model.train()
losses = []
acc = 0
counter = 0
for d in data_loader:
input_ids = d["input_ids"].reshape(4,512).to(device)
attention_mask = d["attention_mask"].to(device)
targets = d["targets"].to(device)
outputs = model(input_ids=input_ids, token_type_ids=None, attention_mask=attention_mask, labels = targets)
loss = outputs[0]
logits = outputs[1]
# preds = preds.cpu().detach().numpy()
_, prediction = torch.max(outputs[1], dim=1)
targets = targets.cpu().detach().numpy()
prediction = prediction.cpu().detach().numpy()
accuracy = metrics.accuracy_score(targets, prediction)
acc += accuracy
losses.append(loss.item())
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
scheduler.step()
optimizer.zero_grad()
counter = counter + 1
return acc / counter, np.mean(losses)
这是我用来评估测试数据的方法:
def eval_model(model, data_loader, device, n_examples):
model = model.eval()
losses = []
acc = 0
counter = 0
with torch.no_grad():
for d in data_loader:
# print(d["input_ids"])
input_ids = d["input_ids"].reshape(4,512).to(device)
attention_mask = d["attention_mask"].to(device)
targets = d["targets"].to(device)
outputs = model(input_ids=input_ids, token_type_ids=None, attention_mask=attention_mask, labels = targets)
loss = outputs[0]
logits = outputs[1]
_, prediction = torch.max(outputs[1], dim=1)
targets = targets.cpu().detach().numpy()
prediction = prediction.cpu().detach().numpy()
accuracy = metrics.accuracy_score(targets, prediction)
acc += accuracy
losses.append(loss.item())
counter += 1
return acc / counter, np.mean(losses)
当我尝试使用我的测试数据 运行 eval_model 方法时,它会生成 运行 时间错误。
我的模型信息:
我无法理解我做错了什么。谁能帮我解决这个问题?谢谢。
我认为问题在于训练数据集的 d['input_ids']
大小为 4*512 = 2048,因此它可以分为 4 和 512。
但是测试数据集的d['input_ids']
大小为1024,不能分为4和512。
由于您没有给出 model
描述,我不能说您是否应该将其更改为 (-1, 512) 或 (4, -1) [在 reshape 中使用 -1 说明numpy 自动计算出该维度。
例如将 2048 个元素的数组重塑为 (4, 512) 也可以通过 reshape(4,512)
和 reshape(-1, 512)
以及 reshape(4, -1)
来完成。
我正在尝试 XLnet over Jigsaw toxic 数据集。
当我使用
训练我的数据时input_ids = d["input_ids"].reshape(4,512).to(device) # batch size x seq length
它训练完美。 但是当我尝试用测试数据测试模型并以同样的方式重塑 input_ids 时,它会产生一个 运行 时间错误:
shape '[4, 512]' is invalid for input of size 1024
这是我用来训练的方法:
def train_epoch(model, data_loader, optimizer, device, scheduler, n_examples):
model = model.train()
losses = []
acc = 0
counter = 0
for d in data_loader:
input_ids = d["input_ids"].reshape(4,512).to(device)
attention_mask = d["attention_mask"].to(device)
targets = d["targets"].to(device)
outputs = model(input_ids=input_ids, token_type_ids=None, attention_mask=attention_mask, labels = targets)
loss = outputs[0]
logits = outputs[1]
# preds = preds.cpu().detach().numpy()
_, prediction = torch.max(outputs[1], dim=1)
targets = targets.cpu().detach().numpy()
prediction = prediction.cpu().detach().numpy()
accuracy = metrics.accuracy_score(targets, prediction)
acc += accuracy
losses.append(loss.item())
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
scheduler.step()
optimizer.zero_grad()
counter = counter + 1
return acc / counter, np.mean(losses)
这是我用来评估测试数据的方法:
def eval_model(model, data_loader, device, n_examples):
model = model.eval()
losses = []
acc = 0
counter = 0
with torch.no_grad():
for d in data_loader:
# print(d["input_ids"])
input_ids = d["input_ids"].reshape(4,512).to(device)
attention_mask = d["attention_mask"].to(device)
targets = d["targets"].to(device)
outputs = model(input_ids=input_ids, token_type_ids=None, attention_mask=attention_mask, labels = targets)
loss = outputs[0]
logits = outputs[1]
_, prediction = torch.max(outputs[1], dim=1)
targets = targets.cpu().detach().numpy()
prediction = prediction.cpu().detach().numpy()
accuracy = metrics.accuracy_score(targets, prediction)
acc += accuracy
losses.append(loss.item())
counter += 1
return acc / counter, np.mean(losses)
当我尝试使用我的测试数据 运行 eval_model 方法时,它会生成 运行 时间错误。
我的模型信息:
我无法理解我做错了什么。谁能帮我解决这个问题?谢谢。
我认为问题在于训练数据集的 d['input_ids']
大小为 4*512 = 2048,因此它可以分为 4 和 512。
但是测试数据集的d['input_ids']
大小为1024,不能分为4和512。
由于您没有给出 model
描述,我不能说您是否应该将其更改为 (-1, 512) 或 (4, -1) [在 reshape 中使用 -1 说明numpy 自动计算出该维度。
例如将 2048 个元素的数组重塑为 (4, 512) 也可以通过 reshape(4,512)
和 reshape(-1, 512)
以及 reshape(4, -1)
来完成。