Python 如何将Pytorch模型导入MATLAB

Python 如何将Pytorch模型导入MATLAB,python,matlab,neural-network,pytorch,Python,Matlab,Neural Network,Pytorch,我已经在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(T

我已经在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中,而无需手动复制所有权重


我正在运行Matlab2018b、Python3.6和Pytorch 0.4.0。我过去一直在使用这个工具,并取得了一些成功:从Pytorch到MatConvNet。importKerasLayers是否具有读取Pytorch权重的功能?我不确定,我能告诉你的最好消息是,无论你在上面放了什么结尾,torch.save都只是以pytorch格式保存,这就是为什么它不起作用的原因。无论如何,权重只是浮点数,所以最主要的是包含权重的结构。这就是为什么我想先把它转换成keras格式,这样matlab就可以理解它的结构。你看过onnx吗?我看过,但我不知道如何将onnx模型加载到matlab中。