﻿{"id":3293,"date":"2025-08-27T15:37:48","date_gmt":"2025-08-27T07:37:48","guid":{"rendered":"https:\/\/www.leexinghai.com\/aic\/?p=3293"},"modified":"2025-08-27T15:40:51","modified_gmt":"2025-08-27T07:40:51","slug":"3-9%e7%94%a8matlab%e7%bc%96%e7%a8%8b%e6%9e%84%e5%bb%ba%e5%a4%9a%e5%b1%82%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%e5%88%86%e7%b1%bb%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.leexinghai.com\/aic\/3-9%e7%94%a8matlab%e7%bc%96%e7%a8%8b%e6%9e%84%e5%bb%ba%e5%a4%9a%e5%b1%82%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%e5%88%86%e7%b1%bb%e5%99%a8\/","title":{"rendered":"3.9\u7528MATLAB\u7f16\u7a0b\u6784\u5efa\u591a\u5c42\u795e\u7ecf\u7f51\u7edc\u5206\u7c7b\u5668"},"content":{"rendered":"\n<p><code>nn_testChess.m<\/code><\/p>\n\n\n\n<p>\u8bfb\u5165\u6570\u636e\uff08one-hot\u4e24\u7c7b\u60c5\u5f62\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if string(13) == 100\n            yapp = &#91;yapp,&#91;1,0]'];\n        else\n            yapp = &#91;yapp,&#91;0,1]'];\n        end;<\/code><\/pre>\n\n\n\n<p>\u5212\u5206\u6570\u636e\u96c6\u5206\u914d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ratioTraining = 0.15;    % 15%\u8bad\u7ec3\nratioValidation = 0.05;  % 5%\u9a8c\u8bc1\nratioTesting = 0.8;      % 80%\u6d4b\u8bd5<\/code><\/pre>\n\n\n\n<p>\u5206\u522b\u5bf9\u8bad\u7ec3\u3001\u9a8c\u8bc1\u3001\u6d4b\u8bd5\u6570\u636e\u96c6\u8fdb\u884c\u5f52\u4e00\u5316\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>xTraining = (xTraining - repmat(avgX,U,1)).\/repmat(sigma,U,1);\nxValidation = (xValidation - repmat(avgX,U,1)).\/repmat(sigma,U,1);\nxTesting = (xTesting - repmat(avgX,U,1)).\/repmat(sigma,U,1);<\/code><\/pre>\n\n\n\n<p>\u6574\u4e2a\u795e\u7ecf\u7f51\u7edc\u7684\u6784\u5efa\u3001\u8bad\u7ec3\u548c\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nn = nn_create(&#91;6,10,10,10,10,10,10,10,10,10,10,2],'active function','relu','learning rate',0.005, 'batch normalization',1,'optimization method','Adam', 'objective function', 'Cross Entropy');\n%\u6570\u7ec4\u4ee3\u8868\u4e86\u795e\u7ecf\u7f51\u7edc\u6bcf\u4e00\u5c42\u4e2d\u7684\u795e\u7ecf\u5143\u4e2a\u6570\u3002\u5b66\u4e60\u7387\u03b1\uff1alearning rate\u3001\u76ee\u6807\u51fd\u6570\uff1aobjective function\u3001\u795e\u7ecf\u7f51\u7edc\u7684\u6fc0\u6d3b\u51fd\u6570\uff1aactive function\uff1b\u8f93\u5165\u662f6\u4e2a\u7ef4\u5ea6\uff0c\u8f93\u51fa\u662f2\u4e2a\u7ef4\u5ea6\uff0c10\u5c42\uff0c\u6bcf\u5c42\u670910\u4e2a\u795e\u7ecf\u5143\u3002<\/code><\/pre>\n\n\n\n<p>\u5176\u4ed6\u89e3\u8bfb\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>option.batch_size = 100;%\u6bcf\u4e2amini-batch\u4e2d\u6709100\u4e2a\u8bad\u7ec3\u6837\u672c\nmaxIteration = 10000; %\u6700\u5927\u8bad\u7ec3\u8f6e\u6b2110000\u8f6e\nnn = nn_train(nn,option,xTraining,yTraining);%\u8bad\u7ec3\ntotalCost(iteration) = sum(nn.cost)\/length(nn.cost);%\u6d4b\u8bd5\u5e73\u5747\u7684\u635f\u5931\u51fd\u6570\n&#91;wrongs,accuracy] = nn_test(nn,xValidation,yValidation); %\u5728\u9a8c\u8bc1\u96c6\u6d4b\u8bd5\u8bc6\u522b\u7387<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6df1\u5165\u7a0b\u5e8f\u5185\u90e8\uff1a<\/p>\n\n\n\n<p><code>nn_forward.m<\/code><\/p>\n\n\n\n<p>\u524d\u5411\u8ba1\u7b97\uff1a<\/p>\n\n\n\n<p>\u4ece\u7b2cK-1\u5c42\u5230\u7b2cK\u5c42\u7684\u8f93\u51fa\u8fc7\u7a0b\uff1aK-1\u5c42\u8f93\u51fa*\u6743\u91cd\u77e9\u9635\u03c9+\u504f\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>y = nn.W{k-1} * nn.a{k-1} + repmat(nn.b{k-1},1,m);\n%repmat(A,m,n)\u5c06A\u590d\u5236m\u00d7n\u5757\n        %\u7531\u4e8e\u8fdb\u884c\u6279\u5904\u7406\uff0c\u5c06m\u7ec4\u6570\u636e\u5b58\u5728\u77e9\u9635\u540c\u65f6\u5904\u7406\uff0c\u800c\u5bf9\u6bcf\u7ec4\u6570\u636e\u6765\u8bf4\u9608\u503c\u8bbe\u5b9a\u662f\u76f8\u540c\u7684\uff0c\u6545\u5c06b\u590d\u5236m\u6b21\n        %\u6b64\u5904y\u5373\u4e3a\u6240\u7ed9\u63a8\u5bfc\u65b9\u6cd5\u4e2d\u7684z.<\/code><\/pre>\n\n\n\n<p>\u7ecf\u8fc7\u975e\u7ebf\u6027\u51fd\u6570\u83b7\u5f97\u7b2cK\u5c42\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> switch nn.active_function%\u9690\u5c42\u6fc0\u6d3b\u51fd\u6570\u9009\u62e9\n                case 'sigmoid'\n                    nn.a{k} = sigmoid(y);\n                case 'tanh'\n                    nn.a{k} = tanh(y);\n                case 'relu'\n                    nn.a{k} = max(y,0);\n            end<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u540e\u5411\u4f20\u64ad\uff08\u6839\u636e\u524d\u9762\u5b66\u8fc7\u7684SIGMOID,TANH,SOFTMAX+CROSSENTROPY\uff09\uff1a<\/p>\n\n\n\n<p><code>nn_backpropagation.m<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>switch nn.output_function \n        case 'sigmoid'\n            nn.theta{nn.depth} = -(batch_y-nn.a{nn.depth}) .* nn.a{nn.depth} .* (1 - nn.a{nn.depth});\n        case 'tanh'\n            nn.theta{nn.depth} = -(batch_y-nn.a{nn.depth}) .* (1 - nn.a{nn.depth}.^2);\n        case 'softmax'\n            nn.theta{nn.depth} = nn.a{nn.depth} - batch_y;\n    end<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u591a\u5c42\u795e\u7ecf\u7f51\u7edc\u53c2\u6570\u7684\u66f4\u65b0\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<p><code>nn_applygradient.m<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if strcmp(nn.optimization_method, 'normal')\n            nn.W{k} = nn.W{k} - nn.learning_rate*nn.W_grad{k};\n            nn.b{k} = nn.b{k} - nn.learning_rate*nn.b_grad{k};<\/code><\/pre>\n\n\n\n<p>\u4e0e3.7\u7b2c2\u9875\u4e2d\u603b\u7ed3\uff084\uff09\u8fed\u4ee3\u516c\u5f0f\u4e00\u6837\u3002<\/p>\n\n\n\n<p>\u5fd8\u8bb0\u4e86\uff1f\u770b\u770b\ud83d\udc47<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"LuSk7xZmkP\"><a href=\"https:\/\/www.leexinghai.com\/aic\/3-7%e5%90%8e%e5%90%91%e4%bc%a0%e6%92%ad%e7%ae%97%e6%b3%95\/\">3.7\u540e\u5411\u4f20\u64ad\u7b97\u6cd5<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"\u300a 3.7\u540e\u5411\u4f20\u64ad\u7b97\u6cd5 \u300b\u2014\u5b66\u672f\u521b\u65b0\u4e2d\u5fc3\" src=\"https:\/\/www.leexinghai.com\/aic\/3-7%e5%90%8e%e5%90%91%e4%bc%a0%e6%92%ad%e7%ae%97%e6%b3%95\/embed\/#?secret=VLAsDql9RX#?secret=LuSk7xZmkP\" data-secret=\"LuSk7xZmkP\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>nn_testChess.m \u8bfb\u5165\u6570\u636e\uff08one-hot\u4e24\u7c7b\u60c5\u5f62\uff09\uff1a \u5212\u5206\u6570\u636e\u96c6\u5206\u914d\uff1a \u5206\u522b\u5bf9\u8bad\u7ec3\u3001\u9a8c\u8bc1\u3001\u6d4b\u8bd5\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3045,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63],"tags":[],"class_list":["post-3293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mlsh"],"_links":{"self":[{"href":"https:\/\/www.leexinghai.com\/aic\/wp-json\/wp\/v2\/posts\/3293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.leexinghai.com\/aic\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.leexinghai.com\/aic\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.leexinghai.com\/aic\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.leexinghai.com\/aic\/wp-json\/wp\/v2\/comments?post=3293"}],"version-history":[{"count":5,"href":"https:\/\/www.leexinghai.com\/aic\/wp-json\/wp\/v2\/posts\/3293\/revisions"}],"predecessor-version":[{"id":3298,"href":"https:\/\/www.leexinghai.com\/aic\/wp-json\/wp\/v2\/posts\/3293\/revisions\/3298"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.leexinghai.com\/aic\/wp-json\/wp\/v2\/media\/3045"}],"wp:attachment":[{"href":"https:\/\/www.leexinghai.com\/aic\/wp-json\/wp\/v2\/media?parent=3293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.leexinghai.com\/aic\/wp-json\/wp\/v2\/categories?post=3293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.leexinghai.com\/aic\/wp-json\/wp\/v2\/tags?post=3293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}