Skip to content

Commit

Permalink
update 293
Browse files Browse the repository at this point in the history
  • Loading branch information
ascoders committed Mar 17, 2024
1 parent 4367f0c commit a90c198
Showing 1 changed file with 15 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,32 +161,25 @@ type TrainingItem = [number[], number[]]
```ts
class NeuralNetwork {
/** 获取上一层神经网络各节点的值 */
private getPreviousLayerValues(layerIndex: number, trainingItem: TraningItem) {
if (layerIndex >= 0) {
return this.networkStructor[layerIndex].neurals.map((neural) => neural.value);
}
return trainingItem[0];
}

private modelFunction(trainingItem: TraningItem) {
this.networkStructor.forEach((layer, layerIndex) => {
// 遍历上一层所有输入
const visitPreviousLayerInputs = (
callback: (value: number, index: number) => void
) => {
if (layerIndex === 0) {
// 第一层的输入来自 Training data
trainingItem[0].forEach(callback);
} else {
// 之后每层输入来自于上一层
this.networkStructor[layerIndex - 1].neurals.forEach(
(neural, index) => {
callback(neural.value!, index);
}
);
}
};

layer.neurals.forEach((neural) => {
// 前置节点的值 * w 的总和
let previousWeightCount = 0;
visitPreviousLayerInputs((value, index) => {
previousWeightCount += value * neural.w[index];
});
const activateResult = activate(layer.activation)(previousWeightCount + neural.b);
let weightCount = 0;
this.getPreviousLayerValues(layerIndex - 1, trainingItem).forEach(
(value, index) => {
weightCount += value * neural.w[index];
},
);
const activateResult = activate(layer.activation)(weightCount + neural.b);
neural.value = neural.c * activateResult;
});
});
Expand Down

0 comments on commit a90c198

Please sign in to comment.