การฝึกอบรม Perceptron
- สร้างวัตถุ Perceptron
- สร้างฟังก์ชั่นการฝึกอบรม
- ฝึก perceptron กับคำตอบที่ต้องการ
งานฝึกอบรม
ลองนึกภาพเส้นตรงในช่องว่างที่มีจุด xy กระจัดกระจาย
ฝึก perceptron เพื่อจำแนกจุดเหนือและใต้เส้น
สร้างวัตถุ Perceptron
สร้างวัตถุ Perceptron ตั้งชื่ออะไรก็ได้ (เช่น Perceptron)
ให้ perceptron ยอมรับสองพารามิเตอร์:
- จำนวนอินพุต (ไม่มี)
- อัตราการเรียนรู้ (learningRate)
ตั้งค่าอัตราการเรียนรู้เริ่มต้นเป็น 0.00001
จากนั้นสร้างน้ำหนักสุ่มระหว่าง -1 ถึง 1 สำหรับแต่ละอินพุต
ตัวอย่าง
// Perceptron Object
function Perceptron(no, learningRate = 0.00001) {
// Set Initial Values
this.learnc = learningRate;
this.bias = 1;
// Compute Random Weights
this.weights = [];
for (let i = 0; i <= no; i++) {
this.weights[i] = Math.random() * 2 - 1;
}
// End Perceptron Object
}
น้ำหนักสุ่ม
Perceptron จะเริ่มต้นด้วยน้ำหนักแบบสุ่มสำหรับอินพุตแต่ละรายการ
อัตราการเรียนรู้
สำหรับข้อผิดพลาดแต่ละครั้ง ขณะฝึก Perceptron น้ำหนักจะถูกปรับด้วยเศษส่วนเล็กน้อย
เศษส่วนเล็ก ๆ นี้คือ " อัตราการเรียนรู้ของ Perceptron "
ในวัตถุ Perceptron เราเรียกว่าlearnc
อคติ
บางครั้ง ถ้าอินพุตทั้งสองมีค่าเป็นศูนย์ ตัวรับอาจสร้างเอาต์พุตที่ถูกต้อง
เพื่อหลีกเลี่ยงปัญหานี้ เราให้อินพุตพิเศษแก่ perceptron ด้วยค่า 1
สิ่งนี้เรียกว่าอคติ
เพิ่มฟังก์ชั่นเปิดใช้งาน
จำอัลกอริทึมของ perceptron:
- คูณอินพุตแต่ละรายการด้วยน้ำหนักของเพอร์เซปตรอน
- รวมผลลัพธ์
- คำนวณผลลัพธ์
ตัวอย่าง
this.activate = function(inputs) {
let sum = 0;
for (let i = 0; i < inputs.length; i++) {
sum += inputs[i] * this.weights[i];
}
if (sum > 0) {return 1} else {return 0}
}
ฟังก์ชันการเปิดใช้งานจะส่งออก:
- 1 ถ้าผลรวมมากกว่า 0
- 0 ถ้าผลรวมน้อยกว่า 0
สร้างฟังก์ชั่นการฝึกอบรม
ฟังก์ชันการฝึกอบรมจะเดาผลลัพธ์ตามฟังก์ชันเปิดใช้งาน
ทุกครั้งที่เดาผิด ผู้รับควรปรับน้ำหนัก
หลังจากการเดาและการปรับหลายครั้ง น้ำหนักจะถูกต้อง
ตัวอย่าง
this.train = function(inputs, desired) {
inputs.push(this.bias);
let guess = this.activate(inputs);
let error = desired - guess;
if (error != 0) {
for (let i = 0; i < inputs.length; i++) {
this.weights[i] += this.learnc * error * inputs[i];
}
}
}
การขยายพันธุ์หลัง
หลังจากการเดาแต่ละครั้ง perceptron จะคำนวณว่าการเดานั้นผิดอย่างไร
หากการเดาผิด Perceptron จะปรับความลำเอียงและน้ำหนักเพื่อให้การเดาถูกต้องมากขึ้นในครั้งต่อไป
การเรียนรู้ประเภทนี้เรียกว่า backpropagation
หลังจากลอง (สองสามพันครั้ง) การรับรู้ของคุณจะคาดเดาได้ดีทีเดียว
สร้างห้องสมุดของคุณเอง
รหัสห้องสมุด
// Perceptron Object
function Perceptron(no, learningRate = 0.00001) {
// Set Initial Values
this.learnc = learningRate;
this.bias = 1;
// Compute Random Weights
this.weights = [];
for (let i = 0; i <= no; i++) {
this.weights[i] = Math.random() * 2 - 1;
}
// Activate Function
this.activate = function(inputs) {
let sum = 0;
for (let i = 0; i < inputs.length; i++) {
sum += inputs[i] * this.weights[i];
}
if (sum > 0) {return 1} else {return 0}
}
// Train Function
this.train = function(inputs, desired) {
inputs.push(this.bias);
let guess = this.activate(inputs);
let error = desired - guess;
if (error != 0) {
for (let i = 0; i < inputs.length; i++) {
this.weights[i] += this.learnc * error * inputs[i];
}
}
}
// End Perceptron Object
}
ตอนนี้คุณสามารถรวมไลบรารีใน HTML:
<script src="myperceptron.js"></script>
ใช้ห้องสมุดของคุณ
ตัวอย่าง
// Initiate Values
const numPoints = 500;
const learningRate = 0.00001;
// Create a Plotter
const plotter = new XYPlotter("myCanvas");
plotter.transformXY();
const xMax = plotter.xMax;
const yMax = plotter.yMax;
const xMin = plotter.xMin;
const yMin = plotter.yMin;
// Create Random XY Points
const xPoints = [];
const yPoints = [];
for (let i = 0; i < numPoints; i++) {
xPoints[i] = Math.random() * xMax;
yPoints[i] = Math.random() * yMax;
}
// Line Function
function f(x) {
return x * 1.2 + 50;
}
//Plot the Line
plotter.plotLine(xMin, f(xMin), xMax, f(xMax), "black");
// Compute Desired Answers
const desired = [];
for (let i = 0; i < numPoints; i++) {
desired[i] = 0;
if (yPoints[i] > f(xPoints[i])) {desired[i] = 1}
}
// Create a Perceptron
const ptron = new Perceptron(2, learningRate);
// Train the Perceptron
for (let j = 0; j <= 10000; j++) {
for (let i = 0; i < numPoints; i++) {
ptron.train([xPoints[i], yPoints[i]], desired[i]);
}
}
// Display the Result
for (let i = 0; i < numPoints; i++) {
const x = xPoints[i];
const y = yPoints[i];
let guess = ptron.activate([x, y, ptron.bias]);
let color = "black";
if (guess == 0) color = "blue";
plotter.plotPoint(x, y, color);
}