{"id":71843,"student_id":10,"content":"var a = createSprite(\"b.png\");\na.moveTo(100, 50);\n\nvar b = createSprite(\"b.png\");\nb.direction = 0;\nb.moveTo(40, 110);\n\nvar c = createSprite(\"b.png\");\nc.direction = 0;\nc.moveTo(160, 110);\n\nvar d = createSprite(\"b.png\");\nd.moveTo(100, 170);\n\nvar e = createSprite(\"b.png\");\ne.direction = 0;\ne.moveTo(40, 230);\n\nvar f = createSprite(\"b.png\");\nf.direction = 0;\nf.moveTo(160, 230);\n\nvar g = createSprite(\"b.png\");\ng.moveTo(100, 290);\n\nvar blocks = [a, b, c, d, e, f, g];\n\nvar data = [\n [[1, 1, 1, 0, 1, 1, 1], [1,-1,-1,-1,-1,-1,-1,-1,-1,-1]], // 0\n [[0, 0, 1, 0, 0, 1, 0], [-1,1,-1,-1,-1,-1,-1,-1,-1,-1]], // 1\n [[1, 0, 1, 1, 1, 0, 1], [-1,-1,1,-1,-1,-1,-1,-1,-1,-1]], // 2\n [[1, 0, 1, 1, 0, 1, 1], [-1,-1,-1,1,-1,-1,-1,-1,-1,-1]], // 3\n [[0, 1, 1, 1, 0, 1, 0], [-1,-1,-1,-1,1,-1,-1,-1,-1,-1]], // 4\n [[1, 1, 0, 1, 0, 1, 1], [-1,-1,-1,-1,-1,1,-1,-1,-1,-1]], // 5\n [[1, 1, 0, 1, 1, 1, 1], [-1,-1,-1,-1,-1,-1,1,-1,-1,-1]], // 6\n [[1, 1, 1, 0, 0, 1, 0], [-1,-1,-1,-1,-1,-1,-1,1,-1,-1]], // 7\n [[1, 1, 1, 1, 1, 1, 1], [-1,-1,-1,-1,-1,-1,-1,-1,1,-1]], // 8\n [[1, 1, 1, 1, 0, 1, 0], [-1,-1,-1,-1,-1,-1,-1,-1,-1,1]], // 9\n];\n\n\nvar nn = new NeuralNet().init(7, 4, 4, 10);\nnn.train(data, 1000, 0.02, 0.03);\n\n\nblocks.forEach((b) =\u003e {\n b.on('click', () =\u003e {\n b.opacity = b.opacity == 1 ? 0.2 : 1; \n });\n});\n\n\nforever(function() {\n var inputs = blocks.map((b) =\u003e {\n return b.opacity == 1 ? 1: 0;\n });\n var output = nn.update(inputs);\n \n for (var i=0; i\u003c10; i++) {\n print(output[i], 400, 200 + 15*i);\n }\n print(output.indexOf(Math.max(...output)), 400, 400, 'red');\n});\n\n\n\n","created_at":"2019-06-14T14:35:51.376+08:00","updated_at":"2019-11-11T14:05:51.322+08:00","name":"七段顯示器辨識 7*4*4*10","language":"javascript","screenshot":{"url":"https://cdn0.koding.school/uploads/project/screenshot/71843/2a5fa01b64ef273d98988cfa7a81aabe.jpg"},"parent_id":71802,"plugin":"// noprotect\n\nfunction sigmoid(x) {\n return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));\n}\n\nfunction dsigmoid(x) {\n return 1.0 - x*x;\n}\n\n\n// 建立大小為 n 的陣列並填入初始值 fill\nvar makeArray = function(n, fill) {\n var a = [];\n for (var i = 0; i \u003c n; i++)\n a.push(fill);\n return a;\n}\n\n// 建立大小為 I*J 的矩陣並填入初始值 fill\nvar makeMatrix = function(I, J, fill) {\n var m = [];\n for (var i = 0; i \u003c I; i++)\n m.push(makeArray(J, fill));\n return m;\n}\n\n// numbersToStr():以精確度為 precision 個小數來輸出陣列 array\nvar numbersToStr = function(array, precision) {\n var rzStr = \"\";\n for (var i = 0; i \u003c array.length; i++) {\n if (array[i] \u003e= 0)\n rzStr += \" \"+array[i].toFixed(precision)+\" \";\n else\n rzStr += array[i].toFixed(precision)+\" \";\n }\n return rzStr;\n}\n\n// rand():取得 a 到 b 之間的一個隨機亂數\nvar rand = function(a, b) {\n return (b-a)*Math.random() + a;\n}\n\n// sigmoid(x)=tanh(x)\nfunction sigmoid(x) {\n var tanh = (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));\n return tanh; // 雙曲正切函數\n}\n\n// dsigmoid(x)=1-x^2;\n// 參考:http://pynopticon.googlecode.com/svn/trunk/src/vlfeat/toolbox/special/dsigmoid.m\n// 參考:http://en.wikipedia.org/wiki/Sigmoid_function\nfunction dsigmoid(x) {\n return 1.0 - x*x;\n}\n\nfunction NeuralNet() {\n\n this.init = function(n1, n2, n3, n4) {\n\n this.n1 = n1;\n this.n2 = n2;\n this.n3 = n3;\n this.n4 = n4;\n\n this.a1 = makeArray(this.n1, 1.0);\n this.a2 = makeArray(this.n2, 1.0);\n this.a3 = makeArray(this.n3, 1.0);\n this.a4 = makeArray(this.n4, 1.0);\n\n this.w12 = makeMatrix(this.n1, this.n2, 0.0);\n this.w23 = makeMatrix(this.n2, this.n3, 0.0);\n this.w34 = makeMatrix(this.n3, this.n4, 0.0);\n\n for (var i = 0; i \u003c this.n1; i++)\n for (var j = 0; j \u003c this.n2; j++)\n this.w12[i][j] = rand(-0.3, 0.3);\n\n for (var i = 0; i \u003c this.n2; i++)\n for (var j = 0; j \u003c this.n3; j++)\n this.w23[i][j] = rand(-0.3, 0.3);\n\n for (var i = 0; i \u003c this.n3; i++)\n for (var j = 0; j \u003c this.n4; j++)\n this.w34[i][j] = rand(-0.3, 0.3);\n\n this.c12 = makeMatrix(this.n1, this.n2, 0.0);\n this.c23 = makeMatrix(this.n2, this.n3, 0.0);\n this.c34 = makeMatrix(this.n3, this.n4, 0.0);\n\n return this;\n }\n\n this.update = function(inputs) {\n for (var i = 0; i \u003c this.n1; i++) {\n this.a1[i] = inputs[i];\n }\n for (var j = 0; j \u003c this.n2; j++) {\n var sum = 0;\n for (var i = 0; i \u003c this.n1; i++) {\n sum += this.a1[i]*this.w12[i][j];\n }\n this.a2[j] = sigmoid(sum);\n }\n for (var j = 0; j \u003c this.n3; j++) {\n var sum = 0;\n for (var i = 0; i \u003c this.n2; i++) {\n sum += this.a2[i]*this.w23[i][j];\n }\n this.a3[j] = sigmoid(sum);\n }\n for (var j = 0; j \u003c this.n4; j++) {\n var sum = 0;\n for (var i = 0; i \u003c this.n3; i++) {\n sum += this.a3[i]*this.w34[i][j];\n }\n \n this.a4[j] = sigmoid(sum);\n }\n\n return this.a4;\n }\n\n this.backPropagate = function(targets, rate, moment) {\n\n var deltas4 = makeArray(this.n4, 0);\n for (var k = 0; k \u003c this.n4; k++) {\n var error = targets[k]-this.a4[k];\n deltas4[k] = dsigmoid(this.a4[k])*error;\n }\n\n var deltas3 = makeArray(this.n3, 0);\n for (var j = 0; j \u003c this.n3; j++) {\n var error = 0;\n for (var k = 0; k \u003c this.n4; k++) {\n error += deltas4[k]*this.w34[j][k];\n }\n deltas3[j] = dsigmoid(this.a3[j])*error;\n }\n\n var deltas2 = makeArray(this.n2, 0);\n for (var j = 0; j \u003c this.n2; j++) {\n var error = 0;\n for (var k = 0; k \u003c this.n3; k++) {\n error += deltas3[k]*this.w23[j][k];\n }\n deltas2[j] = dsigmoid(this.a2[j]) * error;\n }\n\n\n for (var j = 0; j \u003c this.n3; j++) {\n for (var k = 0; k \u003c this.n4; k++) {\n var change = deltas4[k]*this.a3[j];\n this.w34[j][k] += rate*change + moment*this.c34[j][k];\n this.c34[j][k] = change;\n }\n }\n for (var j = 0; j \u003c this.n2; j++) {\n for (var k = 0; k \u003c this.n3; k++) {\n var change = deltas3[k]*this.a2[j];\n this.w23[j][k] += rate*change + moment*this.c23[j][k];\n this.c23[j][k] = change;\n }\n }\n for (var j = 0; j \u003c this.n1; j++) {\n for (var k = 0; k \u003c this.n2; k++) {\n var change = deltas2[k]*this.a1[j];\n this.w12[j][k] += rate*change + moment*this.c12[j][k];\n this.c12[j][k] = change;\n }\n }\n }\n\n\n this.train = function(patterns, iterations, rate, moment) {\n for (var i = 0; i \u003c iterations; i++) {\n var error = 0.0;\n for (var p in patterns) {\n var pat = patterns[p];\n var inputs = pat[0];\n var targets = pat[1];\n var outputs = this.update(inputs);\n error = error + this.backPropagate(targets, rate, moment);\n }\n }\n }\n}","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":375,"hashid":"qmdsd5n3","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":1254611,"file_name":"b.png","project_id":71843,"asset_id":125966,"created_at":"2019-06-14T14:35:51.381+08:00","updated_at":"2019-06-14T14:35:51.381+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦