【问题标题】:How can I reuse the same neural network to recreate the same results I had while training/creating the network?如何重复使用相同的神经网络来重新创建与训练/创建网络时相同的结果?
【发布时间】:2016-05-01 22:01:57
【问题描述】:

我刚刚训练了一个神经网络,我想用一个未包含在训练中的新数据集对其进行测试,以检查它在新数据上的性能。这是我的代码:

net = patternnet(30);
net = train(net,x,t);
save (net);
y = net(x);
perf = perform(net,t,y)
classes = vec2ind(y);

其中 x 和 t 分别是我的输入和目标。我知道save netload net;可以使用,但我的问题如下:

  1. 我应该在代码中的什么位置使用save net

  2. 使用save net;,训练好的网络保存在系统的哪个位置?

  3. 当我退出并再次打开 MATLAB 时,如何加载经过训练的网络并提供我想要对其进行测试的新数据?

请注意:我发现每次运行我的代码时,它都会给出不同的输出,一旦我得到可接受的结果,我就不想要这种输出。我希望能够保存经过训练的神经网络,这样当我使用训练数据集一遍又一遍地运行代码时,它会给出相同的输出。

【问题讨论】:

    标签: matlab neural-network pattern-recognition


    【解决方案1】:

    如果您只是调用save net,则工作区中的所有当前变量都将保存为net.mat。您只想保存经过训练的网络,因此您需要使用save('path_to_file', 'variable')。例如:

    save('C:\Temp\trained_net.mat','net');
    

    在这种情况下,网络将保存在给定的文件名下。

    下次您想使用保存的预训练网络时,只需致电load('path_to_file')。如果您不重新初始化或再次训练此网络,则性能将与以前相同,因为所有权重和偏差值都将相同。

    您可以通过检查net.IW{i,j}(输入权重)、net.LW{i,j}(层权重)和net.b{i}(偏差)等变量来查看使用的权重和偏差值。只要它们保持不变,网络的性能就会保持不变。

    训练和保存

    [x,t] = iris_dataset;
    net = patternnet;
    net = configure(net,x,t);
    
    net = train(net,x,t); 
    save('C:\Temp\trained_net.mat','net');
    
    y = net(x);
    
    perf = perform(net,t,y);
    display(['performance: ', num2str(perf)]);
    

    在我的情况下它返回performance: 0.11748。每次新训练后值都会有所不同。

    加载和使用

    clear;
    [x,t] = iris_dataset;
    load('C:\Temp\trained_net.mat');
    
    y = net(x);
    perf = perform(net,t,y);
    display(['performance: ', num2str(perf)]);
    

    它返回performance: 0.11748。在同一数据集上使用网络时,这些值将相同。这里我们再次使用了训练集。

    如果你得到一个全新的数据集,性能会有所不同,但对于这个特定的数据集,它总是一样的。

    clear;
    
    [x,t] = iris_dataset;
    
    %simulate a new data set of size 50
    data_set = [x; t];
    data_set = data_set(:,randperm(size(data_set,2)));
    x = data_set(1:4, 1:50);
    t = data_set(5:7, 1:50);
    
    load('C:\Temp\trained_net.mat');
    
    y = net(x);
    perf = perform(net,t,y);
    display(['performance: ', num2str(perf)]);
    

    在我的情况下它返回performance: 0.12666

    【讨论】:

      猜你喜欢
      • 2016-12-23
      • 2017-09-08
      • 2019-05-19
      • 2020-06-21
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 2021-01-13
      • 2019-03-01
      相关资源
      最近更新 更多