# 迁移网络tacotron2时mindspore的权重初始化与torch的不一致

**1. 系统环境**

硬件环境\(Ascend/GPU/CPU\): GPU

软件环境: MindSpore 版本: 2.0

执行模式（静态图/动态图）： 不限

Python 版本: 3.7.6

操作系统平台: linux

**2. 报错信息**

2.1 问题描述

迁移网络任务-tacotron2时mindspore的权重初始化与torch的不一致。

![](figures/zh-cn_image_0000002101294110.png)

2.2 脚本信息

```
ts.migrator.compare_pth_and_ckpt(
    weight_map_path="/home/hodia/python/Pytorch-fasterRCNN/pt_net_info/torch_net_map.json",
    pt_file_path="/home/hodia/python/Pytorch-fasterRCNN/pt_net_info/torch_troubleshooter_create.pth",
    ms_file_path="/home/hodia/python/MindSpore-fasterRCNN/ms_net_info/mindspore_troubleshooter_create.ckpt",
compare_value=True)
```

**3. 解决方案**

将pytorch的权重转化为mindspore，进行训练。

```
ts.migrator.convert_weight_and_load(weight_map_path="/home/hodia/python/Pytorch-fasterRCNN/pt_net_info/torch_net_map.json", pt_file_path="/home/hodia/python/Pytorch-fasterRCNN/pt_net_info/torch_troubleshooter_create.pth", net=model)
```

主要表现在初始化中，Torch的初始化。

```
for layer in self.children():
    if isinstance(layer,nn.Conv2d):
        torch.nn.init.normal_(layer.weight,std=0.01)
        torch.nn.init.constant_(layer.bias,0)
```

MindSpore的初始化

```
for _, layer in self.cells_and_names():
    if isinstance(layer,nn.Conv2d):
        layer.weight.set_data(ms.common.initializer.initializer(ms.common.initializer.Normal(sigma=0.01,mean=0.0),layer.weight.shape,layer.weight.dtype))
        layer.bias.set_data(ms.common.initializer.initializer("zeros",layer.bias.shape,layer.bias.dtype))
```

