如何修复运行时错误:当在pytorch中从jit创建检查点时,内置不能用作值(使用dict)?

2024-10-02 14:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用jit编译任意nn模块

在我的代码中,我将我的值与dict进行比较,它会抛出一个错误

        """
        if type(json_data) is dict:
                              ~~~~ <--- HERE

复制

简单地说,任何与dict进行比较的代码:

class Node(object):
    def __init__(self):
        pass

    @classmethod
    def from_json(cls, json_data):
        if type(json_data) is dict:
            node_data = next(iter(json_data))
            assert type(json_data[node_data]) is list
            node_children = [cls.from_json(child) for child in json_data[node_data]]
            return Node(node_data, node_children)
        else:
            return Node(json_data)

预期行为

Jit是我的检查点

环境

  • PyTorch/torchvision版本(例如,1.0/0.4.0):1.7.1
  • 操作系统(如Linux):mac OS x
  • 如何安装PyTorch/torchvision(condapip,源代码):conda
  • 使用的生成命令(如果从源代码处编译):conda
  • Python版本:3.8
  • CUDA/cuDNN版本:CPU
  • GPU型号和配置:CPU
  • 任何其他相关信息:CPU

附加上下文

将任意自定义nn模块编译为jit

错误:

/Users/brando/anaconda3/envs/coq_gym/bin/python /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --cmd-line --multiproc --qt-support=auto --client 127.0.0.1 --port 59213 --file /Users/brando/ML4Coq/playground/running_pytorch_ocaml/treenn2jit_ckpt.py
Connected to pydev debugger (build 203.7148.72)
1.7.1
Traceback (most recent call last):
  File "/Users/brando/anaconda3/envs/coq_gym/lib/python3.7/site-packages/torch/jit/_recursive.py", line 680, in compile_unbound_method
    create_methods_and_properties_from_stubs(concrete_type, (stub,), ())
  File "/Users/brando/anaconda3/envs/coq_gym/lib/python3.7/site-packages/torch/jit/_recursive.py", line 304, in create_methods_and_properties_from_stubs
    concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
  File "/Users/brando/anaconda3/envs/coq_gym/lib/python3.7/site-packages/torch/jit/annotations.py", line 330, in try_ann_to_type
    torch.jit._script._recursive_compile_class(ann, loc)
  File "/Users/brando/anaconda3/envs/coq_gym/lib/python3.7/site-packages/torch/jit/_script.py", line 1056, in _recursive_compile_class
    _compile_and_register_class(obj, rcb, _qual_name)
  File "/Users/brando/anaconda3/envs/coq_gym/lib/python3.7/site-packages/torch/jit/_script.py", line 64, in _compile_and_register_class
    torch._C._jit_script_class_compile(qualified_name, ast, defaults, rcb)
RuntimeError: 
builtin cannot be used as a value:
  File "/Users/brando/ML4Coq/ml4coq-proj/embeddings_zoo/extract_tactic_from_lasse_data.py", line 56
            term = string
        """
        if type(json_data) is dict:
                              ~~~~ <--- HERE
            node_data = next(iter(json_data))
            assert type(json_data[node_data]) is list
'Node.from_json' is being compiled since it was called from '__torch__.embeddings_zoo.extract_tactic_from_lasse_data.Node'

相关的:


Tags: infrompynodejsondataistype