Skip to content

Commit bec10d6

Browse files
authored
Merge pull request #1322 from singnet/senna-1286-2
[#1286] Allow multiples cycles of spreading activation in stimulate()
2 parents 21d5a68 + 061c243 commit bec10d6

7 files changed

Lines changed: 94 additions & 27 deletions

File tree

‎src/attention_broker/AttentionBrokerServer.cc‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ using namespace attention_broker;
88
double AttentionBrokerServer::RENT_RATE = 0.75;
99
double AttentionBrokerServer::SPREADING_RATE_LOWERBOUND = 0.10;
1010
double AttentionBrokerServer::SPREADING_RATE_UPPERBOUND = 0.10;
11+
unsigned int AttentionBrokerServer::EXTRA_ACTIVATION_CYCLES_PER_ROUND = 0;
1112

1213
// --------------------------------------------------------------------------------
1314
// Public methods
@@ -62,7 +63,13 @@ Status AttentionBrokerServer::stimulate(ServerContext* grpc_context,
6263
HebbianNetwork* network = select_hebbian_network(request->context());
6364
((dasproto::HandleCount*) request)->set_hebbian_network((long) network);
6465
// this->stimulus_requests->enqueue((void *) request);
65-
this->stimulus_spreader->spread_stimuli(request);
66+
this->stimulus_spreader->spread_stimuli(request, false);
67+
dasproto::HandleCount empty_request;
68+
empty_request.set_hebbian_network((long) network);
69+
(*empty_request.mutable_map())["SUM"] = 0;
70+
for (unsigned int i = 0; i < EXTRA_ACTIVATION_CYCLES_PER_ROUND; i++) {
71+
this->stimulus_spreader->spread_stimuli(&empty_request, true);
72+
}
6673
}
6774
reply->set_msg("STIMULATE");
6875
#if LOG_LEVEL >= DEBUG_LEVEL
@@ -351,7 +358,7 @@ Status AttentionBrokerServer::drop_and_load_context(ServerContext* grpc_context,
351358
LOG_LOCAL_DEBUG("SUM: " + std::to_string(activation_request.map().find("SUM")->second));
352359
#endif
353360
LOG_DEBUG("Spreading " + std::to_string(sum_activation) + " tokens of activation");
354-
this->stimulus_spreader->spread_stimuli(&activation_request);
361+
this->stimulus_spreader->spread_stimuli(&activation_request, false);
355362
}
356363
LOG_DEBUG("Finished loading context file");
357364
} catch (const std::exception& e) {

‎src/attention_broker/AttentionBrokerServer.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class AttentionBrokerServer final : public AttentionBroker::Service {
6262
static double RENT_RATE; /// double in [0..1] range.
6363
static double SPREADING_RATE_LOWERBOUND; /// double in [0..1] range.
6464
static double SPREADING_RATE_UPPERBOUND; /// double in [0..1] range.
65+
static unsigned int EXTRA_ACTIVATION_CYCLES_PER_ROUND;
6566

6667
// RPC API
6768

‎src/attention_broker/StimulusSpreader.cc‎

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ static bool consolidate_rent_and_wages(HandleTrie::TrieNode* node, void* data) {
6464

6565
TokenSpreader::ImportanceChanges* changes =
6666
(TokenSpreader::ImportanceChanges*) ((DATA*) data)->importance_changes->lookup(node->suffix);
67-
value->importance -= changes->rent;
68-
value->importance += changes->wages;
67+
ImportanceType rent = 0;
68+
ImportanceType wages = 0;
69+
if (changes != NULL) {
70+
rent = changes->rent;
71+
wages = changes->wages;
72+
value->importance -= rent;
73+
value->importance += wages;
74+
}
6975

7076
// Compute amount to be spread
7177
ImportanceType largest_arity = ((DATA*) data)->largest_arity;
@@ -80,8 +86,8 @@ static bool consolidate_rent_and_wages(HandleTrie::TrieNode* node, void* data) {
8086
LOG_LOCAL_DEBUG(\
8187
"Update " + node->suffix + ":" + \
8288
" " + std::to_string(original_importance) + \
83-
" - " + std::to_string(changes->rent) + " (rent)" + \
84-
" + " + std::to_string(changes->wages) + " (wages)" + \
89+
" - " + std::to_string(rent) + " (rent)" + \
90+
" + " + std::to_string(wages) + " (wages)" + \
8591
" - " + std::to_string(to_spread) + " (spread) " + \
8692
" = " + std::to_string(value->importance));
8793
// clang-format on
@@ -134,17 +140,17 @@ static bool consolidate_stimulus(HandleTrie::TrieNode* node, void* data) {
134140
// Public methods
135141

136142
void TokenSpreader::distribute_wages(const dasproto::HandleCount* handle_count,
137-
ImportanceType& total_to_spread,
143+
ImportanceType& total_to_distribute,
138144
DATA* data) {
139145
auto iterator = handle_count->map().find("SUM");
140146
if (iterator == handle_count->map().end()) {
141147
RAISE_ERROR("Missing 'SUM' key in HandleCount request");
142148
}
143-
unsigned int total_wages = iterator->second;
144-
LOG_DEBUG("Total wages: " + std::to_string(total_wages));
149+
unsigned int sum = iterator->second;
150+
LOG_DEBUG("Sum: " + std::to_string(sum));
145151
for (auto pair : handle_count->map()) {
146152
if (pair.first != "SUM") {
147-
double normalized_amount = (((double) pair.second) * total_to_spread) / total_wages;
153+
double normalized_amount = (((double) pair.second) * total_to_distribute) / sum;
148154
LOG_LOCAL_DEBUG("Normalized wage to handle " + pair.first + ": " +
149155
std::to_string(normalized_amount));
150156
data->importance_changes->insert(
@@ -153,7 +159,7 @@ void TokenSpreader::distribute_wages(const dasproto::HandleCount* handle_count,
153159
}
154160
}
155161

156-
void TokenSpreader::spread_stimuli(const dasproto::HandleCount* request) {
162+
void TokenSpreader::spread_stimuli(const dasproto::HandleCount* request, bool spread_only_flag) {
157163
HebbianNetwork* network = (HebbianNetwork*) request->hebbian_network();
158164
if (network == NULL) {
159165
return;
@@ -173,22 +179,26 @@ void TokenSpreader::spread_stimuli(const dasproto::HandleCount* request) {
173179
// clang-format on
174180
data.total_rent = 0.0;
175181

176-
// Collect rent
177-
network->visit_nodes(true, &collect_rent, (void*) &data);
178-
LOG_DEBUG("Collected rent: " + std::to_string(data.total_rent));
179-
180-
// Distribute wages
181-
ImportanceType total_to_spread = network->alienate_tokens();
182-
LOG_DEBUG("Alienated tokens: " + std::to_string(total_to_spread));
183-
total_to_spread += data.total_rent;
184-
LOG_DEBUG("Total do spread: " + std::to_string(total_to_spread));
185-
distribute_wages(request, total_to_spread, &data);
182+
if (!spread_only_flag) {
183+
// Collect rent
184+
network->visit_nodes(true, &collect_rent, (void*) &data);
185+
LOG_DEBUG("Collected rent: " + std::to_string(data.total_rent));
186+
// Distribute wages
187+
ImportanceType total_to_distribute = network->alienate_tokens();
188+
LOG_DEBUG("Alienated tokens: " + std::to_string(total_to_distribute));
189+
total_to_distribute += data.total_rent;
190+
LOG_DEBUG("Total do spread: " + std::to_string(total_to_distribute));
191+
distribute_wages(request, total_to_distribute, &data);
192+
}
186193

187194
// Consolidate changes
188195
#if LOG_LEVEL >= LOCAL_DEBUG_LEVEL
189196
LOG_LOCAL_DEBUG("Importances before consolidate_rent_and_wages()");
190197
network->visit_nodes(true, &print_importance, (void*) &data);
191198
#endif
199+
// Required regardless spread_only_flag because the amount each node will
200+
// spread is computed here, just after consolidating rent and wage. We could
201+
// separate it but it would require an unecessary extra tree traversal.
192202
network->visit_nodes(true, &consolidate_rent_and_wages, (void*) &data);
193203
#if LOG_LEVEL >= LOCAL_DEBUG_LEVEL
194204
LOG_LOCAL_DEBUG("Importances after consolidate_rent_and_wages()");

‎src/attention_broker/StimulusSpreader.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class StimulusSpreader {
5454
* the magnitude of such boost. The actual way importance is boosted and then spread among
5555
* HebbianNetwork links are delegated to the concrete subclasses.
5656
*/
57-
virtual void spread_stimuli(const dasproto::HandleCount* request) = 0;
57+
virtual void spread_stimuli(const dasproto::HandleCount* request, bool spread_only_flag) = 0;
5858

5959
protected:
6060
StimulusSpreader(); /// Basic empty constructor.
@@ -124,7 +124,7 @@ class TokenSpreader : public StimulusSpreader {
124124
* @param request A list of handles to be boosted and respective counts which are used to determine
125125
* the magnitude of such boost.
126126
*/
127-
void spread_stimuli(const dasproto::HandleCount* request);
127+
void spread_stimuli(const dasproto::HandleCount* request, bool spread_only_flag);
128128

129129
// Used only in "visit" functions during trie traversals. Such functions aren't methods so this
130130
// method must be public.

‎src/attention_broker/WorkerThreads.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void WorkerThreads::worker_thread(unsigned int thread_id,
5656
if (request.second != NULL) {
5757
switch (request.first) {
5858
case RequestType::STIMULUS: {
59-
stimulus_spreader->spread_stimuli((dasproto::HandleCount*) request.second);
59+
stimulus_spreader->spread_stimuli((dasproto::HandleCount*) request.second, false);
6060
break;
6161
}
6262
case RequestType::CORRELATION: {

‎src/tests/cpp/attention_broker_test.cc‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ bool importance_equals(ImportanceType importance, double v2) {
2323

2424
TEST(AttentionBrokerTest, basics) {
2525
AttentionBrokerServer service;
26+
unsigned int save_parameter = AttentionBrokerServer::EXTRA_ACTIVATION_CYCLES_PER_ROUND;
27+
AttentionBrokerServer::EXTRA_ACTIVATION_CYCLES_PER_ROUND = 0;
2628
dasproto::Empty empty;
2729
dasproto::HandleCount handle_count;
2830
dasproto::HandleList handle_list;
@@ -41,13 +43,15 @@ TEST(AttentionBrokerTest, basics) {
4143
EXPECT_EQ(ack.msg(), "SET_DETERMINERS");
4244
service.get_importance(context, &handle_list, &importance_list);
4345
EXPECT_EQ(importance_list.list_size(), 0);
46+
AttentionBrokerServer::EXTRA_ACTIVATION_CYCLES_PER_ROUND = save_parameter;
4447
}
4548

46-
/*
4749
TEST(AttentionBrokerTest, get_importance) {
4850
string* handles = build_handle_space(4);
4951

5052
AttentionBrokerServer service;
53+
unsigned int save_parameter = AttentionBrokerServer::EXTRA_ACTIVATION_CYCLES_PER_ROUND;
54+
AttentionBrokerServer::EXTRA_ACTIVATION_CYCLES_PER_ROUND = 0;
5155
dasproto::HandleList handle_list0;
5256
dasproto::HandleList handle_list1;
5357
dasproto::HandleList handle_list2;
@@ -95,9 +99,54 @@ TEST(AttentionBrokerTest, get_importance) {
9599
EXPECT_TRUE(importance_list3.list(0) > 0.4);
96100
EXPECT_TRUE(importance_list3.list(1) > 0.4);
97101
EXPECT_TRUE(importance_list4.list(0) < 0.1);
98-
EXPECT_TRUE(importance_list4.list(1) > 0.4);
102+
double avg = (importance_list3.list(0) + importance_list4.list(0)) / 2;
103+
EXPECT_TRUE(Utils::is_zero(importance_list4.list(1) - avg));
104+
AttentionBrokerServer::EXTRA_ACTIVATION_CYCLES_PER_ROUND = save_parameter;
99105
}
100106

107+
TEST(AttentionBrokerTest, get_importance_multiple_cycle) {
108+
string* handles = build_handle_space(4);
109+
110+
AttentionBrokerServer service;
111+
unsigned int save_parameter = AttentionBrokerServer::EXTRA_ACTIVATION_CYCLES_PER_ROUND;
112+
AttentionBrokerServer::EXTRA_ACTIVATION_CYCLES_PER_ROUND = 2;
113+
dasproto::HandleList handle_list0;
114+
dasproto::HandleList handle_list1;
115+
dasproto::HandleList handle_list2;
116+
dasproto::HandleCount handle_count;
117+
dasproto::Ack ack;
118+
dasproto::ImportanceList importance_list1;
119+
dasproto::ImportanceList importance_list2;
120+
ServerContext* context = NULL;
121+
122+
(*handle_count.mutable_map())[handles[0]] = 1;
123+
(*handle_count.mutable_map())[handles[1]] = 1;
124+
(*handle_count.mutable_map())["SUM"] = 2;
125+
126+
handle_list0.add_list(handles[0]);
127+
handle_list0.add_list(handles[1]);
128+
handle_list0.add_list(handles[2]);
129+
handle_list0.add_list(handles[3]);
130+
handle_list1.add_list(handles[0]);
131+
handle_list1.add_list(handles[1]);
132+
handle_list2.add_list(handles[2]);
133+
handle_list2.add_list(handles[3]);
134+
135+
service.correlate(context, &handle_list0, &ack);
136+
Utils::sleep(1000);
137+
service.stimulate(context, &handle_count, &ack);
138+
Utils::sleep(1000);
139+
service.get_importance(context, &handle_list1, &importance_list1);
140+
service.get_importance(context, &handle_list2, &importance_list2);
141+
142+
EXPECT_TRUE(importance_list1.list(0) > 0.41);
143+
EXPECT_TRUE(importance_list1.list(1) > 0.41);
144+
EXPECT_TRUE(importance_list2.list(0) < 0.09);
145+
EXPECT_TRUE(importance_list2.list(1) < 0.09);
146+
AttentionBrokerServer::EXTRA_ACTIVATION_CYCLES_PER_ROUND = save_parameter;
147+
}
148+
149+
/*
101150
TEST(AttentionBrokerTest, save_and_load) {
102151
string* handles = build_handle_space(4);
103152

‎src/tests/cpp/stimulus_spreader_test.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ TEST(TokenSpreader, spread_stimuli) {
148148
(*request->mutable_map())[handles[5]] = 1;
149149
(*request->mutable_map())["SUM"] = 6;
150150
unsigned int SUM = (*request->mutable_map())["SUM"];
151-
spreader->spread_stimuli(request);
151+
spreader->spread_stimuli(request, false);
152152

153153
// ----------------------------------------------------------
154154
// Compute expected value for importance of each node

0 commit comments

Comments
 (0)