{"id":71800,"student_id":10,"content":"// noprotect\nvar nn = new NeuralNet().init(625, 20, 10, 10);\n\nvar grid = [];\nfor (var x = 0; x \u003c 25; x++) {\n var row = [];\n for (var y = 0; y \u003c 25; y++) {\n row.push(0);\n }\n grid.push(row);\n}\n\nforever(() =\u003e {\n for (var x = 0; x \u003c 25; x++) {\n for (var y = 0; y \u003c 25; y++) {\n \n var deep = Math.floor((1 - grid[x][y])*256);\n pen.fillColor = `rgb(${deep},${deep},${deep})`;\n pen.drawRect(x*15, y*15, 15, 15);\n \n if (cursor.isDown) {\n var d = Math.sqrt((cursor.x - x*15 - 7.5)**2 + (cursor.y - y*15 - 7.5)**2);\n if (d \u003c 70 \u0026\u0026 1 - d/70 \u003e grid[x][y]) {\n grid[x][y] = 1 - d/70;\n }\n }\n }\n }\n});\n\n\nfunction clear () {\n for (var x = 0; x \u003c 25; x++) {\n for (var y = 0; y \u003c 25; y++) {\n grid[x][y] = 0;\n }\n }\n}\nwhen('keydown', 'c', clear);\n\n\n\n\nvar clock = 0;\nvar outputs = [];\nforever(function() {\n clock++;\n if (clock%30 == 0) {\n var inputs = [];\n grid.forEach((row) =\u003e {\n inputs = inputs.concat(row);\n });\n outputs = nn.update(inputs);\n }\n});\n\nforever(function() {\n var colors = outputs.map(function (o) {\n var deep = Math.floor(((o + 1)/2)*256);\n return `rgb(${255 - deep}, ${255 - deep}, ${255 - deep})`;\n });\n for (var i = 0; i \u003c 10; i++) {\n pen.fillColor = colors[i];\n pen.drawRect(500, i*30, 30, 30);\n print(i, 500, i*30, 'red');\n }\n var max = Math.max(...outputs);\n print('數字:' + outputs.indexOf(max), 500, 350, 'red');\n});\n\n\nvar data = [];\nwhen('keydown', 'a', () =\u003e {\n var inputs = [];\n grid.forEach((row) =\u003e {\n inputs = inputs.concat(row);\n });\n\n var target = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1];\n var n = Number(prompt('數字?'));\n target[n] = 1;\n data.push([inputs, target]);\n\n clear();\n});\n\nwhen('keydown', 't', function () {\n nn.train(data, 100, 0.2, 0.00);\n});","created_at":"2019-06-12T15:43:23.275+08:00","updated_at":"2019-11-11T15:23:39.230+08:00","name":"NN 手寫辨識","language":"javascript","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/71800/6930d91fa51a7f26b7857f98fa69ebb2.jpg"},"parent_id":2,"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\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\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\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":321,"hashid":"p93sm89k","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦