【问题标题】:Saving and reading huge structs in matlab?在matlab中保存和读取巨大的结构?
【发布时间】:2012-12-05 02:30:06
【问题描述】:

在matlab中,我有一个程序是this implementation的线性支持向量机做机器学习。

库在训练阶段输出一个表示模型的结构。此结构将用于测试阶段。

我想将此结构保存到一个文件中,这样我就不必每次都经历训练阶段。

我尝试过psychtoolboxWriteStructsToText()ReadStructsFromText(),但由于在将结构读入内存时缓冲区溢出,它们失败了。

输出的结构是非常大量的数据(几十兆字节),所以这可能是问题所在。

结构:

    -Parameters: Parameters
    -nr_class: number of classes; = 2 for regression
    -nr_feature: number of features in training data (without including the bias term)
    -bias: If >= 0, we assume one additional feature is added to the end
        of each data instance.
    -Label: label of each class; empty for regression
    -w: a nr_w-by-n matrix for the weights, where n is nr_feature
        or nr_feature+1 depending on the existence of the bias term.
        nr_w is 1 if nr_class=2 and -s is not 4 (i.e., not
        multi-class svm by Crammer and Singer). It is
        nr_class otherwise.

编辑:

有人知道如何解决这个错误吗?

>> save('mode.txt','-struct','model');
>> model = load('mode.txt');
??? Error using ==> load
Number of columns on line 1 of ASCII file C:\Users\jason\Dropbox\Homework\cs280\FINAL PROJECTO\mode.txt
must be the same as previous lines.

【问题讨论】:

    标签: matlab serialization struct save


    【解决方案1】:

    最简单的方法是将结构保存到 .mat 文件:

    outputStructure = yourTrainingFunction;
    
    save('someFileName.mat','-struct','outputStructure');
    

    然后加载

    outputStructure = load('someFileName.mat')
    

    请注意,如果您的文件非常大,您可能必须在 Matlab 的常规设置中设置适当的兼容性(如果我没记错的话,7.3 之前的保存文件版本无法处理 >2GB 的文件)。

    【讨论】:

    • 我收到一个错误:>> save('mode.txt','-struct','model'); >> 模型 = 加载('mode.txt'); ???错误使用 ==> 加载 ASCII 文件 C:\Users\jason\Dropbox\Homework\cs280\FINAL PROJECTO\mode.txt 的第 1 行的列数必须与前面的行相同。
    • @RazorStorm:如果你保存为 .mat 文件,你不应该给它一个 .txt 扩展名,否则,Matlab 会尝试将其读取为 ascii。
    猜你喜欢
    • 2015-11-14
    • 1970-01-01
    • 2017-11-29
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多