PARL icon indicating copy to clipboard operation
PARL copied to clipboard

[dygraph] set weights with wrong params shape should report an error

Open rical730 opened this issue 4 years ago • 0 comments

When loading parameters, paddle2.0 will skip the parameters of the wrong shape and just throw a warning. PARL will add patchs to prevent this kind of situation and report an error

Please note that this patch should be removed after paddle2.0 fixed it.

pip install paddlepaddle==2.0.0rc0

import numpy as np
import paddle
import paddle.nn as nn
import paddle.optimizer as opt

class LinearNet(nn.Layer):
    def __init__(self):
        super(LinearNet, self).__init__()
        self.fc1 = nn.Linear(4, 3)
        self.fc2 = nn.Linear(3, 2)

    def forward(self, x):
        out = self.fc1(x)
        out = self.fc2(out)
        return out

mylayer = LinearNet()
print(mylayer)


params = mylayer.state_dict()
print(params)
print('------------')

params['fc1.weight'] = params['fc1.bias'] # wrong shape
params['fc2.bias'] += 1 # other param modified

mylayer.set_state_dict(params)

params = mylayer.state_dict()
print(params)
print('------------')

rical730 avatar Nov 06 '20 10:11 rical730