[{"data":1,"prerenderedAt":162},["ShallowReactive",2],{"content-query-MVjCs9joyj":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":156,"_id":157,"_source":158,"_file":159,"_stem":160,"_extension":161},"/technology-blogs/zh/1868","zh",false,"","【MindSpore易点通】通过Print方法保存输出数据","mindspore.ops.Print方法通过context接口中print_file_path参数将数据保存在文件中","2022-09-22","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/09/30/e4e2b9ee5d0149d5b1d407eeb13b979a.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":143},"root",[17,25,31,37,43,50,60,66,71,79,85,90,98,104,109,117,122,130,135],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mindspore易点通通过print方法保存输出数据",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":29},"h3",{"id":28},"背景信息",[30],{"type":24,"value":28},{"type":18,"tag":32,"props":33,"children":34},"p",{},[35],{"type":24,"value":36},"当开发人员遇到数据量较大的张量或字符串输出时，默认情况下，程序会将输出的数据打印到屏幕上，海量的数据无法用人眼直接识别有价值的数据，此时mindspore.ops.Print方法应运而生，该方法通过context接口中print_file_path参数将其保存在文件中。输出数据将保存在指定路径的文件中。之后可使用mindspore.parse_print()方法加载保存的数据。",{"type":18,"tag":26,"props":38,"children":40},{"id":39},"一保存print数据",[41],{"type":24,"value":42},"一、保存print数据",{"type":18,"tag":44,"props":45,"children":47},"h4",{"id":46},"_1示例代码",[48],{"type":24,"value":49},"1、示例代码",{"type":18,"tag":51,"props":52,"children":54},"pre",{"code":53},"import numpy as npimport mindspore.ops as opsfrom mindspore import Tensor, contextfrom mindspore.ops import operations as Pimport mindspore.nn as nnimport mindspore\n\n#保存数据\n\ncontext.set_context(mode=context.GRAPH_MODE, print_file_path='./save_print_data', device_target=\"Ascend\")\n\nclass PrintDemo(nn.Cell):\n\n        def __init__(self):\n\n            super(PrintDemo, self).__init__()\n\n            self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=4, stride=1 ,has_bias=False, weight_init='normal', pad_mode='valid')\n\n            self.conv2 = nn.Conv2d(in_channels=6, out_channels=2, kernel_size=2, pad_mode=\"valid\")\n\n            self.conv3 = nn.Conv2d(in_channels=2, out_channels=6, kernel_size=2, pad_mode=\"valid\")\n\n            self.print = P.Print()\n\n        \n\n        #打印出每层计算输出结果\n\n        def construct(self, input_data):\n\n            x = self.conv1(input_data)\n\n            self.print('self.conv1', x)\n\n            x = self.conv2(x)\n\n            self.print('self.conv2', x)\n\n            x = self.conv3(x)\n\n            self.print('self.conv3', x)\n\n            return x\n\ndef test():\n\n    input_data = Tensor(np.ones([1, 1, 32, 32]), mindspore.float32)\n\n    net = PrintDemo()\n\n    net(input_data)\n\n    return net(input_data)\n\n\n\ntest()\n",[55],{"type":18,"tag":56,"props":57,"children":58},"code",{"__ignoreMap":7},[59],{"type":24,"value":53},{"type":18,"tag":44,"props":61,"children":63},{"id":62},"_2代码解析context设置",[64],{"type":24,"value":65},"2、代码解析：context设置",{"type":18,"tag":32,"props":67,"children":68},{},[69],{"type":24,"value":70},"为context.set_context方法添加print_file_path参数来设置保存输出数据的路径：",{"type":18,"tag":51,"props":72,"children":74},{"code":73},"mindspore.context.set_context(mode=context.GRAPH_MODE,\n\n                    device_target=\"Ascend\"，print_file_path='./xxx_data')\n",[75],{"type":18,"tag":56,"props":76,"children":77},{"__ignoreMap":7},[78],{"type":24,"value":73},{"type":18,"tag":26,"props":80,"children":82},{"id":81},"二解析print数据",[83],{"type":24,"value":84},"二、解析print数据",{"type":18,"tag":44,"props":86,"children":88},{"id":87},"_1示例代码-1",[89],{"type":24,"value":49},{"type":18,"tag":51,"props":91,"children":93},{"code":92},"data = mindspore.parse_print('./xxx_data')\n\nfor i in range(0,len(data)):\n\n    if isinstance(data[i], str):\n\n        print(data[i])\n\n        continue\n\n    else:\n\n        tmp = data[i].asnumpy()\n\n        data[i] = tmp\n\nprint(data)\n",[94],{"type":18,"tag":56,"props":95,"children":96},{"__ignoreMap":7},[97],{"type":24,"value":92},{"type":18,"tag":44,"props":99,"children":101},{"id":100},"_2代码解析",[102],{"type":24,"value":103},"2、代码解析",{"type":18,"tag":32,"props":105,"children":106},{},[107],{"type":24,"value":108},"mindspore.ops.Print方法所保存的数据为二进制文件，不便于开发人员使用，故需要进一步的文件解析。在mindspore中提供了该文件的解析方法如下所示：",{"type":18,"tag":51,"props":110,"children":112},{"code":111},"data = mindspore.parse_print('./xxx_data')\n",[113],{"type":18,"tag":56,"props":114,"children":115},{"__ignoreMap":7},[116],{"type":24,"value":111},{"type":18,"tag":32,"props":118,"children":119},{},[120],{"type":24,"value":121},"此时该文件解析后的数据格式为：list['输入名称（str）', Tensor]，示例代码运行的结果如下所示：",{"type":18,"tag":32,"props":123,"children":124},{},[125],{"type":18,"tag":126,"props":127,"children":129},"img",{"alt":7,"src":128},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/09/30/4309459f154343599fcbf3075e93b551.png",[],{"type":18,"tag":32,"props":131,"children":132},{},[133],{"type":24,"value":134},"由于输入将转变为Tensor数据类型输出，不便于开发人员进行数据计算操作，故对此数据类型转换为numpy数据类型：",{"type":18,"tag":51,"props":136,"children":138},{"code":137},"data = mindspore.parse_print('./xxx.data')for i in range(0,len(data)):\n\n    if isinstance(data[i], str):\n\n        print(data[i])\n\n        continue\n\n    else:\n\n        tmp = data[i].asnumpy()\n\n        data[i] = tmp\n\nprint(data)\n\n",[139],{"type":18,"tag":56,"props":140,"children":141},{"__ignoreMap":7},[142],{"type":24,"value":137},{"title":7,"searchDepth":144,"depth":144,"links":145},4,[146,148,152],{"id":28,"depth":147,"text":28},3,{"id":39,"depth":147,"text":42,"children":149},[150,151],{"id":46,"depth":144,"text":49},{"id":62,"depth":144,"text":65},{"id":81,"depth":147,"text":84,"children":153},[154,155],{"id":87,"depth":144,"text":49},{"id":100,"depth":144,"text":103},"markdown","content:technology-blogs:zh:1868.md","content","technology-blogs/zh/1868.md","technology-blogs/zh/1868","md",1776506116501]