如何在MLMD MetadataStore的Execution对象中添加属性?
解决MLMD中Execution添加属性时的"Unknown property"错误
你遇到的InvalidArgumentError: Found unknown property: pipeline_name错误,核心原因是MLMD要求Execution的所有属性必须先在其关联的ExecutionType中预定义,不能直接给Execution设置未在对应ExecutionType里声明的属性。
修正后的代码实现
from ml_metadata.proto import metadata_store_pb2 from ml_metadata.metadata_store import metadata_store def create_mlmd_database_in_memory(): connection_config = metadata_store_pb2.ConnectionConfig() connection_config.fake_database.SetInParent() # 使用内存模拟数据库 store = metadata_store.MetadataStore(connection_config) return store store = create_mlmd_database_in_memory() # 创建带属性定义的ExecutionType trainer_type = metadata_store_pb2.ExecutionType() trainer_type.name = "TrainerTest" # 预定义需要的属性:指定属性名和类型(这里是STRING类型) trainer_type.properties["pipeline_name"].type = metadata_store_pb2.PropertyType.STRING trainer_type_id = store.put_execution_type(trainer_type) # 注册Execution并设置属性 trainer_run = metadata_store_pb2.Execution() trainer_run.type_id = trainer_type_id trainer_run.properties["pipeline_name"].string_value = "PIPELINE_NAME" [run_id] = store.put_executions([trainer_run]) # 验证:获取刚才创建的Execution,检查属性是否正确存储 fetched_executions = store.get_executions_by_id([run_id]) print(fetched_executions[0].properties["pipeline_name"].string_value) # 输出: PIPELINE_NAME
关键说明
- 在定义
ExecutionType时,必须通过properties[属性名].type声明该属性的数据类型(支持STRING、INT、DOUBLE等),MLMD才会允许对应的Execution使用这个属性。 - 后续创建Execution实例时,只能使用在ExecutionType中预定义过的属性名,否则会触发未知属性的错误。
内容的提问来源于stack exchange,提问作者natielle
相关产品推荐
相关产品推荐

