Created
April 15, 2024 06:43
-
-
Save mkwng/719eac37774265029d123b2c5bdca6fa to your computer and use it in GitHub Desktop.
Simple Neural Network in React with TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```typescript | |
import React, { useState } from 'react'; | |
interface NeuralNetworkProps { | |
inputs: number[]; | |
weights: number[]; | |
bias: number; | |
} | |
const NeuralNetwork: React.FC<NeuralNetworkProps> = ({ inputs, weights, bias }) => { | |
const [output, setOutput] = useState(0); | |
const calculateOutput = (): number => { | |
let sum = 0; | |
for (let i = 0; i < inputs.length; i++) { | |
sum += inputs[i] * weights[i]; | |
} | |
sum += bias; | |
return Math.max(0, sum); | |
}; | |
const handleClick = () => { | |
const result = calculateOutput(); | |
setOutput(result); | |
}; | |
return ( | |
<div> | |
<button onClick={handleClick}>Calculate Output</button> | |
<p>Output: {output}</p> | |
</div> | |
); | |
}; | |
export default NeuralNetwork; | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment