alibaba / FederatedScope

An easy-to-use federated learning platform
https://www.federatedscope.io
Apache License 2.0
1.26k stars 206 forks source link

how can i use my saving model #762

Closed junzlovegood closed 5 months ago

junzlovegood commented 5 months ago

I want to use my saving model , but i get error:

Traceback (most recent call last): File "ause_model.py", line 82, in <module> model = load_model('./client_1_main_global_model.pth') File "ause_model.py", line 78, in load_model model.load_state_dict(torch.load(model_path)) File "//site-packages/torch/nn/modules/module.py", line 1482, in load_state_dict raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format( RuntimeError: Error(s) in loading state_dict for MyLSTM: Missing key(s) in state_dict: "rnn.weight_ih_l0", "rnn.weight_hh_l0", "rnn.bias_ih_l0", "rnn.bias_hh_l0", "rnn.weight_ih_l0_reverse", "rnn.weight_hh_l0_reverse", "rnn.bias_ih_l0_reverse", "rnn.bias_hh_l0_reverse", "output_layer.weight", "output_layer.bias". Unexpected key(s) in state_dict: "cur_round", "model".

# 模型结构
class MyLSTM(nn.Module):
    def __init__(self,
                 in_channels,
                 hidden,
                 out_channels,
                 n_layers=1,
                 embed_size=8,
                 dropout=.0):
        super(MyLSTM, self).__init__()
        self.in_channels = in_channels
        self.hidden = hidden
        self.embed_size = embed_size
        self.out_channels = out_channels
        self.n_layers = n_layers

        # self.encoder = nn.Embedding(in_channels, embed_size)

        self.rnn =\
            nn.LSTM(
                input_size=in_channels,
                hidden_size=hidden,
                num_layers=n_layers,
                batch_first=True,
                dropout=dropout,
                bidirectional=True
            )

        # 双向 LSTM 输出维度为 hidden_size * 2
        self.output_layer = nn.Linear(hidden * 2, out_channels)

    def forward(self, input_):
        input_ = input_.unsqueeze(1)
        output, _ = self.rnn(input_)
        lstm_out_last = output[:, -1, :]

        # 通过输出层,得到最终的预测结果
        # 输出的形状: (batch_size, output_size)
        output = self.output_layer(lstm_out_last)

        return output

# 加载模型
# model = torch.load('final_main_global_model.pth')

def load_model(model_path):
    # 创建一个与预训练模型相同结构的实例
    model = MyLSTM(in_channels=23, hidden=128, out_channels=1)  # 以ResNet18为例
    # 加载保存的模型参数
    model.load_state_dict(torch.load(model_path))
    return model

model = load_model('./client_1_main_global_model.pth')

i = 0
model.eval()
with torch.no_grad():
    for data, target in eval_loader:
        if (i == 100):
            break
        # data = data.unsqueeze(1)
        output = model(data)
        print(output)
        i += 1
rayrayraykk commented 5 months ago
You can use the following args in cfg file to load from ckpt. federate.restore_from (string) '' The checkpoint file to restore the model. -
federate.save_to (string) '' The path to save the model. -
junzlovegood commented 5 months ago

Thanks. I want to obtain the model's predicted output results for each input data (eval result). How should I modify the code?

rayrayraykk commented 5 months ago

Duplicated #764