-
Notifications
You must be signed in to change notification settings - Fork 19
/
perceptron.html
160 lines (143 loc) · 5.55 KB
/
perceptron.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simulação Perceptron AND - Entrada Individual</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
.container {
max-width: 900px;
margin: 0 auto;
padding: 20px;
}
input[type="number"] {
width: 50px;
padding: 5px;
margin: 0 5px;
}
button {
padding: 10px 20px;
margin-top: 20px;
cursor: pointer;
}
.network {
display: flex;
justify-content: center;
margin: 20px 0;
position: relative;
}
.node {
border-radius: 50%;
width: 50px;
height: 50px;
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0;
margin: 0 10px;
font-weight: bold;
}
.highlight {
color: red;
font-weight: bold;
}
svg {
width: 500px;
height: 300px;
margin: 20px auto;
}
.line {
stroke: #000;
stroke-width: 2;
}
.text-flow {
font-size: 14px;
font-weight: bold;
}
.result-text {
font-size: 18px;
font-weight: bold;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>Simulação de Treinamento Perceptron - Função AND</h1>
<label for="w1">Peso inicial w1: </label>
<input type="number" id="w1" value="0.6" step="0.1">
<label for="w2">Peso inicial w2: </label>
<input type="number" id="w2" value="0.6" step="0.1">
<br>
<label for="x1">Entrada x1: </label>
<input type="number" id="x1" value="0" min="0" max="1">
<label for="x2">Entrada x2: </label>
<input type="number" id="x2" value="0" min="0" max="1">
<br>
<button onclick="runSingleInput()">Enviar Entrada</button>
<svg viewBox="0 0 500 300">
<!-- Entradas -->
<circle cx="100" cy="100" r="30" fill="#f0f0f0" stroke="#000" stroke-width="2"/>
<text x="90" y="105" class="text-flow" id="input1">x1</text>
<circle cx="100" cy="200" r="30" fill="#f0f0f0" stroke="#000" stroke-width="2"/>
<text x="90" y="205" class="text-flow" id="input2">x2</text>
<!-- Pesos -->
<circle cx="250" cy="100" r="30" fill="#f0f0f0" stroke="#000" stroke-width="2"/>
<text x="240" y="105" class="text-flow" id="weight1">w1</text>
<circle cx="250" cy="200" r="30" fill="#f0f0f0" stroke="#000" stroke-width="2"/>
<text x="240" y="205" class="text-flow" id="weight2">w2</text>
<!-- Soma ponderada (z) e Saída -->
<circle cx="400" cy="150" r="30" fill="#f0f0f0" stroke="#000" stroke-width="2"/>
<text x="390" y="155" class="text-flow" id="sum">z</text>
<!-- Conexões -->
<line x1="130" y1="100" x2="220" y2="100" class="line" />
<line x1="130" y1="200" x2="220" y2="200" class="line" />
<line x1="280" y1="100" x2="370" y2="150" class="line" />
<line x1="280" y1="200" x2="370" y2="150" class="line" />
</svg>
<p id="final-result" class="result-text"></p>
</div>
<script>
const outputs = [0, 0, 0, 1]; // Saídas para função AND
let weights = [parseFloat(document.getElementById('w1').value), parseFloat(document.getElementById('w2').value)];
const learningRate = 0.5;
function stepFunction(z) {
return z >= 1 ? 1 : 0;
}
function updateDisplay(x, w, z) {
// Atualiza os valores dos nós na rede
document.getElementById('input1').textContent = x[0];
document.getElementById('input2').textContent = x[1];
document.getElementById('weight1').textContent = w[0].toFixed(2);
document.getElementById('weight2').textContent = w[1].toFixed(2);
document.getElementById('sum').textContent = z.toFixed(2);
}
function runSingleInput() {
const x1 = parseInt(document.getElementById('x1').value);
const x2 = parseInt(document.getElementById('x2').value);
const x = [x1, x2];
const yExpected = outputs[x1 * 2 + x2]; // Calcula o índice da saída esperada
// Cálculo da soma ponderada (z)
const z = x[0] * weights[0] + x[1] * weights[1];
const yPred = stepFunction(z);
const error = yExpected - yPred;
// Atualiza a visualização da rede
updateDisplay(x, weights, z);
// Atualização de pesos se houver erro
let newWeights = [...weights];
if (error !== 0) {
newWeights[0] += learningRate * error * x[0];
newWeights[1] += learningRate * error * x[1];
}
// Atualiza os pesos
weights = newWeights;
// Exibe o resultado da linha de entrada
document.getElementById('final-result').textContent = `Entrada: [${x1}, ${x2}] - Saída esperada: ${yExpected}, Saída obtida: ${yPred}, Pesos atualizados: [${weights[0].toFixed(2)}, ${weights[1].toFixed(2)}]`;
}
</script>
</body>
</html>