Skip to content

Commit 13ac243

Browse files
committed
updated deprecated parameter specs
1 parent 71a8415 commit 13ac243

27 files changed

+199
-93
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ permalink: /
1111
</div>
1212

1313
<p style="display: flex; align-items: center;">
14-
<img src="{{ '/assets/images/icons/github.png' | relative love }}" width="20" alt="GitHub">
14+
<img src="{{ '/assets/images/icons/github.png' | relative_url }}" width="20" alt="GitHub">
1515
<a href="https://github.com/NN4Neurosim/nn4n" style="margin-left: 5px; color: #333; text-decoration: underline">GitHub Repository</a>
1616
</p>
1717

@@ -31,4 +31,4 @@ Immense thanks to [Dr. Christopher J. Cueva](https://www.metaconscious.org/autho
3131
This project is licensed under the terms of the MIT license. See the [LICENSE](https://opensource.mit.edu/license) file for details.
3232

3333
#### Template
34-
The project documentation is based on [Jekyll](https://jekyllrb.com/) and uses [Jekyll Gitbook](https://github.com/sighingnow/jekyll-gitbook) theme developed by [sighingnow](https://github.com/sighingnow).
34+
The project documentation is based on [Jekyll](https://jekyllrb.com/) and uses [Jekyll Gitbook](https://github.com/sighingnow/jekyll-gitbook) theme developed by [sighingnow](https://github.com/sighingnow).

_collection_1/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pip install nn4n
1717
```
1818
git clone https://github.com/NN4Neurosim/nn4n.git
1919
```
20-
#### Navigate to the NN4Neurosci directory
20+
#### Navigate to the project directory
2121
```
2222
cd nn4n/
2323
pip install -e .

_collection_1/quickstart.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,11 @@ layout: post
77
order: 2
88
---
99

10-
## Initialize a Continuous-Time RNN
11-
```python
12-
import torch
13-
from nn4n.model import CTRNN
14-
15-
rnn = CTRNN(input_dim=1, hidden_size=10, output_dim=1)
16-
optimizer = torch.optim.Adam(rnn.parameters(), lr=0.001)
17-
```
18-
19-
2010
## Define a Task
2111
The input/output signals to train the RNN can be any time series data. Let $X$ be the input signal and $Y$ be the output signal. $X$ should of shape `(n_timesteps, batch_size, input_dim)` and $Y$ should be of shape `(n_timesteps, batch_size, output_dim)`. These signals could be representations of many cognitive tasks, such as working memory, decision making, or motor control, etc. Here we use a simple sin wave as an example.
12+
2213
```python
14+
import torch
2315
import numpy as np
2416
import matplotlib.pyplot as plt
2517

@@ -38,6 +30,14 @@ plt.show()
3830
<img src="{{ '/assets/images/results/sin_wave.png' | relative_url }}" width="400" alt="Sin Wave">
3931
</p>
4032

33+
## Initialize a Continuous-Time RNN
34+
```python
35+
from nn4n.model import CTRNN
36+
37+
rnn = CTRNN(input_dim=1, hidden_size=10, output_dim=1)
38+
optimizer = torch.optim.Adam(rnn.parameters(), lr=0.001)
39+
```
40+
4141
## Train the RNN
4242
```python
4343
losses = []

_collection_2/layer_methods.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Layer Methods
3+
author: Zhaoze Wang
4+
date: 2024-06-16
5+
category: docs
6+
layout: post
7+
order: 4
8+
---
9+
10+
# layer.set_weight( )
11+
12+
##### Description
13+
Set the weight of the layer. Only applicable to the hidden_layer and linear_layer.
14+
15+
##### Parameters
16+
> `weight`: (torch.Tensor), required. The weight tensor to be set. The shape of the weight tensor should be `(output_dim, input_dim)`.
17+
18+
##### Usage
19+
```python
20+
import torch
21+
from nn4n.model import CTRNN
22+
23+
ctrnn = CTRNN()
24+
weight = torch.rand(100, 1)
25+
ctrnn.layers[0].set_weight(weight)
26+
```

_collection_2/methods.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,30 @@ for _ in range(100):
206206
loss.backward()
207207
optimizer.step()
208208
```
209+
210+
211+
# CTRNN.layers
212+
213+
##### Description
214+
Get a list of the network layers.
215+
216+
##### Returns
217+
> `layers`: (list) A list of the network layers.
218+
219+
##### Usage
220+
```python
221+
from nn4n.model import CTRNN
222+
223+
ctrnn = CTRNN()
224+
layers = ctrnn.layers
225+
226+
for layer in layers:
227+
print(layer)
228+
```
229+
230+
##### Output
231+
```
232+
LinearLayer()
233+
HiddenLayer()
234+
LinearLayer()
235+
```

_collection_2/parameters.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@ These parameters primarily determine the structure of the network. It is recomme
1212

1313
<div class="table-wrapper" markdown="block">
1414

15-
|Parameter|Default|Type|Description|
15+
| Parameter | Default | Type | Description |
1616
|:-:|:-:|:-:|:-:|
17-
| input_dim | 1 | `int` | Input dimension |
18-
| output_dim | 1 | `int` | Output dimension |
19-
| hidden_size | 100 | `int` | Number of hidden nodes |
20-
| scaling | 1.0 | `float` | Scaling factor for the hidden weights, it will scale the hidden weight by $\frac{scaling}{\sqrt{N\_{hid}}}$. Won't be affected when the HiddenLayer distribution is `uniform`. |
21-
| self_connections | False | `boolean` | Whether a neuron can connect to itself |
22-
| activation | `relu` | `string` | Activation function, could be `relu`/`tanh` / `sigmoid`/`retanh` |
23-
| layer_distributions | `uniform` | `string`/`list` | Layer distributions. Either `string` or a `list` of three elements. The `string` or `list` element must be either `uniform`, `normal`, or `zero`. If the given value is a `string`, it will be broadcasted to all layers. If the provided value is a `list`, its length must match the number of layers in the network and contains only valid distribution values. |
24-
| layer_biases | `True` | `boolean` or `list` | Whether to use bias in each layer. Either a `boolean` or a `list` of three `boolean`s. If the given value is a list, its length must match the number of layers in the network and contains only `boolean` values. |
17+
| dims | [1, 100, 1] | `list` | Dimensions of the network, must be a list of three integers [input_dim, hidden_size, output_dim] |
18+
| activation | 'relu' | `string` | Activation function, could be 'relu', 'tanh', 'sigmoid', or 'retanh' |
19+
| biases | `None` | `None`, `string`, or `list` | Use bias or not for each layer. A single value is broadcasted to a list of three values, which can be `None` (not using bias); 'zero' or 0 (bias initialized to 0 but could change during training); 'normal' (normal distribution), or ;'uniform' (bias initialized from a uniform distribution). If a list of three values is passed, each can be as described or a numpy array/torch tensor specifying the bias. |
20+
| weights | 'uniform' | `string` or `list` | Distribution of weights for each layer. A single string is broadcasted to a list of three strings. Possible values: 'normal' (weights initialized from a normal distribution), 'uniform' (weights initialized from a uniform distribution). If a list of three values is passed, each can be as described or a numpy array/torch tensor specifying the weights. |
2521

2622
</div>
2723

@@ -41,16 +37,17 @@ These parameters primarily determine the training process of the network. The `t
4137

4238
</div>
4339

44-
# Constraint Parameters
45-
These parameters primarily determine the constraints of the network. By default, the network is initialized using the most lenient constraints, i.e., no constraints being enforced.
40+
# Mask Parameters
41+
When modeling the brain with neural networks, both the connections between neurons (synapses) and the neuron's non-linear activation functions are crucial components. Synapses, in particular, provide numerous degrees of freedom within the network. The connectivity matrix, for example, determines the network's structure, while various properties of synapses—such as their plasticity, whether they are excitatory or inhibitory, their strength, and the potential for new synapses to form—add layers of complexity and control. Here, we use masks to manage these characteristics.
4642

4743
<div class="table-wrapper" markdown="block">
4844

49-
| Parameter | Default | Type | Description |
50-
|:-:|:-:|:-:|:-:|
51-
| positivity_constraints | False | `boolean`/`list` | Whether to enforce Dale's law. Either a `boolean` or a `list` of three `boolean`s. If the given value is a list, from the first element to the last element, corresponds to the InputLayer, HiddenLayer, and ReadoutLayer, respectively. |
52-
| sparsity_constraints | True | `boolean`/`list` | Whether a neuron can grow new connections. See [constraints and masks](#constraints-and-masks). If it's a list, it must have precisely three elements. Note: this must be checked even if your mask is sparse, otherwise the new connection will still be generated. |
53-
| layer_masks | `None` or `list` | `list` of `np.ndarray` | Layer masks if `sparsity_constraints/positivity_constraints is set to true. From the first to the last, the list elements correspond to the mask for Input-Hidden, Hidden-Hidden, and Hidden-Readout weights, respectively. Each mask must have the same dimension as the corresponding weight matrix. See [constraints and masks](#constraints-and-masks) for details. |
45+
| Parameter | Default | Type | Description |
46+
|:-: |:-: |:-: |:-: |
47+
| sparsity_masks | None | `None` or `list` | Use `sparsity_masks` or not. A single `None` will be broadcasted to a list of three `None`s. If a list of three values is passed, each value can be either `None` or a numpy array/torch tensor specifying the `sparsity_masks`. |
48+
| ei_masks | None | `None` or `list` | Use `ei_masks` or not. A single `None` will be broadcasted to a list of three `None`s. If a list of three values is passed, each value can be either `None` or a numpy array/torch tensor specifying the `ei_masks`. |
49+
| plasticity_masks | None | `None` or `list` | Use `plasticity_masks` or not. A single `None` will be broadcasted to a list of three `None`s. If a list of three values is passed, each value can be either `None` or a numpy array/torch tensor specifying the `plasticity_masks`. |
50+
| synapse_growth_masks | None | `None` or `list` | Use `synapse_growth_masks` or not. A single `None` will be broadcasted to a list of three `None`s. If a list of three values is passed, each value can be either `None` or a numpy array/torch tensor that directly specifies the probability of growing a synapse at the selected location if there is no synapse. |
5451

5552
</div>
5653

@@ -104,11 +101,12 @@ Whether a neuron can connect to itself. This feature is enforced along with the
104101

105102
| Method | Description |
106103
|:-:|:-:|
107-
| [`forward()`]({{ site.baseurl }}/rnn/methods/#ctrnnforward-) | Forward pass |
104+
| [`forward()`]({{ site.baseurl }}/rnn/methods/#ctrnnforward-) | Forward pass. |
108105
| [`save()`]({{ site.baseurl }}/rnn/methods/#ctrnnsave-) | Save the network to a given path. |
109106
| [`load()`]({{ site.baseurl }}/rnn/methods/#ctrnnload-) | Load the network from a given path. |
110-
| [`print_layers()`]({{ site.baseurl }}/rnn/methods/#ctrnnprint_layers-) | Print the network architecture and layer-by-layer specifications |
107+
| [`print_layers()`]({{ site.baseurl }}/rnn/methods/#ctrnnprint_layers-) | Print the network architecture and layer-by-layer specifications. |
111108
| [`train()`]({{ site.baseurl }}/rnn/methods/#ctrnntrain-) | Set the network to training mode, training will be performed and constraints will be enforced. Also, during training, the recurrent noises (preact_noise and postact_noise) won't be added. |
112-
| [`eval()`]({{ site.baseurl }}/rnn/methods/#ctrnneval-) | Set the network to evaluation mode, no training will be performed and no constraints will be enforced |
109+
| [`eval()`]({{ site.baseurl }}/rnn/methods/#ctrnneval-) | Set the network to evaluation mode, no training will be performed and no constraints will be enforced. |
110+
| [`layers`]({{ site.baseurl }}/rnn/methods/#ctrnnlayers) | Return a list of the network layers. |
113111

114112
</div>

_collection_4/fr_constraints.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,35 @@ order: 6
1313

1414
Firing rate loss function. The Firing rate loss function is defined as:
1515

16-
$$ \mathcal{L}_{fr} = \frac{\lambda_{fr}}{N_{batch} T N_{hid}} \sum_{b,t,n=0}^{N_{batch}, T, N_{hid}} fr_{btn}^2 $$
16+
$$ \mathcal{L}_{fr} = \frac{\lambda_{fr}}{B \times T \times N_{hid}} \sum_{b,t,i=0}^{B, T, N_{hid}} r_{b,t,i}^2 $$
1717

18-
where $N_{batch}$ is the batch size, $T$ is the number of time steps, $N_{out}$ is the number of output neurons, $fr_{btn}$ is the firing rate of neuron $n$ at time $t$ in the $i$-th batch.
18+
- $ B $: number of batches.
19+
- $ T $: number of time steps.
20+
- $ N_{hid} $: number of hidden neurons.
21+
- $ r_{b,t,i} $: firing rate of the $i$-th neuron at time $t$ in the $b$-th batch.
1922

2023

2124
# Firing Rate Constraint (SD)
2225
**Lambda Key:** `lambda_fr_sd`
2326

24-
Regularize the SD of the HiddenLayer firing rates such that all neurons will fire at approximately the same rate. The Firing rate SD loss function is defined as:
27+
Regularize the standard deviation of the firing rate.
2528

26-
$$ \mathcal{L}_{fr\_sd} = \lambda_{fr\_sd} \sqrt{\frac{1}{N_{hid}} \sum_{n=0}^{N_{hid}} \left(\sum_{b,t=0}^{N_{batch}, T}\frac{fr_{bt}}{N_{batch} T} - \mu \right)^2} $$
29+
$$ \mathcal{L}_{fr\_sd} = \lambda_{fr\_sd} \sqrt{\frac{1}{N_{hid}} \sum_{n=0}^{N_{hid}} \left(\frac{1}{B \times T} \sum_{b,t=0}^{B,T} r_{b,t} - \mu \right)^2} $$
2730

28-
$$ \mu = \frac{\lambda_{fr}}{N_{batch} T N_{hid}} \sum_{b,t,n=0}^{N_{batch}, T, N_{hid}} fr_{btn} $$
31+
$$ \mu = \frac{1}{B \times T \times N_{hid}} \sum_{b,t,i=0}^{B, T, N_{hid}} r_{b,t,i} $$
2932

30-
where $N_{batch}$ is the batch size, $T$ is the number of time steps, $ y_{bt} $ is the predicted firing rate of the neuron at time $t$ in the $i$-th batch, and $f_{bt}$ is the ground truth firing rate of the neuron at time $t$ in the $i$-th batch.
33+
- $ B $: number of batches.
34+
- $ T $: number of time steps.
35+
- $ N_{hid} $: number of hidden neurons.
36+
- $ r_{b,t,i} $: firing rate of the $i$-th neuron at time $t$ in the $b$-th batch.
37+
- $ \mu $: mean firing rate.
3138

3239
# Firing Rate Constraint (CV)
3340
**Lambda Key:** `lambda_fr_cv`
3441

35-
Regularize the firing rate of a single neuron such that all neurons will fire at approximately the same rate. The Firing Rate Regularization loss function is defined as:
42+
Regularize the coefficient of variation of the firing rate.
3643

3744
$$ \mathcal{L}_{fr\_cv} = \lambda_{fr\_cv} \frac{\sigma}{\mu} $$
3845

39-
where $\sigma$ is the standard deviation of the firing rate of the neuron, and $\mu$ is the network mean firing rate.
46+
- $ \sigma $: standard deviation of the firing rate of the neuron.
47+
- $ \mu $: network mean firing rate.

_collection_4/readout_constraints.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ ReadoutLayer constraint. The ReadoutLayer sparsity loss function is defined as:
1313

1414
$$ \mathcal{L}_{out} = \frac{\lambda_{out}}{N_{out} N_{hid}} ||W_{out}||_F^2 $$
1515

16-
- $N_{out}$: number of output neurons.
16+
- $ N_{out} $: number of readout neurons.
17+
- $ N_{hid} $: number of hidden neurons.
18+
- $ \textbf{W}_{out} $: readout layer weight matrix.

_collection_5/base_struct.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ Base class for all masks. It serves as a boilerplate for other masks. It is not
1616

1717
| Parameter | Default | Required | Type | Description |
1818
|:---------------------|:-------------:|:-------------:|:----------------:|:-------------------------------------------|
19-
| input_dim | `None` | True |`int` | Input dimension. Used to generate the input layer mask. |
20-
| hidden_size | `None` | True | `int` | HiddenLayer size. Used to generate the hidden layer mask. |
21-
| output_dim | `None` | True | `int` | Output dimension. Used to generate the readout layer mask. |
19+
| dims | [1, 100, 1] | `list` | Dimensions of the network, must be a list of three integers [input_dim, hidden_size, output_dim] |
2220

2321
</div>
2422

@@ -34,7 +32,7 @@ Methods that are shared by all structures.
3432
| [`get_readout_idx()`]({{ site.baseurl }}/mask/methods/#maskget_readout_idx-) | Get indices of neurons that readout from. |
3533
| [`get_non_input_idx()`]({{ site.baseurl }}/mask/methods/#maskget_non_input_idx-) | Get indices of neurons that don't receive input. |
3634
| [`visualize()`]({{ site.baseurl }}/mask/methods/#maskvisualize-) | Visualize the generated masks. |
37-
| [`masks()`]({{ site.baseurl }}/mask/methods/#maskmasks-) | Return a list of np.ndarray masks. It will be of length 3, where the first element is the input mask, the second element is the hidden mask, and the third element is the readout mask. For those structures that do not have specification for a certain mask, it will be an all-one matrix. |
35+
| [`get_masks()`]({{ site.baseurl }}/mask/methods/#maskget_masks-) | Return a list of np.ndarray masks. It will be of length 3, where the first element is the input mask, the second element is the hidden mask, and the third element is the readout mask. For those structures that do not have specification for a certain mask, it will be an all-one matrix. |
3836
| [`get_areas()`]({{ site.baseurl }}/mask/methods/#maskget_areas-) | Get a list of areas names. |
3937
| [`get_area_idx()`]({{ site.baseurl }}/mask/methods/#maskget_area_idx-) | Get indices of neurons in a specific area. The parameter `area` could be either a string from the `get_areas()` or a index of the area. |
4038

_collection_5/methods.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Output:
5050
</p>
5151

5252

53-
# mask.masks( )
53+
# mask.get_masks( )
5454
##### Description
5555
Return layer masks in a list.
5656

@@ -68,7 +68,7 @@ params = {
6868
"output_dim": 2,
6969
}
7070
multiarea = MultiArea(**params)
71-
multiarea.masks()
71+
multiarea.get_masks()
7272
```
7373

7474
Output

_collection_5/multi_area.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ This will generate a multi-area RNN without E/I constraints. Therefore, by defau
1919

2020
## Parameters
2121

22+
### Inherited Parameters
23+
This class inherits all parameters from `BaseStruct`. See [BaseStruct]({{ site.baseurl }}/mask/base_struct) for more details.
24+
25+
### Other Parameters
2226
<div class="table-wrapper" markdown="block">
2327

2428
| Parameter | Default | Type | Description |
@@ -53,4 +57,60 @@ $W$ may not matter if your connectivity matrix is symmetric. But if it's not, yo
5357
<p align="center">
5458
<img src="{{ '/assets/images/basics/Multi_Area.png' | relative_url }}" width="600">
5559
</p>
56-
<br>
60+
61+
62+
## Example
63+
64+
```python
65+
from nn4n.mask import MultiArea
66+
67+
area_connectivities = np.array([
68+
[1.0, 0.1, 0.0, 0.0],
69+
[0.1, 1.0, 0.1, 0.0],
70+
[0.0, 0.1, 1.0, 0.1],
71+
[0.0, 0.0, 0.1, 1.0],
72+
])
73+
74+
mask_params = {
75+
"n_areas": 4,
76+
"area_connectivities": area_connectivities,
77+
"input_areas": [0],
78+
"readout_areas": [2, 3],
79+
"dims": [1, 100, 1],
80+
}
81+
82+
network_mask = MultiArea(**mask_params)
83+
network_mask.plot_masks()
84+
```
85+
86+
###### Output:
87+
<p>
88+
<img src="{{ '/assets/images/mask/results/multi_area_input_mask.png' | relative_url }}" width="480">
89+
</p>
90+
<p>
91+
<img src="{{ '/assets/images/mask/results/multi_area_hidden_mask.png' | relative_url }}" width="480">
92+
</p>
93+
<p>
94+
<img src="{{ '/assets/images/mask/results/multi_area_output_mask.png' | relative_url }}" width="480">
95+
</p>
96+
97+
##### Use It as a Sparsity Mask
98+
```python
99+
from nn4n.model import CTRNN
100+
101+
rnn = CTRNN(sparsity_masks=network_struct.get_masks())
102+
layer = rnn.layers[1]
103+
optimizer = torch.optim.Adam(rnn.parameters(), lr=0.001)
104+
rnn.plot_layers()
105+
```
106+
107+
###### Output:
108+
<p>
109+
<img src="{{ '/assets/images/mask/results/multi_area_input_weight.png' | relative_url }}" width="500">
110+
</p>
111+
<p>
112+
<img src="{{ '/assets/images/mask/results/multi_area_hidden_weight.png' | relative_url }}" width="500">
113+
</p>
114+
<p>
115+
<img src="{{ '/assets/images/mask/results/multi_area_output_weight.png' | relative_url }}" width="500">
116+
</p>

_collection_5/multi_area_ei.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ This class is a child class of `MultiArea`. It will generate a multi-area RNN wi
1717

1818
## Parameters
1919

20+
### Inherited Parameters
21+
This class inherits all parameters from `MultiArea`. See [MultiArea]({{ site.baseurl }}/mask/multi_area) for more details.
22+
23+
### Other Parameters
2024
<div class="table-wrapper" markdown="block">
2125

2226
| Parameter | Default | Type | Description |

0 commit comments

Comments
 (0)