forked from danindiana/Deep_Convo_GPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrainmodel.rs
67 lines (59 loc) · 1.56 KB
/
Brainmodel.rs
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
// File: brain_model.rs
use rand::Rng;
use std::sync::Arc;
use std::sync::Mutex;
pub struct Neuron {
weights: Vec<f32>,
}
impl Neuron {
pub fn new(connections: usize) -> Neuron {
let mut rng = rand::thread_rng();
let mut weights = Vec::new();
for _ in 0..connections {
weights.push(rng.gen());
}
Neuron { weights }
}
pub fn process(&self, inputs: &Vec<f32>) -> f32 {
self.weights.iter().zip(inputs).map(|(w, i)| w * i).sum()
}
}
pub struct Brain {
neurons: Arc<Mutex<Vec<Neuron>>>,
}
impl Brain {
pub fn new(size: usize) -> Brain {
let mut neurons = Vec::new();
for _ in 0..size {
neurons.push(Neuron::new(size));
}
Brain {
neurons: Arc::new(Mutex::new(neurons)),
}
}
pub fn encode(&self, inputs: Vec<f32>) -> Vec<f32> {
self.neurons
.lock()
.unwrap()
.iter()
.map(|neuron| neuron.process(&inputs))
.collect()
}
pub fn retrieve(&self, encoded: Vec<f32>) -> Vec<f32> {
self.neurons
.lock()
.unwrap()
.iter()
.map(|neuron| neuron.process(&encoded))
.collect()
}
}
fn main() {
let brain = Brain::new(100);
let sensory_input = vec![0.5; 100];
let encoded = brain.encode(sensory_input.clone());
let retrieved = brain.retrieve(encoded);
println!("Sensory input: {:?}", sensory_input);
println!("Encoded: {:?}", encoded);
println!("Retrieved: {:?}", retrieved);
}