# start
check all output paths
checkPath('./results/')
checkPath(f'./results/{dataset}/')
checkPath(f'{loader.task_dir}/saveModel/')
model = BaseModel(opts, loader)
opts.perf_file = f'results/{dataset}/{model.modelName}_perf.txt'
print(f'==> perf_file: {opts.perf_file}')
config_str = '%.4f, %.4f, %.6f, %d, %d, %d, %d, %.4f,%s\n' % (
opts.lr, opts.decay_rate, opts.lamb, opts.hidden_dim, opts.attn_dim, opts.n_layer, opts.n_batch, opts.dropout,
opts.act)
print(config_str)
with open(opts.perf_file, 'a+') as f:
f.write(config_str)
if args.weight != None:
model.loadModel(args.weight)
model._update()
model.model.updateTopkNums(opts.n_node_topk)
if opts.train:
writer = SummaryWriter(log_dir=f'./tensorboard_logs/{dataset}')
# training mode
best_v_mrr = 0
for epoch in range(opts.epoch):
epoch_loss = model.train_batch()
if epoch_loss is not None:
writer.add_scalar('Training Loss', epoch_loss, epoch)
else:
print("Warning: Skipping logging of Training Loss due to NoneType.")
model.train_batch()
# eval on val/test set
if (epoch + 1) % args.eval_interval == 0:
result_dict, out_str = model.evaluate(eval_val=True, eval_test=True)
v_mrr, t_mrr = result_dict['v_mrr'], result_dict['t_mrr']
writer.add_scalar('Validation MRR', result_dict['v_mrr'], epoch)
writer.add_scalar('Validation Hits@1', result_dict['v_h1'], epoch)
writer.add_scalar('Validation Hits@10', result_dict['v_h10'], epoch)
writer.add_scalar('Test MRR', result_dict['t_mrr'], epoch)
writer.add_scalar('Test Hits@1', result_dict['t_h1'], epoch)
writer.add_scalar('Test Hits@10', result_dict['t_h10'], epoch)
print(out_str)
with open(opts.perf_file, 'a+') as f:
f.write(out_str)
if v_mrr > best_v_mrr:
best_v_mrr = v_mrr
best_str = out_str
print(str(epoch) + '\t' + best_str)
BestMetricStr = f'ValMRR_{str(v_mrr)[:5]}_TestMRR_{str(t_mrr)[:5]}'
model.saveModelToFiles(BestMetricStr, deleteLastFile=False)
# show the final result
print(best_str)
writer.close()
model.writer.close()
编程未来,从这里启航!解锁无限创意,让每一行代码都成为你通往成功的阶梯,帮助更多人欣赏与学习;更多内容详见:传知代码