The ANN will be use on each synaps to messure the previous activity of the neuron and descide to close or open connection. :param name: Name of the feature :param ann_values: Values to be use to build an ANN that will adapt the connectivity of the layer. :param valu
(
self,
name: str,
value: Union[torch.Tensor, float, int] = None,
value_dtype: torch.dtype = torch.float32,
ann_values: Union[list, tuple] = None,
const_update_rate: float = 0.1,
const_decay: float = 0.01,
sparse: Optional[bool] = False,
batch_size: int = 1,
)
| 925 | |
| 926 | class AdaptationBaseOtherSynaps(AbstractFeature): |
| 927 | def __init__( |
| 928 | self, |
| 929 | name: str, |
| 930 | value: Union[torch.Tensor, float, int] = None, |
| 931 | value_dtype: torch.dtype = torch.float32, |
| 932 | ann_values: Union[list, tuple] = None, |
| 933 | const_update_rate: float = 0.1, |
| 934 | const_decay: float = 0.01, |
| 935 | sparse: Optional[bool] = False, |
| 936 | batch_size: int = 1, |
| 937 | ) -> None: |
| 938 | # language=rst |
| 939 | """ |
| 940 | The ANN will be use on each synaps to messure the previous activity of the neuron and descide to close or open connection. |
| 941 | |
| 942 | :param name: Name of the feature |
| 943 | :param ann_values: Values to be use to build an ANN that will adapt the connectivity of the layer. |
| 944 | :param value: Values to be use to build an initial mask for the synapses. |
| 945 | :param value_dtype: Data type for :code:`value` tensor |
| 946 | :param const_update_rate: The mask upatate rate of the ANN decision. |
| 947 | :param const_decay: The spontaneous activation of the synapses. |
| 948 | :param sparse: Should :code:`value` parameter be sparse tensor or not |
| 949 | :param batch_size: Mini-batch size. |
| 950 | """ |
| 951 | self.value_dtype = value_dtype |
| 952 | value = value.to(self.value_dtype) |
| 953 | |
| 954 | # Define the ANN |
| 955 | class ANN(nn.Module): |
| 956 | def __init__(self, input_size, hidden_size, output_size): |
| 957 | super(ANN, self).__init__() |
| 958 | self.fc1 = nn.Linear(input_size, hidden_size, bias=False) |
| 959 | self.fc2 = nn.Linear(hidden_size, output_size, bias=False) |
| 960 | |
| 961 | def forward(self, x): |
| 962 | x = torch.relu(self.fc1(x)) |
| 963 | x = torch.tanh(self.fc2(x)) # MUST HAVE output between -1 and 1 |
| 964 | return x |
| 965 | |
| 966 | self.init_value = value.clone().detach() # initial mask |
| 967 | self.mask = value # final decision of the ANN |
| 968 | value = torch.zeros_like(value) # initial mask |
| 969 | self.ann = ANN(ann_values[0].shape[0], ann_values[0].shape[1], 1) |
| 970 | |
| 971 | # load weights from ann_values |
| 972 | with torch.no_grad(): |
| 973 | self.ann.fc1.weight.data = ann_values[0] |
| 974 | self.ann.fc2.weight.data = ann_values[1] |
| 975 | self.ann.to(ann_values[0].device) |
| 976 | |
| 977 | self.spike_buffer = torch.zeros( |
| 978 | (value.numel(), ann_values[0].shape[1]), |
| 979 | device=ann_values[0].device, |
| 980 | dtype=torch.bool, |
| 981 | ) |
| 982 | self.counter = 0 |
| 983 | self.start_counter = False |
| 984 | self.const_update_rate = const_update_rate |