Skip to content

Instantly share code, notes, and snippets.

@mkwng
Created April 15, 2024 07:03
Show Gist options
  • Save mkwng/236e9951032286f2cd4b1a0bda7461cb to your computer and use it in GitHub Desktop.
Save mkwng/236e9951032286f2cd4b1a0bda7461cb to your computer and use it in GitHub Desktop.
Simple Neural Network in TypeScript React
import React, { useState } from 'react';
// Define the neural network structure
interface NeuralNetworkProps {
inputs: number[];
weights: number[];
bias: number;
}
const NeuralNetwork: React.FC<NeuralNetworkProps> = ({ inputs, weights, bias }) => {
const [output, setOutput] = useState(0);
// Implement the forward propagation
const forwardPropagation = () => {
let sum = 0;
for (let i = 0; i < inputs.length; i++) {
sum += inputs[i] * weights[i];
}
sum += bias;
const activation = 1 / (1 + Math.exp(-sum)); // Sigmoid activation function
setOutput(activation);
};
return (
<div>
<button onClick={forwardPropagation}>Forward Propagate</button>
<p>Output: {output.toFixed(2)}</p>
</div>
);
};
// Example usage
const App: React.FC = () => {
const inputs = [0.5, 0.1, 0.2];
const weights = [0.3, 0.4, 0.1];
const bias = 0.2;
return (
<div>
<h1>Simple Neural Network</h1>
<NeuralNetwork inputs={inputs} weights={weights} bias={bias} />
</div>
);
};
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment