| 2509 | } |
| 2510 | |
| 2511 | computeLayerPositions(layerIndex, neuronCount, layerX) { |
| 2512 | const positions = []; |
| 2513 | const isOutputLayer = layerIndex === this.mlp.architecture.length - 1; |
| 2514 | if (layerIndex === 0) { |
| 2515 | const spacing = this.options.inputSpacing; |
| 2516 | let rows; |
| 2517 | let cols; |
| 2518 | if (neuronCount === 28 * 28) { |
| 2519 | rows = 28; |
| 2520 | cols = 28; |
| 2521 | } else { |
| 2522 | cols = Math.ceil(Math.sqrt(neuronCount)); |
| 2523 | rows = Math.ceil(neuronCount / cols); |
| 2524 | } |
| 2525 | const height = (rows - 1) * spacing; |
| 2526 | const width = (cols - 1) * spacing; |
| 2527 | let filled = 0; |
| 2528 | for (let row = 0; row < rows && filled < neuronCount; row += 1) { |
| 2529 | for (let col = 0; col < cols && filled < neuronCount; col += 1) { |
| 2530 | const y = height / 2 - row * spacing; |
| 2531 | const z = -width / 2 + col * spacing; |
| 2532 | positions.push(new THREE.Vector3(layerX, y, z)); |
| 2533 | filled += 1; |
| 2534 | } |
| 2535 | } |
| 2536 | } else if (isOutputLayer) { |
| 2537 | const spacing = this.options.outputSpacing ?? this.options.hiddenSpacing; |
| 2538 | const height = (neuronCount - 1) * spacing; |
| 2539 | for (let index = 0; index < neuronCount; index += 1) { |
| 2540 | const y = height / 2 - index * spacing; |
| 2541 | positions.push(new THREE.Vector3(layerX, y, 0)); |
| 2542 | } |
| 2543 | } else { |
| 2544 | const spacing = this.options.hiddenSpacing; |
| 2545 | const cols = Math.max(1, Math.ceil(Math.sqrt(neuronCount))); |
| 2546 | const rows = Math.ceil(neuronCount / cols); |
| 2547 | const height = (rows - 1) * spacing; |
| 2548 | const width = (cols - 1) * spacing; |
| 2549 | for (let index = 0; index < neuronCount; index += 1) { |
| 2550 | const row = Math.floor(index / cols); |
| 2551 | const col = index % cols; |
| 2552 | const y = height / 2 - row * spacing; |
| 2553 | const z = -width / 2 + col * spacing; |
| 2554 | positions.push(new THREE.Vector3(layerX, y, z)); |
| 2555 | } |
| 2556 | } |
| 2557 | return positions; |
| 2558 | } |
| 2559 | |
| 2560 | clearOutputLabels() { |
| 2561 | if (!this.outputLabels.length || !this.labelGroup) return; |