Unable to approximate the sine function using a neural network

后端 未结 4 1578
后悔当初
后悔当初 2020-11-30 02:58

I am trying to approximate the sine() function using a neural network I wrote myself. I have tested my neural network on a simple OCR problem already and it worked, but I am

4条回答
  •  难免孤独
    2020-11-30 03:17

    When you train the network, you should normalize the target (the sin function) to the range [0,1], then you can keep the sigmoid transfer function.

    sin(x) in [-1,1]  =>  0.5*(sin(x)+1) in [0,1]
    
    Train data:
        input    target    target_normalized
        ------------------------------------
        0         0          0.5
        pi/4      0.70711    0.85355
        pi/2      1           1
        ...
    

    Note that that we mapped the target before training. Once you train and simulate the network, you can map back the output of the net.


    The following is a MATLAB code to illustrate:

    %% input and target
    input = linspace(0,4*pi,200);
    target = sin(input) + 0.2*randn(size(input));
    
    % mapping
    [targetMinMax,mapping] = mapminmax(target,0,1);
    
    %% create network (one hidden layer with 6 nodes)
    net = newfit(input, targetMinMax, [6], {'tansig' 'tansig'});
    net.trainParam.epochs = 50;
    view(net)
    
    %% training
    net = init(net);                            % init
    [net,tr] = train(net, input, targetMinMax); % train
    output = sim(net, input);                   % predict
    
    %% view prediction
    plot(input, mapminmax('reverse', output, mapping), 'r', 'linewidth',2), hold on
    plot(input, target, 'o')
    plot(input, sin(input), 'g')
    hold off
    legend({'predicted' 'target' 'sin()'})
    

    network output

提交回复
热议问题