allennlp 中的TypeError: Object of type Tensor is not JSON serializable错误

错误展示:

File "/home/yanshangyao/anaconda3/envs/torch14/bin/allennlp", line 8, in 
    sys.exit(run())
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/site-packages/allennlp/run.py", line 18, in run
    main(prog="allennlp")
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/site-packages/allennlp/commands/__init__.py", line 102, in main
    args.func(args)
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/site-packages/allennlp/commands/train.py", line 124, in train_model_from_args
    args.cache_prefix)
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/site-packages/allennlp/commands/train.py", line 168, in train_model_from_file
    cache_directory, cache_prefix)
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/site-packages/allennlp/commands/train.py", line 252, in train_model
    metrics = trainer.train()
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/site-packages/allennlp/training/trainer.py", line 529, in train
    dump_metrics(os.path.join(self._serialization_dir, f'metrics_epoch_{epoch}.json'), metrics)
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/site-packages/allennlp/common/util.py", line 419, in dump_metrics
    metrics_json = json.dumps(metrics, indent=2)
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/json/encoder.py", line 201, in encode
    chunks = list(chunks)
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/json/encoder.py", line 438, in _iterencode
    o = _default(o)
  File "/home/yanshangyao/anaconda3/envs/torch14/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Tensor is not JSON serializable

这个错误发生过很多次了,每次都忘记。首先看到报错中有设计到metric,我就一直以为是meric的格式不对,不符合json.dumps的标准,仔细看错误发现是 Object of type Tensor,其实就是说tensor格式的对象不能dumps,那么我们就需要在get_metirc的时候把塞到metric里面的值首先变为数值。

你可能感兴趣的:(NLP)