如何将Pytorch模型导入MATLAB

3

我在Pytorch中创建了一个模型,现在希望将其转移到MATLAB中,以下是一个简单的示例:

import torch.nn as nn
import torch
class cnn(nn.Module):
    def __init__(self):
        super(cnn, self).__init__()
        self.fc1 = nn.Sequential(
            nn.Linear(10, 1),
            nn.ReLU(True)
        )

    def forward(self, x):
        out = self.fc1(x)
        return out
the_net = cnn()
torch.save(the_net,'desperation.h5')

在 MATLAB 中,我调用以下代码:
net = importKerasLayers('desperation.h5')

这会产生错误消息。
Error using importKerasLayers (line 104)
Unable to read HDF5 file 'desperation.h5'. The error message was: 'The filename specified was either
not found on the MATLAB path or it contains unsupported characters.''

文件在路径上,我可以将模型重新加载到Python中。我真正想要的是任何解决方案,允许我将模型从Pytorch转移到MATLAB而不需要手动复制所有权重。
我正在运行MATLAB 2018b、Python 3.6和Pytorch 0.4.0。

importKerasLayers是否具有读取PyTorch权重的功能? - Ryan
我不确定,但据我所知,torch.save无论你在上面放什么结尾,它都只是以pytorch格式保存,这就是为什么它不能正常工作的原因。无论如何,权重只是浮点数,所以主要的是包含权重的结构。这就是为什么我想先将其转换为keras格式,以便matlab可以理解结构的原因。 - Durkee
你看过ONNX吗? - Shai
我有ONNX模型,但是我不知道如何将其加载到MATLAB中。 - Durkee
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接