neural network toolbox中的purelin函數是比較基礎也是比較重要的一種函數。purelin函數定義如下:
function n = purelin(n) %PURELIN Calculates a linear transfer. % % purelin(N) takes N neuron outputs and returns them unchanged. % % Here N is a matrix of net input (column) vectors. % % Purelin can be used as the transfer function in any network because it % simply takes the input and returns it as output without any transform. % Processing such networks consists of linear combinations of weight % and bias values. % % Purelin can also be used as the output function for networks that use other % transfer functions. % % A set of neurons with purelin output functions often serve as linear % classifiers if the weight and bias values have been properly set during % network design and training. % % Examples: % % Here is how to create a layer of 10 neurons with purelin neurons. % % net = feedforwardnet(10); % net.layers{1}.transferFcn = 'purelin'; % % Here is how to change an existing network to use purelin output neurons. % % net.outputFcn = 'purelin'; % % See also SIM, COMPETLAYER, HARDLIM, HARDLIMS, LOGSIG, NETSUM, POSLIN, SATLIN, SATLINS. % Mark Beale, 2003-12-04 % Revised 12-15-03, MB: Added example.
一、purelin函數剖析
在神經網絡中,可以通過不同的激活函數(activation function)來為每個神經元定義它的輸出。通常,這些激活函數是非線性的,有助於神經網絡更好地處理非線性問題。但是,purelin函數是一種線性激活函數,其輸出等於輸入。它的公式非常簡單:
n = purelin(n) n = n
這個函數只是把輸入原封不動地作為輸出,因此它可以分類器和一些線性網絡中充當輸出層激活函數使用。而對於一些非線性問題,可能不適合使用此函數。
二、intlinprog函數與purelin函數
在MATLAB中,有一個名為intlinprog的函數可以進行線性整數規劃。而該函數中也可以使用purelin函數作為約束條件(即線性函數)。其用法如下:
f = [20 30]; intcon = [1 2]; A = [-5 -10; -8 -12]; b = [-50; -60]; lb = zeros(2,1); [x,optval] = intlinprog(f,intcon,A,b,[],[],lb,[],[],optimoptions('intlinprog','Display','off'))
在上述代碼中,使用了一個純整數線性規劃模型。其中,f為目標函數,A和b是線性約束條件,intcon是整數變量的索引向量。就這個模型而言,整數線性規劃模型的解為:
x = [6;3]; optval = 270;
三、true函數python
在Python中,有一個名為true的函數可以進行布爾運算,該函數通常用於過濾列表或條件。在進行運算時,真值是以非零整數或非空字符串的形式表示的。當用purelin函數作為一個判斷條件時,其值為0就被當作false,非0值視為true。代碼示例:
sum = 100 price = 5 if purelin(sum - price): print("你可以購買此商品") else: print("餘額不足,請充值")
以上代碼的意思是,如果價格小於等於餘額,那麼可以購買此商品。而「purelin(sum – price)」這段代碼的輸出值就是0或1,判斷語句以此為基礎來執行下去。
四、purelin函數的應用
purelin函數主要應用在神經網絡分類器、線性網絡輸出層、MATLAB整數線性規劃等領域。例如,可以根據用戶的輸入創建一個簡單的線性模型,並將其用於分類。同時,整數線性規劃模型也可以使用purelin函數作為一個線性函數使用。
function [net, tr, res] = create_simple_linear_model(x_train, y_train, x_test, y_test) %create_simple_linear_model creates a simple linear model using purelin function as an output layer. % % Syntax: % [net, tr, res] = create_simple_linear_model(x_train, y_train, x_test, y_test) % % Description: % Given a training data x_train (n x p) and y_train (n x 1), this function creates a simple linear regression model for prediction. % The output layer uses purelin function as an activation function. % x_test and y_test are used to test the performance of the linear model. % % Examples: % % Here is how to use this function. % % x_train = [0 0; 0 1; 1 0; 1 1]; % y_train = [0, 1, 1, 0]; % x_test = [0 0; 0 1; 1 0; 1 1]; % y_test = [0, 1, 1, 0]; % [net, tr, res] = create_simple_linear_model(x_train, y_train, x_test, y_test); % % See also TRAIN, FEEDFORWARDNET. % Create a new network net = feedforwardnet(1); % Use purelin function as an output function net.layers{1}.transferFcn = 'purelin'; % Train the model [net, tr] = train(net, x_train', y_train'); % Evaluate the model res.y_predicted_train = net(x_train')'; res.r2_train = corr(y_train, res.y_predicted_train)^2; res.y_predicted_test = net(x_test')'; res.r2_test = corr(y_test, res.y_predicted_test)^2;
這個函數創建了一個簡單的線性分類模型,其中輸出層使用purelin函數作為激活函數而不是常見的sigmoid函數等。此函數的輸入數據包括x_train,y_train,x_test和y_test,將它們作為訓練集和測試集使用。函數返回了擬合的線性模型、訓練過程中的報告(提供了互動式、迭代式的過程展示)、以及模型在訓練集和測試集上的表現結果(即預測結果的r-squared值)。
總之,purelin函數是一種非常基礎的線性激活函數,主要應用於神經網絡分類器、線性網絡的輸出層與MATLAB整數線性規劃。當數據不適合使用非線性激活函數時,purelin函數是一種值得基於CNN、RNN等複雜模型進一步優化的方法。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/247610.html