Deploy mạng Shallow Neural Network trên phần mềm Matlab


Hàm genFunction cho phép deploy một mạng nơ-ron đã được huấn luyện để chạy độc lập. Mã được tạo chứa tất cả thông tin cần thiết để mô phỏng mạng thần kinh, bao gồm cài đặt, giá trị trọng số và độ lệch, các mô-đun chức năng và tính toán.

Hàm MATLAB đã tạo có thể được sử dụng để kiểm tra các tính toán mô phỏng chính xác mà một mạng nơ-ron cụ thể thực hiện và giúp triển khai mạng nơ-ron cho nhiều mục đích dễ dàng hơn với nhiều sản phẩm và công cụ triển khai MATLAB.

Chức năng genFunction được sử dụng bởi các ứng dụng Lắp mạng thần kinh, Nhận dạng mẫu mạng thần kinh, Phân cụm mạng thần kinh và Chuỗi thời gian mạng thần kinh. Để biết thông tin về các ứng dụng này, hãy xem Fit Data with a Shallow Neural Network, Pattern Recognition with a Shallow Neural Network, Cluster Data with a Self-Organizing Map và Dự đoán và lập mô hình chuỗi thời gian của mạng nơ-ron nông.

Các tập lệnh toàn diện được tạo bởi các ứng dụng này bao gồm một ví dụ về triển khai mạng với genFunction.


Generate Neural Network Functions for Application Deployment

Hàm genFunction tạo ra  hàm MATLAB độc lập mô phỏng bất kỳ mạng shallow neural network nào đã được huấn luyện và  preparing it for deployment. Điều này có ý nghĩa khi các bạn thực hiện 1 số tasks sau:

  • Document the input-output transforms of a neural network used as a calculation template for manual reimplementations of the network

  • Use the MATLAB Function block to create a Simulink® block

  • Use MATLAB Compiler™ to:

    • Generate stand-alone executables

    • Generate Excel® add-ins

  • Use MATLAB Compiler SDK™ to:

    • Tạo thư viện C/C++ 

    • Tạo đối tượng .COM

    • Tạo đối tượng Java® 

    • Tạo đối tượng .NET 

  • Sử dụng MATLAB Coder™ để:

    • Tạo code C/C++ 

    • Tạo hàm MEX-functions

genFunction(net,'pathname') takes a neural network and file path, and produces a standalone MATLAB function file filename.m.

genFunction(...,'MatrixOnly','yes') overrides the default cell/matrix notation and instead generates a function that uses only matrix arguments compatible with MATLAB Coder tools. For static networks, the matrix columns are interpreted as independent samples. For dynamic networks, the matrix columns are interpreted as a series of time steps. The default value is 'no'.

genFunction(___,'ShowLinks','no') disables the default behavior of displaying links to generated help and source code. The default is 'yes'.

- Ví dụ đây là 1 mạng nơ-ron bodyfatNet đã được huấn luyện;

[x, t] = bodyfat_dataset;
bodyfatNet = feedforwardnet(10);
bodyfatNet = train(bodyfatNet, x, t);
y = bodyfatNet(x);

Đoạn code dưới đây sử dụng hàm  genFunction tạo ra hàm bodyfatFcn test thử và hiển thị hàm Matlab đã tạo ra:

genFunction(bodyfatNet, 'bodyfatFcn');
y2 = bodyfatFcn(x);
accuracy2 = max(abs(y - y2))
edit bodyfatFcn


You can compile the new function with the MATLAB Compiler tools (license required) to a shared/dynamically linked library with mcc.

mcc -W lib:libBodyfat -T link:lib bodyfatFcn

The next code generates another version of the MATLAB function that supports only matrix arguments (no cell arrays). This function is tested. Then it is used to generate a MEX-function with the MATLAB Coder tool codegen (license required), which is also tested.

genFunction(bodyfatNet, 'bodyfatFcn', 'MatrixOnly', 'yes');
y3 = bodyfatFcn(x);
accuracy3 = max(abs(y - y3))

x1Type = coder.typeof(double(0), [13, Inf]); % Coder type of input 1
codegen bodyfatFcn.m -config:mex -o bodyfatCodeGen -args {x1Type}
y4 = bodyfatCodeGen(x);
accuracy4 = max(abs(y - y4))

Here a dynamic network is trained and its outputs calculated.

[x,t] = maglev_dataset;
maglevNet = narxnet(1:2,1:2,10);
[X,Xi,Ai,T] = preparets(maglevNet,x,{},t);
maglevNet = train(maglevNet,X,T,Xi,Ai);
[y,xf,af] = maglevNet(X,Xi,Ai);

Next a MATLAB function is generated and tested. The function is then used to create a shared/dynamically linked library with mcc.
genFunction(maglevNet,'maglevFcn');
[y2,xf,af] = maglevFcn(X,Xi,Ai);
accuracy2 = max(abs(cell2mat(y)-cell2mat(y2)))
mcc -W lib:libMaglev -T link:lib maglevFcn

Đoạn code dưới đây tạo rates another version of the MATLAB function that supports only matrix arguments (no cell arrays). This function is tested. Then it is used to generate a MEX-function with the MATLAB Coder tool codegen, which is also tested.

genFunction(maglevNet,'maglevFcn','MatrixOnly','yes');
x1 = cell2mat(X(1,:)); % Convert each input to matrix
x2 = cell2mat(X(2,:));
xi1 = cell2mat(Xi(1,:)); % Convert each input state to matrix
xi2 = cell2mat(Xi(2,:));
[y3,xf1,xf2] = maglevFcn(x1,x2,xi1,xi2);
accuracy3 = max(abs(cell2mat(y)-y3))

x1Type = coder.typeof(double(0),[1 Inf]); % Coder type of input 1
x2Type = coder.typeof(double(0),[1 Inf]); % Coder type of input 2
xi1Type = coder.typeof(double(0),[1 2]); % Coder type of input 1 states
xi2Type = coder.typeof(double(0),[1 2]); % Coder type of input 2 states
codegen maglevFcn.m -config:mex -o maglevNetCodeGen ...
                    -args {x1Type x2Type xi1Type xi2Type}
[y4,xf1,xf2] = maglevNetCodeGen(x1,x2,xi1,xi2);
dynamic_codegen_accuracy = max(abs(cell2mat(y)-y4)


0 nhận xét:

Cài đặt Matlab 2018

Cảm nhận Học viên

Matlabthayhai. Powered by Blogger.

Sản phẩm sinh viên