OSCR

Awareness of being: a computational neurophenomenological model of mindfulness, mind-wandering, and meta-attentional control.

Code ↔ Paper

The paper beside its authors' code: matches between them have not been computed for this paper yet.

Paper

Loaded from Europe PMC by your browser, not stored by OSCR: doi.org · Europe PMC

The paper is loaded when this pane is shown.

The authors' code

C++ · 678 lines · 59 KB · Apache-2.0

  1. // Predictive-coding-inspired Variational RNN
  2. // error_regression.cpp
  3. // Copyright © 2022 Hayato Idei. All rights reserved.
  4. //
  5. #include "network.hpp"
  6. #include <sys/time.h>
  7. //hyper-parameter used in error regression
  8. #define TEST_SEQ_NUM 10 //sequence number of target data in error regression
  9. #define ADAM1 0.9
  10. #define ADAM2 0.999
  11. #define MAX_TIME_LENGTH 2200 //max time length including future states
  12. #define TIME_LENGTH 2000
  13. #define POINT 25
  14. #define CUE_STATE_STEP 5
  15. #define CUE_STATE_STEP_KEEP 10
  16. #define FOOD_CYCLE 100
  17. #define NO_FOOD_STEP 25
  18. #define GET_DISTANCE 0.2
  19. double sigmoid(double x, double bias, double gain)
  20. {
  21. return 1.0 / (1.0 + exp(-gain * (x-bias)));
  22. }
  23. //robot error regression
  24. int main(void){
  25. vector<vector<vector<double> > > real_state_extero(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_extero_num, 0)));
  26. vector<vector<vector<double> > > real_state_proprio(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_proprio_num, 0)));
  27. vector<vector<vector<double> > > real_state_intero(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_intero_num, 0)));
  28. vector<vector<vector<double> > > target(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_num, 0)));
  29. vector<vector<vector<double> > > target_extero(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_extero_num, 0)));
  30. vector<vector<vector<double> > > target_proprio(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_proprio_num, 0)));
  31. vector<vector<vector<double> > > joint_angle(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_proprio_num, 0)));
  32. vector<vector<vector<double> > > target_intero(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_intero_num, 0)));
  33. vector<vector<vector<double> > > pe_extero(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_extero_num, 0)));
  34. vector<vector<vector<double> > > pe_proprio(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_proprio_num, 0)));
  35. vector<vector<vector<double> > > pe_intero(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_intero_num, 0)));
  36. vector<vector<vector<double> > > wkl_exteroceptive(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(exteroceptive_z_num, 0)));
  37. vector<vector<vector<double> > > wkl_proprioceptive(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(proprioceptive_z_num, 0)));
  38. vector<vector<vector<double> > > wkl_interoceptive(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(interoceptive_z_num, 0)));
  39. vector<vector<vector<double> > > wkl_neuromodulation(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(neuromodulation_z_num, 0)));
  40. vector<vector<vector<double> > > wkl_associative(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(associative_z_num, 0)));
  41. vector<vector<vector<double> > > wkl_executive(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(executive_z_num, 0)));
  42. vector<vector<vector<double> > > in_p_mu_executive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(executive_z_num, 0)));
  43. vector<vector<vector<double> > > in_p_sigma_executive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(executive_z_num, 0)));
  44. vector<vector<vector<double> > > a_mu_executive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(executive_z_num, 0)));
  45. vector<vector<vector<double> > > a_sigma_executive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(executive_z_num, 0)));
  46. vector<vector<vector<double> > > wkl_executive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(executive_z_num, 0)));
  47. vector<vector<vector<double> > > in_p_mu_neuromodulation_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(neuromodulation_z_num, 0)));
  48. vector<vector<vector<double> > > in_p_sigma_neuromodulation_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(neuromodulation_z_num, 0)));
  49. vector<vector<vector<double> > > a_mu_neuromodulation_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(neuromodulation_z_num, 0)));
  50. vector<vector<vector<double> > > a_sigma_neuromodulation_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(neuromodulation_z_num, 0)));
  51. vector<vector<vector<double> > > wkl_neuromodulation_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(neuromodulation_z_num, 0)));
  52. vector<vector<vector<double> > > in_p_mu_associative_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(associative_z_num, 0)));
  53. vector<vector<vector<double> > > in_p_sigma_associative_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(associative_z_num, 0)));
  54. vector<vector<vector<double> > > a_mu_associative_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(associative_z_num, 0)));
  55. vector<vector<vector<double> > > a_sigma_associative_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(associative_z_num, 0)));
  56. vector<vector<vector<double> > > wkl_associative_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(associative_z_num, 0)));
  57. vector<vector<vector<double> > > in_p_mu_exteroceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(exteroceptive_z_num, 0)));
  58. vector<vector<vector<double> > > in_p_sigma_exteroceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(exteroceptive_z_num, 0)));
  59. vector<vector<vector<double> > > a_mu_exteroceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(exteroceptive_z_num, 0)));
  60. vector<vector<vector<double> > > a_sigma_exteroceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(exteroceptive_z_num, 0)));
  61. vector<vector<vector<double> > > wkl_exteroceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(exteroceptive_z_num, 0)));
  62. vector<vector<vector<double> > > in_p_mu_proprioceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(proprioceptive_z_num, 0)));
  63. vector<vector<vector<double> > > in_p_sigma_proprioceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(proprioceptive_z_num, 0)));
  64. vector<vector<vector<double> > > a_mu_proprioceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(proprioceptive_z_num, 0)));
  65. vector<vector<vector<double> > > a_sigma_proprioceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(proprioceptive_z_num, 0)));
  66. vector<vector<vector<double> > > wkl_proprioceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(proprioceptive_z_num, 0)));
  67. vector<vector<vector<double> > > in_p_mu_interoceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(interoceptive_z_num, 0)));
  68. vector<vector<vector<double> > > in_p_sigma_interoceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(interoceptive_z_num, 0)));
  69. vector<vector<vector<double> > > a_mu_interoceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(interoceptive_z_num, 0)));
  70. vector<vector<vector<double> > > a_sigma_interoceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(interoceptive_z_num, 0)));
  71. vector<vector<vector<double> > > wkl_interoceptive_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(interoceptive_z_num, 0)));
  72. vector<vector<vector<double> > > output_extero_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_extero_num, 0)));
  73. vector<vector<vector<double> > > output_proprio_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_proprio_num, 0)));
  74. vector<vector<vector<double> > > output_intero_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_intero_num, 0)));
  75. vector<vector<vector<double> > > sensory_sigma_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_num, 0)));
  76. vector<vector<vector<double> > > pe_extero_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_extero_num, 0)));
  77. vector<vector<vector<double> > > pe_proprio_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_proprio_num, 0)));
  78. vector<vector<vector<double> > > pe_intero_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_intero_num, 0)));
  79. vector<vector<vector<double> > > fe_past_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(1, 0)));
  80. vector<vector<vector<double> > > fe_future_window_head(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(1, 0)));
  81. vector<vector<double> > points(POINT, vector<double>(2, 0));
  82. for(int i=0; i<POINT; ++i){
  83. int x = i%5;
  84. int y = i/5;
  85. points[i][0] = -0.8 + x*0.4;
  86. points[i][1] = 0.8 - y*0.4;
  87. }
  88. //create food position
  89. uniform_int_distribution<> rand25(0, POINT-1);
  90. vector<vector<vector<double> > > food_position_state_sequence(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(3, 0)));// x, y, state
  91. int sample_point = rand25(engine);
  92. for(int s=0; s<TEST_SEQ_NUM; ++s){
  93. for(int t=0; t<TIME_LENGTH; ++t){
  94. if(t%FOOD_CYCLE==0){
  95. sample_point = rand25(engine);
  96. }
  97. food_position_state_sequence[s][t][0] = points[sample_point][0];
  98. food_position_state_sequence[s][t][1] = points[sample_point][1];
  99. }
  100. }
  101. //set model
  102. ER_PVRNNTopLayer executive(TEST_SEQ_NUM, MAX_TIME_LENGTH, executive_d_num, executive_z_num, executive_W, executive_W1, executive_tau, "executive");
  103. ER_PVRNNLayer associative(TEST_SEQ_NUM, MAX_TIME_LENGTH, associative_d_num, executive_d_num, associative_z_num, associative_W, associative_W1, associative_tau, "associative");
  104. ER_PVRNNLayer neuromodulation(TEST_SEQ_NUM, MAX_TIME_LENGTH, neuromodulation_d_num, executive_d_num, neuromodulation_z_num, neuromodulation_W, neuromodulation_W1, neuromodulation_tau, "neuromodulation");
  105. ER_PVRNNLayer exteroceptive(TEST_SEQ_NUM, MAX_TIME_LENGTH, exteroceptive_d_num, associative_d_num, exteroceptive_z_num, exteroceptive_W, exteroceptive_W1, exteroceptive_tau, "exteroceptive");
  106. ER_PVRNNLayer proprioceptive(TEST_SEQ_NUM, MAX_TIME_LENGTH, proprioceptive_d_num, associative_d_num, proprioceptive_z_num, proprioceptive_W, proprioceptive_W1, proprioceptive_tau, "proprioceptive");
  107. ER_PVRNNLayer interoceptive(TEST_SEQ_NUM, MAX_TIME_LENGTH, interoceptive_d_num, associative_d_num, interoceptive_z_num, interoceptive_W, interoceptive_W1, interoceptive_tau, "interoceptive");
  108. ER_Output out_extero(TEST_SEQ_NUM, MAX_TIME_LENGTH, x_extero_num, exteroceptive_d_num, "out_extero");
  109. ER_Output out_proprio(TEST_SEQ_NUM, MAX_TIME_LENGTH, x_proprio_num, proprioceptive_d_num, "out_proprio");
  110. ER_Output out_intero(TEST_SEQ_NUM, MAX_TIME_LENGTH, x_intero_num, interoceptive_d_num, "out_intero");
  111. ER_OutputNM out_nm(TEST_SEQ_NUM, MAX_TIME_LENGTH, x_num, neuromodulation_d_num, x_nm_tau, "out_nm");
  112. //concatenated variables used in backprop
  113. vector<vector<vector<double> > > x_mean(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_num, 0)));
  114. vector<vector<vector<double> > > x_sigma_extero(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_extero_num, 0)));
  115. vector<vector<vector<double> > > x_sigma_proprio(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_proprio_num, 0)));
  116. vector<vector<vector<double> > > x_sigma_intero(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(x_intero_num, 0)));
  117. vector<double> exte_prop_intero_tau(exteroceptive_d_num+proprioceptive_d_num+interoceptive_d_num, 0);
  118. vector<vector<double> > exte_prop_intero_weight_ld(exteroceptive_d_num+proprioceptive_d_num+interoceptive_d_num, vector<double>(associative_d_num, 0));
  119. vector<vector<vector<double> > > exte_prop_intero_grad_internal_state_d(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(exteroceptive_d_num+proprioceptive_d_num+interoceptive_d_num, 0)));
  120. vector<double> nm_ass_tau(neuromodulation_d_num+associative_d_num, 0);
  121. vector<vector<double> > nm_ass_weight_ld(neuromodulation_d_num+associative_d_num, vector<double>(executive_d_num, 0));
  122. vector<vector<vector<double> > > nm_ass_grad_internal_state_d(TEST_SEQ_NUM, vector<vector<double> >(MAX_TIME_LENGTH, vector<double>(neuromodulation_d_num+associative_d_num, 0)));
  123. //set variables related with sensory uncertainty
  124. normal_distribution<> dist_sn(0.0, sqrt(0.0001)); //sensory noise
  125. normal_distribution<> dist_normal(0.0, 1.0); //sensory noise caused by interoception
  126. vector<int> future_window_list = {200}; //time window in future
  127. vector<int> past_window_list ={10}; //time window in past
  128. vector<int> iteration_list = {200};
  129. vector<double> learning_rate_list = {0.1};
  130. int future_window_list_size = int(future_window_list.size());
  131. int past_window_list_size = int(past_window_list.size());
  132. int iteration_list_size = int(iteration_list.size());
  133. int learning_rate_list_size = int(learning_rate_list.size());
  134. for(int fw=0;fw<future_window_list_size;++fw){
  135. int future_window = future_window_list[fw];
  136. for(int pw=0;pw<past_window_list_size;++pw){
  137. int past_window = past_window_list[pw];
  138. for(int itr=0;itr<iteration_list_size;++itr){
  139. int iteration = iteration_list[itr];
  140. for(int lr=0;lr<learning_rate_list_size;++lr){
  141. double alpha = learning_rate_list[lr];
  142. //make save directory
  143. stringstream path_generation_executive;
  144. path_generation_executive << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha << "/executive";
  145. string str_path_generation_executive = path_generation_executive.str();
  146. mkdir(str_path_generation_executive.c_str(), 0777);
  147. stringstream path_generation_associative;
  148. path_generation_associative << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha << "/associative";
  149. string str_path_generation_associative = path_generation_associative.str();
  150. mkdir(str_path_generation_associative.c_str(), 0777);
  151. stringstream path_generation_neuromodulation;
  152. path_generation_neuromodulation << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha << "/neuromodulation";
  153. string str_path_generation_neuromodulation = path_generation_neuromodulation.str();
  154. mkdir(str_path_generation_neuromodulation.c_str(), 0777);
  155. stringstream path_generation_exteroceptive;
  156. path_generation_exteroceptive << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha << "/exteroceptive";
  157. string str_path_generation_exteroceptive = path_generation_exteroceptive.str();
  158. mkdir(str_path_generation_exteroceptive.c_str(), 0777);
  159. stringstream path_generation_proprioceptive;
  160. path_generation_proprioceptive << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha << "/proprioceptive";
  161. string str_path_generation_proprioceptive = path_generation_proprioceptive.str();
  162. mkdir(str_path_generation_proprioceptive.c_str(), 0777);
  163. stringstream path_generation_interoceptive;
  164. path_generation_interoceptive << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha << "/interoceptive";
  165. string str_path_generation_interoceptive = path_generation_interoceptive.str();
  166. mkdir(str_path_generation_interoceptive.c_str(), 0777);
  167. stringstream path_generation_out_extero;
  168. path_generation_out_extero << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha << "/out_extero";
  169. string str_path_generation_out_extero = path_generation_out_extero.str();
  170. mkdir(str_path_generation_out_extero.c_str(), 0777);
  171. stringstream path_generation_out_proprio;
  172. path_generation_out_proprio << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha << "/out_proprio";
  173. string str_path_generation_out_proprio = path_generation_out_proprio.str();
  174. mkdir(str_path_generation_out_proprio.c_str(), 0777);
  175. stringstream path_generation_out_intero;
  176. path_generation_out_intero << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha << "/out_intero";
  177. string str_path_generation_out_intero = path_generation_out_intero.str();
  178. mkdir(str_path_generation_out_intero.c_str(), 0777);
  179. stringstream path_generation_out_nm;
  180. path_generation_out_nm << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha << "/out_nm";
  181. string str_path_generation_out_nm = path_generation_out_nm.str();
  182. mkdir(str_path_generation_out_nm.c_str(), 0777);
  183. stringstream path_generation_food;
  184. path_generation_food << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha << "/food";
  185. string str_path_generation_food = path_generation_food.str();
  186. mkdir(str_path_generation_food.c_str(), 0777);
  187. stringstream path_generation_fe;
  188. path_generation_fe << "./test_generation_allostasis" << "/fw_" << future_window << "/pw_" << past_window << "/itr_" << iteration << "/lr_" << alpha;
  189. string str_path_generation_fe = path_generation_fe.str();
  190. double max_sigma_intero = sqrt(0.1);
  191. double cue_x = 0.0;
  192. double cue_y = 0.4;
  193. //error regression
  194. for(int s=0;s<TEST_SEQ_NUM;++s){
  195. //measure time
  196. struct timeval start, end;
  197. gettimeofday(&start, NULL);
  198. double interoceptive_state = 0.0; // initialize interoception
  199. double cue_state_x = -0.8;
  200. double cue_state_y = -0.8;
  201. int cue_flag = 1;
  202. int cue_count = 0;
  203. double cue_state_x_store = 0.0;
  204. double cue_state_y_store = 0.0;
  205. int hit_count = 0;
  206. for(int ct=0;ct<TIME_LENGTH;++ct){//ct: current time step
  207. printf("Future window size: %d, Past window size: %d, Iteration size: %d, Alpha: %lf, Sequence: %d, Time step: %d\n", future_window, past_window, iteration, alpha, s, ct);
  208. int initial_regression;
  209. (ct==0) ? (initial_regression=1) : (initial_regression=0);
  210. int er_past_window;
  211. (ct>=past_window-1) ? (er_past_window=past_window) : (er_past_window=ct+1);
  212. int lr_normalization = er_past_window+future_window;
  213. //int epoch_flag = 0;
  214. for(int epoch=0;epoch<iteration;++epoch){
  215. //Feedforward
  216. for(int wt=ct-er_past_window+1;wt<=ct+future_window;++wt){//wt: time step within time window
  217. int last_window_step=0;
  218. (wt>=ct) ? (last_window_step=1) : (last_window_step=0);
  219. executive.er_forward(wt, s, epoch, initial_regression, last_window_step, "posterior");
  220. associative.er_forward(executive.d, wt, s, epoch, initial_regression, last_window_step, "posterior");
  221. neuromodulation.er_forward(executive.d, wt, s, epoch, initial_regression, last_window_step, "posterior");
  222. exteroceptive.er_forward(associative.d, wt, s, epoch, initial_regression, last_window_step, "posterior");
  223. proprioceptive.er_forward(associative.d, wt, s, epoch, initial_regression, last_window_step, "posterior");
  224. interoceptive.er_forward(associative.d, wt, s, epoch, initial_regression, last_window_step, "posterior");
  225. out_extero.er_forward(exteroceptive.d, wt, s);
  226. out_proprio.er_forward(proprioceptive.d, wt, s);
  227. out_intero.er_forward(interoceptive.d, wt, s);
  228. out_nm.er_forward(neuromodulation.d, wt, s);
  229. //Set future target and so on
  230. for(int i = 0; i < x_num; ++i){
  231. if(i<x_extero_num){
  232. x_mean[s][wt][i] = out_extero.output[s][wt][i];
  233. x_sigma_extero[s][wt][i] = out_nm.output[s][wt][i];
  234. if(wt>=ct+1){
  235. target_extero[s][wt][i] = out_extero.output[s][wt][i];
  236. target[s][wt][i] = x_mean[s][wt][i];
  237. }
  238. }else if(i<x_extero_num+x_proprio_num){
  239. x_mean[s][wt][i] = out_proprio.output[s][wt][i-x_extero_num];
  240. x_sigma_proprio[s][wt][i-x_extero_num] = out_nm.output[s][wt][i];
  241. if(wt>=ct+1){
  242. target_proprio[s][wt][i-x_extero_num] = out_proprio.output[s][wt][i-x_extero_num];
  243. target[s][wt][i] = x_mean[s][wt][i];
  244. }
  245. }else{
  246. x_mean[s][wt][i] = out_intero.output[s][wt][i-x_extero_num-x_proprio_num];
  247. x_sigma_intero[s][wt][i-x_extero_num-x_proprio_num] = out_nm.output[s][wt][i];
  248. if(wt>=ct+1){
  249. target_intero[s][wt][i-x_extero_num-x_proprio_num] = out_intero.output[s][wt][i-x_extero_num-x_proprio_num];
  250. target[s][wt][i] = x_mean[s][wt][i];
  251. }
  252. }
  253. }
  254. }
  255. //set incoming target at the current time step (if online robot operation)
  256. //set through PID control using proprioceptive prediction in robot operation
  257. if(epoch==0){
  258. double std = max_sigma_intero*(sigmoid(-interoceptive_state, 0.5, 20) + sigmoid(interoceptive_state, 0.5, 20));
  259. if(ct%FOOD_CYCLE<=FOOD_CYCLE-NO_FOOD_STEP){
  260. food_position_state_sequence[s][ct][2] = 0.2*(1.0-1.0/(FOOD_CYCLE-NO_FOOD_STEP)*(ct%FOOD_CYCLE));
  261. }else{
  262. food_position_state_sequence[s][ct][2] = 0.0;
  263. }
  264. real_state_extero[s][ct][0] = cue_state_x;
  265. real_state_extero[s][ct][1] = cue_state_y;
  266. target_extero[s][ct][0] = cue_state_x + std*dist_normal(engine) + dist_sn(engine);
  267. target_extero[s][ct][1] = cue_state_y + std*dist_normal(engine) + dist_sn(engine);
  268. for(int i=0; i<x_extero_num; ++i){
  269. if(target_extero[s][ct][i] >= 1.0){
  270. target_extero[s][ct][i] = 1.0;
  271. }else if(target_extero[s][ct][i] <= -1.0){
  272. target_extero[s][ct][i] = -1.0;
  273. }
  274. target[s][ct][i]=target_extero[s][ct][i];
  275. }
  276. for(int i=0;i<x_proprio_num;++i){
  277. if(ct==0){
  278. joint_angle[s][ct][i] = PID(joint_angle[s][ct][i], out_proprio.output[s][ct][i]);
  279. }else{
  280. joint_angle[s][ct][i] = PID(joint_angle[s][ct-1][i], out_proprio.output[s][ct][i]);
  281. }
  282. real_state_proprio[s][ct][i] = joint_angle[s][ct][i];
  283. target_proprio[s][ct][i] = joint_angle[s][ct][i] + std*dist_normal(engine) + dist_sn(engine);
  284. if(target_proprio[s][ct][i] >= 1.0){
  285. target_proprio[s][ct][i] = 1.0;
  286. }else if(target_proprio[s][ct][i] <= -1.0){
  287. target_proprio[s][ct][i] = -1.0;
  288. }
  289. target[s][ct][i+x_extero_num] = target_proprio[s][ct][i];
  290. }
  291. real_state_intero[s][ct][0] = interoceptive_state;
  292. target_intero[s][ct][0] = interoceptive_state + std*dist_normal(engine) + dist_sn(engine);
  293. if(target_intero[s][ct][0] >= 1.0){
  294. target_intero[s][ct][0] = 1.0;
  295. }else if(target_intero[s][ct][0] <= -1.0){
  296. target_intero[s][ct][0] = -1.0;
  297. }
  298. target[s][ct][x_extero_num+x_proprio_num] = target_intero[s][ct][0];
  299. double food_distance = sqrt(pow(real_state_proprio[s][ct][0] - food_position_state_sequence[s][ct][0],2) + pow(real_state_proprio[s][ct][1] - food_position_state_sequence[s][ct][1],2));
  300. double cue_distance = sqrt(pow(real_state_proprio[s][ct][0] - cue_x,2) + pow(real_state_proprio[s][ct][1] - cue_y,2));
  301. double movement;
  302. if(ct==0){
  303. movement = 0.0;
  304. }else{
  305. movement = sqrt(pow(real_state_proprio[s][ct][0] - real_state_proprio[s][ct-1][0],2) + pow(real_state_proprio[s][ct][1] - real_state_proprio[s][ct-1][1],2));
  306. }
  307. interoceptive_state -= 0.013*(0.1+movement);
  308. //get food
  309. if(food_distance <= GET_DISTANCE){
  310. interoceptive_state += food_position_state_sequence[s][ct][2];
  311. hit_count++;
  312. }
  313. //get cue
  314. if(ct%FOOD_CYCLE>=FOOD_CYCLE-5){
  315. if(ct%FOOD_CYCLE==FOOD_CYCLE-5){
  316. cue_state_x_store = cue_state_x;
  317. cue_state_y_store = cue_state_y;
  318. }
  319. cue_state_x -= (cue_state_x_store+0.8)/5;
  320. cue_state_y -= (cue_state_y_store+0.8)/5;
  321. cue_flag = 1;
  322. cue_count = 0;
  323. }else{
  324. if(cue_flag==1){
  325. if(cue_distance <= GET_DISTANCE){
  326. cue_flag = 0;
  327. }
  328. }else if(cue_flag==0){
  329. cue_count++;
  330. if(cue_count<=CUE_STATE_STEP){
  331. cue_state_x += (food_position_state_sequence[s][ct][0]+0.8)/CUE_STATE_STEP;
  332. cue_state_y += (food_position_state_sequence[s][ct][1]+0.8)/CUE_STATE_STEP;
  333. }else if(cue_count>CUE_STATE_STEP && cue_count<=CUE_STATE_STEP+CUE_STATE_STEP_KEEP){
  334. cue_state_x = food_position_state_sequence[s][ct][0];
  335. cue_state_y = food_position_state_sequence[s][ct][1];
  336. }else if(cue_count>CUE_STATE_STEP+CUE_STATE_STEP_KEEP && cue_count<=2*CUE_STATE_STEP+CUE_STATE_STEP_KEEP){
  337. cue_state_x -= (food_position_state_sequence[s][ct][0]+0.8)/CUE_STATE_STEP;
  338. cue_state_y -= (food_position_state_sequence[s][ct][1]+0.8)/CUE_STATE_STEP;
  339. }else{
  340. cue_flag = 1;
  341. cue_count = 0;
  342. }
  343. }
  344. }
  345. if(interoceptive_state >= 1.0){
  346. interoceptive_state = 1.0;
  347. }else if(interoceptive_state <= -1.0){
  348. interoceptive_state = -1.0;
  349. }
  350. }
  351. //error
  352. double normalize = 1.0/lr_normalization;
  353. fe_past_window_head[s][ct][0] = 0.0;
  354. fe_future_window_head[s][ct][0] = 0.0;
  355. for(int wt=ct-er_past_window+1;wt<=ct+future_window;++wt){
  356. double future_entropy_constant_term;
  357. (wt<=ct) ? (future_entropy_constant_term=0.0) : (future_entropy_constant_term=1.0);
  358. double past_flag;
  359. (wt<=ct) ? (past_flag=1.0) : (past_flag=0.0);
  360. double future_flag;
  361. (wt<=ct) ? (future_flag=0.0) : (future_flag=1.0);
  362. for(int i=0; i<x_extero_num; ++i){
  363. pe_extero[s][wt][i] = 0.5*(pow((target_extero[s][wt][i]-out_extero.output[s][wt][i])/x_sigma_extero[s][wt][i],2)+log(2*PI)+2.0*log(x_sigma_extero[s][wt][i])+future_entropy_constant_term)*normalize/x_extero_num;
  364. fe_past_window_head[s][ct][0] += past_flag*pe_extero[s][wt][i];
  365. fe_future_window_head[s][ct][0] += future_flag*pe_extero[s][wt][i];
  366. }
  367. for(int i=0; i<x_proprio_num; ++i){
  368. pe_proprio[s][wt][i] = 0.5*(pow((target_proprio[s][wt][i]-out_proprio.output[s][wt][i])/x_sigma_proprio[s][wt][i],2)+log(2*PI)+2.0*log(x_sigma_proprio[s][wt][i])+future_entropy_constant_term)*normalize/x_proprio_num;
  369. fe_past_window_head[s][ct][0] += past_flag*pe_proprio[s][wt][i];
  370. fe_future_window_head[s][ct][0] += future_flag*pe_proprio[s][wt][i];
  371. }
  372. for(int i=0; i<x_intero_num; ++i){
  373. pe_intero[s][wt][i] = 0.5*(pow((target_intero[s][wt][i]-out_intero.output[s][wt][i])/x_sigma_intero[s][wt][i],2)+log(2*PI)+2.0*log(x_sigma_intero[s][wt][i])+future_entropy_constant_term)*normalize/x_intero_num;
  374. fe_past_window_head[s][ct][0] += past_flag*pe_intero[s][wt][i];
  375. fe_future_window_head[s][ct][0] += future_flag*pe_intero[s][wt][i];
  376. }
  377. for(int i=0; i<exteroceptive_z_num; ++i){
  378. wkl_exteroceptive[s][wt][i] = exteroceptive_W*(log(exteroceptive.p_sigma[s][wt][i])-log(exteroceptive.q_sigma[s][wt][i])+0.5*(pow(exteroceptive.p_mu[s][wt][i]-exteroceptive.q_mu[s][wt][i],2)+pow(exteroceptive.q_sigma[s][wt][i],2))/pow(exteroceptive.p_sigma[s][wt][i],2)-0.5)*normalize/exteroceptive_z_num;
  379. fe_past_window_head[s][ct][0] += past_flag*wkl_exteroceptive[s][wt][i];
  380. fe_future_window_head[s][ct][0] += future_flag*wkl_exteroceptive[s][wt][i];
  381. }
  382. for(int i=0; i<proprioceptive_z_num; ++i){
  383. wkl_proprioceptive[s][wt][i] = proprioceptive_W*(log(proprioceptive.p_sigma[s][wt][i])-log(proprioceptive.q_sigma[s][wt][i])+0.5*(pow(proprioceptive.p_mu[s][wt][i]-proprioceptive.q_mu[s][wt][i],2)+pow(proprioceptive.q_sigma[s][wt][i],2))/pow(proprioceptive.p_sigma[s][wt][i],2)-0.5)*normalize/proprioceptive_z_num;
  384. fe_past_window_head[s][ct][0] += past_flag*wkl_proprioceptive[s][wt][i];
  385. fe_future_window_head[s][ct][0] += future_flag*wkl_proprioceptive[s][wt][i];
  386. }
  387. for(int i=0; i<interoceptive_z_num; ++i){
  388. wkl_interoceptive[s][wt][i] = interoceptive_W*(log(interoceptive.p_sigma[s][wt][i])-log(interoceptive.q_sigma[s][wt][i])+0.5*(pow(interoceptive.p_mu[s][wt][i]-interoceptive.q_mu[s][wt][i],2)+pow(interoceptive.q_sigma[s][wt][i],2))/pow(interoceptive.p_sigma[s][wt][i],2)-0.5)*normalize/interoceptive_z_num;
  389. fe_past_window_head[s][ct][0] += past_flag*wkl_interoceptive[s][wt][i];
  390. fe_future_window_head[s][ct][0] += future_flag*wkl_interoceptive[s][wt][i];
  391. }
  392. for(int i=0; i<neuromodulation_z_num; ++i){
  393. wkl_neuromodulation[s][wt][i] = neuromodulation_W*(log(neuromodulation.p_sigma[s][wt][i])-log(neuromodulation.q_sigma[s][wt][i])+0.5*(pow(neuromodulation.p_mu[s][wt][i]-neuromodulation.q_mu[s][wt][i],2)+pow(neuromodulation.q_sigma[s][wt][i],2))/pow(neuromodulation.p_sigma[s][wt][i],2)-0.5)*normalize/neuromodulation_z_num;
  394. fe_past_window_head[s][ct][0] += past_flag*wkl_neuromodulation[s][wt][i];
  395. fe_future_window_head[s][ct][0] += future_flag*wkl_neuromodulation[s][wt][i];
  396. }
  397. for(int i=0; i<associative_z_num; ++i){
  398. wkl_associative[s][wt][i] = associative_W*(log(associative.p_sigma[s][wt][i])-log(associative.q_sigma[s][wt][i])+0.5*(pow(associative.p_mu[s][wt][i]-associative.q_mu[s][wt][i],2)+pow(associative.q_sigma[s][wt][i],2))/pow(associative.p_sigma[s][wt][i],2)-0.5)*normalize/associative_z_num;
  399. fe_past_window_head[s][ct][0] += past_flag*wkl_associative[s][wt][i];
  400. fe_future_window_head[s][ct][0] += future_flag*wkl_associative[s][wt][i];
  401. }
  402. for(int i=0; i<executive_z_num; ++i){
  403. wkl_executive[s][wt][i] = executive_W*(log(executive.p_sigma[s][wt][i])-log(executive.q_sigma[s][wt][i])+0.5*(pow(executive.p_mu[s][wt][i]-executive.q_mu[s][wt][i],2)+pow(executive.q_sigma[s][wt][i],2))/pow(executive.p_sigma[s][wt][i],2)-0.5)*normalize/executive_z_num;
  404. fe_past_window_head[s][ct][0] += past_flag*wkl_executive[s][wt][i];
  405. fe_future_window_head[s][ct][0] += future_flag*wkl_executive[s][wt][i];
  406. }
  407. }
  408. //Backward
  409. //set constant for Rectified Adam
  410. double rho_inf = 2.0/(1.0-ADAM2)-1.0;
  411. double rho = rho_inf-2.0*(epoch+1)*pow(ADAM2,epoch+1)/(1.0-pow(ADAM2,epoch+1));
  412. double r_store = sqrt((rho-4.0)*(rho-2.0)*rho_inf/ (rho_inf-4.0)/(rho_inf-2.0)/rho);
  413. double m_radam1 = 1.0/(1.0-pow(ADAM1,epoch+1));
  414. double l_radam2 = 1.0-pow(ADAM2,epoch+1);
  415. for(int wt=ct+future_window;wt>=ct-er_past_window+1;--wt){
  416. int last_window_step;
  417. (wt==ct+future_window) ? (last_window_step=1) : (last_window_step=0);
  418. out_intero.er_backward_nm(target_intero, x_sigma_intero, wt, s, lr_normalization);
  419. out_proprio.er_backward_nm(target_proprio, x_sigma_proprio, wt, s, lr_normalization);
  420. out_extero.er_backward_nm(target_extero, x_sigma_extero, wt, s, lr_normalization);
  421. out_nm.er_backward(target, x_mean, last_window_step, wt, s, lr_normalization);
  422. interoceptive.er_backward(out_intero.weight_ohd, out_intero.grad_internal_state_output, out_intero.tau, last_window_step, wt, s, lr_normalization);
  423. proprioceptive.er_backward(out_proprio.weight_ohd, out_proprio.grad_internal_state_output, out_proprio.tau, last_window_step, wt, s, lr_normalization);
  424. exteroceptive.er_backward(out_extero.weight_ohd, out_extero.grad_internal_state_output, out_extero.tau, last_window_step, wt, s, lr_normalization);
  425. neuromodulation.er_backward(out_nm.weight_ohd, out_nm.grad_internal_state_output, out_nm.tau, last_window_step, wt, s, lr_normalization);
  426. //concatenate some variables in proprioceptive and exteroceptive areas to make backprop-inputs to the associative area
  427. for(int i=0;i<exteroceptive_d_num+proprioceptive_d_num+interoceptive_d_num;++i){
  428. if(i<exteroceptive_d_num) {
  429. exte_prop_intero_grad_internal_state_d[s][wt][i] = exteroceptive.grad_internal_state_d[s][wt][i];
  430. exte_prop_intero_tau[i] = exteroceptive.tau[i];
  431. }else if(i<exteroceptive_d_num+proprioceptive_d_num){
  432. exte_prop_intero_grad_internal_state_d[s][wt][i] = proprioceptive.grad_internal_state_d[s][wt][i-exteroceptive_d_num];
  433. exte_prop_intero_tau[i] = proprioceptive.tau[i-exteroceptive_d_num];
  434. }else{
  435. exte_prop_intero_grad_internal_state_d[s][wt][i] = interoceptive.grad_internal_state_d[s][wt][i-exteroceptive_d_num-proprioceptive_d_num];
  436. exte_prop_intero_tau[i] = interoceptive.tau[i-exteroceptive_d_num-proprioceptive_d_num];
  437. }
  438. for(int j=0;j<associative_d_num;++j){
  439. if(i<exteroceptive_d_num){
  440. exte_prop_intero_weight_ld[i][j] = exteroceptive.weight_dhd[i][j];
  441. }else if(i<exteroceptive_d_num+proprioceptive_d_num){
  442. exte_prop_intero_weight_ld[i][j] = proprioceptive.weight_dhd[i-exteroceptive_d_num][j];
  443. }else{
  444. exte_prop_intero_weight_ld[i][j] = interoceptive.weight_dhd[i-exteroceptive_d_num-proprioceptive_d_num][j];
  445. }
  446. }
  447. }
  448. associative.er_backward(exte_prop_intero_weight_ld, exte_prop_intero_grad_internal_state_d, exte_prop_intero_tau, last_window_step, wt, s, lr_normalization);
  449. //concatenate some variables in associative and neuromodulation areas to make backprop-inputs to the executive area
  450. for(int i=0;i<neuromodulation_d_num+associative_d_num;++i){
  451. if(i<neuromodulation_d_num){
  452. nm_ass_grad_internal_state_d[s][wt][i] = neuromodulation.grad_internal_state_d[s][wt][i];
  453. nm_ass_tau[i] = neuromodulation.tau[i];
  454. }else{
  455. nm_ass_grad_internal_state_d[s][wt][i] = associative.grad_internal_state_d[s][wt][i-neuromodulation_d_num];
  456. nm_ass_tau[i] = associative.tau[i-neuromodulation_d_num];
  457. }
  458. for(int j=0;j<executive_d_num;++j){
  459. if(i<neuromodulation_d_num){
  460. nm_ass_weight_ld[i][j] = neuromodulation.weight_dhd[i][j];
  461. }else{
  462. nm_ass_weight_ld[i][j] = associative.weight_dhd[i-neuromodulation_d_num][j];
  463. }
  464. }
  465. }
  466. executive.er_backward(nm_ass_weight_ld, nm_ass_grad_internal_state_d, nm_ass_tau, last_window_step, wt, s, lr_normalization);
  467. //update adaptive vector (Rectified Adam)
  468. interoceptive.er_update_parameter_radam(wt, s, epoch, alpha, ADAM1, ADAM2, rho, r_store, m_radam1, l_radam2);
  469. proprioceptive.er_update_parameter_radam(wt, s, epoch, alpha, ADAM1, ADAM2, rho, r_store, m_radam1, l_radam2);
  470. exteroceptive.er_update_parameter_radam(wt, s, epoch, alpha, ADAM1, ADAM2, rho, r_store, m_radam1, l_radam2);
  471. neuromodulation.er_update_parameter_radam(wt, s, epoch, alpha, ADAM1, ADAM2, rho, r_store, m_radam1, l_radam2);
  472. associative.er_update_parameter_radam(wt, s, epoch, alpha, ADAM1, ADAM2, rho, r_store, m_radam1, l_radam2);
  473. executive.er_update_parameter_radam(wt, s, epoch, alpha, ADAM1, ADAM2, rho, r_store, m_radam1, l_radam2);
  474. }
  475. }
  476. //set timeseries at the head of the past time window
  477. for(int i=0; i<executive_z_num; ++i){
  478. in_p_mu_executive_window_head[s][ct][i] = executive.internal_state_p_mu[s][ct][i];
  479. in_p_sigma_executive_window_head[s][ct][i] = executive.internal_state_p_sigma[s][ct][i];
  480. a_mu_executive_window_head[s][ct][i] = executive.a_mu[s][ct][i];
  481. a_sigma_executive_window_head[s][ct][i] = executive.a_sigma[s][ct][i];
  482. wkl_executive_window_head[s][ct][i] = wkl_executive[s][ct][i];
  483. }
  484. for(int i=0; i<neuromodulation_z_num; ++i){
  485. in_p_mu_neuromodulation_window_head[s][ct][i] = neuromodulation.internal_state_p_mu[s][ct][i];
  486. in_p_sigma_neuromodulation_window_head[s][ct][i] = neuromodulation.internal_state_p_sigma[s][ct][i];
  487. a_mu_neuromodulation_window_head[s][ct][i] = neuromodulation.a_mu[s][ct][i];
  488. a_sigma_neuromodulation_window_head[s][ct][i] = neuromodulation.a_sigma[s][ct][i];
  489. wkl_neuromodulation_window_head[s][ct][i] = wkl_neuromodulation[s][ct][i];
  490. }
  491. for(int i=0; i<associative_z_num; ++i){
  492. in_p_mu_associative_window_head[s][ct][i] = associative.internal_state_p_mu[s][ct][i];
  493. in_p_sigma_associative_window_head[s][ct][i] = associative.internal_state_p_sigma[s][ct][i];
  494. a_mu_associative_window_head[s][ct][i] = associative.a_mu[s][ct][i];
  495. a_sigma_associative_window_head[s][ct][i] = associative.a_sigma[s][ct][i];
  496. wkl_associative_window_head[s][ct][i] = wkl_associative[s][ct][i];
  497. }
  498. for(int i=0; i<exteroceptive_z_num; ++i){
  499. in_p_mu_exteroceptive_window_head[s][ct][i] = exteroceptive.internal_state_p_mu[s][ct][i];
  500. in_p_sigma_exteroceptive_window_head[s][ct][i] = exteroceptive.internal_state_p_sigma[s][ct][i];
  501. a_mu_exteroceptive_window_head[s][ct][i] = exteroceptive.a_mu[s][ct][i];
  502. a_sigma_exteroceptive_window_head[s][ct][i] = exteroceptive.a_sigma[s][ct][i];
  503. wkl_exteroceptive_window_head[s][ct][i] = wkl_exteroceptive[s][ct][i];
  504. }
  505. for(int i=0; i<proprioceptive_z_num; ++i){
  506. in_p_mu_proprioceptive_window_head[s][ct][i] = proprioceptive.internal_state_p_mu[s][ct][i];
  507. in_p_sigma_proprioceptive_window_head[s][ct][i] = proprioceptive.internal_state_p_sigma[s][ct][i];
  508. a_mu_proprioceptive_window_head[s][ct][i] = proprioceptive.a_mu[s][ct][i];
  509. a_sigma_proprioceptive_window_head[s][ct][i] = proprioceptive.a_sigma[s][ct][i];
  510. wkl_proprioceptive_window_head[s][ct][i] = wkl_proprioceptive[s][ct][i];
  511. }
  512. for(int i=0; i<interoceptive_z_num; ++i){
  513. in_p_mu_interoceptive_window_head[s][ct][i] = interoceptive.internal_state_p_mu[s][ct][i];
  514. in_p_sigma_interoceptive_window_head[s][ct][i] = interoceptive.internal_state_p_sigma[s][ct][i];
  515. a_mu_interoceptive_window_head[s][ct][i] = interoceptive.a_mu[s][ct][i];
  516. a_sigma_interoceptive_window_head[s][ct][i] = interoceptive.a_sigma[s][ct][i];
  517. wkl_interoceptive_window_head[s][ct][i] = wkl_interoceptive[s][ct][i];
  518. }
  519. for(int i = 0; i < x_num; ++i){
  520. sensory_sigma_window_head[s][ct][i] = out_nm.output[s][ct][i];
  521. if(i<x_extero_num){
  522. output_extero_window_head[s][ct][i] = out_extero.output[s][ct][i];
  523. pe_extero_window_head[s][ct][i] = pe_extero[s][ct][i];
  524. }else if(i<x_extero_num+x_proprio_num){
  525. output_proprio_window_head[s][ct][i-x_extero_num] = out_proprio.output[s][ct][i-x_extero_num];
  526. pe_proprio_window_head[s][ct][i-x_extero_num] = pe_proprio[s][ct][i-x_extero_num];
  527. }else{
  528. output_intero_window_head[s][ct][i-x_extero_num-x_proprio_num] = out_intero.output[s][ct][i-x_extero_num-x_proprio_num];
  529. pe_intero_window_head[s][ct][i-x_extero_num-x_proprio_num] = pe_intero[s][ct][i-x_extero_num-x_proprio_num];
  530. }
  531. }
  532. //Save generated sequence
  533. if(ct==TIME_LENGTH-1){
  534. executive.er_save_sequence(str_path_generation_executive, TIME_LENGTH, s, ct);
  535. associative.er_save_sequence(str_path_generation_associative, TIME_LENGTH, s, ct);
  536. neuromodulation.er_save_sequence(str_path_generation_neuromodulation, TIME_LENGTH, s, ct);
  537. exteroceptive.er_save_sequence(str_path_generation_exteroceptive, TIME_LENGTH, s, ct);
  538. proprioceptive.er_save_sequence(str_path_generation_proprioceptive, TIME_LENGTH, s, ct);
  539. interoceptive.er_save_sequence(str_path_generation_interoceptive, TIME_LENGTH, s, ct);
  540. out_nm.er_save_sequence(str_path_generation_out_nm, TIME_LENGTH, s, ct);
  541. out_extero.er_save_sequence(str_path_generation_out_extero, target_extero, TIME_LENGTH, s, ct);
  542. out_proprio.er_save_sequence(str_path_generation_out_proprio, target_proprio, TIME_LENGTH, s, ct);
  543. out_intero.er_save_sequence(str_path_generation_out_intero, target_intero, TIME_LENGTH, s, ct);
  544. save_generated_sequence(str_path_generation_executive, "wkld", wkl_executive, TIME_LENGTH, s, ct);
  545. save_generated_sequence(str_path_generation_associative, "wkld", wkl_associative, TIME_LENGTH, s, ct);
  546. save_generated_sequence(str_path_generation_neuromodulation, "wkld", wkl_neuromodulation, TIME_LENGTH, s, ct);
  547. save_generated_sequence(str_path_generation_exteroceptive, "wkld", wkl_exteroceptive, TIME_LENGTH, s, ct);
  548. save_generated_sequence(str_path_generation_proprioceptive, "wkld", wkl_proprioceptive, TIME_LENGTH, s, ct);
  549. save_generated_sequence(str_path_generation_interoceptive, "wkld", wkl_interoceptive, TIME_LENGTH, s, ct);
  550. save_generated_sequence(str_path_generation_out_extero, "pe", pe_extero, TIME_LENGTH, s, ct);
  551. save_generated_sequence(str_path_generation_out_proprio, "pe", pe_proprio, TIME_LENGTH, s, ct);
  552. save_generated_sequence(str_path_generation_out_intero, "pe", pe_intero, TIME_LENGTH, s, ct);
  553. save_generated_sequence(str_path_generation_out_extero, "real", real_state_extero, TIME_LENGTH, s, ct);
  554. save_generated_sequence(str_path_generation_out_proprio, "real", real_state_proprio, TIME_LENGTH, s, ct);
  555. save_generated_sequence(str_path_generation_out_intero, "real", real_state_intero, TIME_LENGTH, s, ct);
  556. save_generated_sequence(str_path_generation_food, "food", food_position_state_sequence, TIME_LENGTH, s, ct);
  557. //save timeseries at the head of the past time window
  558. save_generated_sequence(str_path_generation_executive, "in_p_mu_window_head", in_p_mu_executive_window_head, TIME_LENGTH, s, ct);
  559. save_generated_sequence(str_path_generation_executive, "in_p_sigma_window_head", in_p_sigma_executive_window_head, TIME_LENGTH, s, ct);
  560. save_generated_sequence(str_path_generation_executive, "a_mu_window_head", a_mu_executive_window_head, TIME_LENGTH, s, ct);
  561. save_generated_sequence(str_path_generation_executive, "a_sigma_window_head", a_sigma_executive_window_head, TIME_LENGTH, s, ct);
  562. save_generated_sequence(str_path_generation_executive, "wkld_window_head", wkl_executive_window_head, TIME_LENGTH, s, ct);
  563. save_generated_sequence(str_path_generation_neuromodulation, "in_p_mu_window_head", in_p_mu_neuromodulation_window_head, TIME_LENGTH, s, ct);
  564. save_generated_sequence(str_path_generation_neuromodulation, "in_p_sigma_window_head", in_p_sigma_neuromodulation_window_head, TIME_LENGTH, s, ct);
  565. save_generated_sequence(str_path_generation_neuromodulation, "a_mu_window_head", a_mu_neuromodulation_window_head, TIME_LENGTH, s, ct);
  566. save_generated_sequence(str_path_generation_neuromodulation, "a_sigma_window_head", a_sigma_neuromodulation_window_head, TIME_LENGTH, s, ct);
  567. save_generated_sequence(str_path_generation_neuromodulation, "wkld_window_head", wkl_neuromodulation_window_head, TIME_LENGTH, s, ct);
  568. save_generated_sequence(str_path_generation_associative, "in_p_mu_window_head", in_p_mu_associative_window_head, TIME_LENGTH, s, ct);
  569. save_generated_sequence(str_path_generation_associative, "in_p_sigma_window_head", in_p_sigma_associative_window_head, TIME_LENGTH, s, ct);
  570. save_generated_sequence(str_path_generation_associative, "a_mu_window_head", a_mu_associative_window_head, TIME_LENGTH, s, ct);
  571. save_generated_sequence(str_path_generation_associative, "a_sigma_window_head", a_sigma_associative_window_head, TIME_LENGTH, s, ct);
  572. save_generated_sequence(str_path_generation_associative, "wkld_window_head", wkl_associative_window_head, TIME_LENGTH, s, ct);
  573. save_generated_sequence(str_path_generation_exteroceptive, "in_p_mu_window_head", in_p_mu_exteroceptive_window_head, TIME_LENGTH, s, ct);
  574. save_generated_sequence(str_path_generation_exteroceptive, "in_p_sigma_window_head", in_p_sigma_exteroceptive_window_head, TIME_LENGTH, s, ct);
  575. save_generated_sequence(str_path_generation_exteroceptive, "a_mu_window_head", a_mu_exteroceptive_window_head, TIME_LENGTH, s, ct);
  576. save_generated_sequence(str_path_generation_exteroceptive, "a_sigma_window_head", a_sigma_exteroceptive_window_head, TIME_LENGTH, s, ct);
  577. save_generated_sequence(str_path_generation_exteroceptive, "wkld_window_head", wkl_exteroceptive_window_head, TIME_LENGTH, s, ct);
  578. save_generated_sequence(str_path_generation_proprioceptive, "in_p_mu_window_head", in_p_mu_proprioceptive_window_head, TIME_LENGTH, s, ct);
  579. save_generated_sequence(str_path_generation_proprioceptive, "in_p_sigma_window_head", in_p_sigma_proprioceptive_window_head, TIME_LENGTH, s, ct);
  580. save_generated_sequence(str_path_generation_proprioceptive, "a_mu_window_head", a_mu_proprioceptive_window_head, TIME_LENGTH, s, ct);
  581. save_generated_sequence(str_path_generation_proprioceptive, "a_sigma_window_head", a_sigma_proprioceptive_window_head, TIME_LENGTH, s, ct);
  582. save_generated_sequence(str_path_generation_proprioceptive, "wkld_window_head", wkl_proprioceptive_window_head, TIME_LENGTH, s, ct);
  583. save_generated_sequence(str_path_generation_interoceptive, "in_p_mu_window_head", in_p_mu_interoceptive_window_head, TIME_LENGTH, s, ct);
  584. save_generated_sequence(str_path_generation_interoceptive, "in_p_sigma_window_head", in_p_sigma_interoceptive_window_head, TIME_LENGTH, s, ct);
  585. save_generated_sequence(str_path_generation_interoceptive, "a_mu_window_head", a_mu_interoceptive_window_head, TIME_LENGTH, s, ct);
  586. save_generated_sequence(str_path_generation_interoceptive, "a_sigma_window_head", a_sigma_interoceptive_window_head, TIME_LENGTH, s, ct);
  587. save_generated_sequence(str_path_generation_interoceptive, "wkld_window_head", wkl_interoceptive_window_head, TIME_LENGTH, s, ct);
  588. save_generated_sequence(str_path_generation_out_extero, "output_window_head", output_extero_window_head, TIME_LENGTH, s, ct);
  589. save_generated_sequence(str_path_generation_out_proprio, "output_window_head", output_proprio_window_head, TIME_LENGTH, s, ct);
  590. save_generated_sequence(str_path_generation_out_intero, "output_window_head", output_intero_window_head, TIME_LENGTH, s, ct);
  591. save_generated_sequence(str_path_generation_out_nm, "output_window_head", sensory_sigma_window_head, TIME_LENGTH, s, ct);
  592. save_generated_sequence(str_path_generation_out_extero, "pe_window_head", pe_extero_window_head, TIME_LENGTH, s, ct);
  593. save_generated_sequence(str_path_generation_out_proprio, "pe_window_head", pe_proprio_window_head, TIME_LENGTH, s, ct);
  594. save_generated_sequence(str_path_generation_out_intero, "pe_window_head", pe_intero_window_head, TIME_LENGTH, s, ct);
  595. save_generated_sequence(str_path_generation_fe, "fe_past_window_head", fe_past_window_head, TIME_LENGTH, s, ct);
  596. save_generated_sequence(str_path_generation_fe, "fe_future_window_head", fe_future_window_head, TIME_LENGTH, s, ct);
  597. }
  598. }
  599. printf("Final Interoceptive state: %lf \n", interoceptive_state);
  600. gettimeofday(&end, NULL);
  601. float delta = end.tv_sec - start.tv_sec + (float)(end.tv_usec - start.tv_usec) / 1000000;
  602. printf("time %lf[s]\n", delta);
  603. }
  604. }
  605. }
  606. }
  607. }
  608. return 0;
  609. }

error_regression_allostasis.cpp at commit 1501fca, under Apache-2.0 · at the source

Overview

Authors: Hayato Idei1, Keisuke Suzuki2, Yuichi Yamashita1
ORCID iDs: Hayato Idei
  1. Department of Information Medicine, National Institute of Neuroscience, National Center of Neurology and Psychiatry, 4-1-1 Ogawa-Higashi, Kodaira, Tokyo 187-8502, Japan
  2. Center for Human Nature, Artificial Intelligence, and Neuroscience (CHAIN), Hokkaido University, Kita 12 Nishi 7, Kita-ku, Sapporo, Hokkaido 060-0812, Japan
Journal: Neuroscience of consciousness, volume 2026, issue 1, article niag046
Dates: received 14 October 2025; accepted 21 July 2026; published online 5 September 2026
Type: Research article · Language: English
License: CC BY
Identifiers: DOI 10.1093/nc/niag046 · PMID 42701647 · PMCID PMC13545998 · OpenAlex W4402948919
Open access: gold, a free copy (OpenAlex)
Status: code verified
Categories: computational modeling (no new data) (modality), none (in silico) (organism)
Methods: Statistics, Machine learning
Keywords: computational neurophenomenology, mindfulness, mind-wandering, allostasis, interoception, variational recurrent neural network
Topic: Mind wandering and attention (Cognitive Neuroscience, Neuroscience), according to OpenAlex
Funding: Intramural Research Grant (6-9, 4-6); JSPS KAKENHI (JP24K00499, JP24H00076, JP20H00625); JST Moonshot R&D (JPMJMS2031); AMED Multidisciplinary Frontier Brain and Neuroscience Discoveries (JP24wm0625407); JST, CREST (JPMJCR21P4); JST ACT-X (JPMJAX24C2); JSPS Research Fellows (JP22KJ3167, JP22J01708)
Citations: not cited yet (Europe PMC); 53 references in the paper

Abstract

Mindfulness has established psychological benefits, such as stress reduction and emotional regulation; however, the underlying computational mechanisms, particularly in relation to mind-wandering, remain unclear. This study aimed to present a hierarchical recurrent neural network-based agent model grounded in the free energy principle to explore these processes within the framework of allostasis. The model integrates interoceptive, proprioceptive, and exteroceptive predictive processing, which minimizes the variational free energy throughout past and future contexts. Simulations of the resting state showed that inferred (mentally simulated) homeostatic context modulated attentional shifts between a ‘being’ mode (focused on present interoceptive perception) and a ‘doing’ mode (involving mentally simulated proprioceptive and exteroceptive sensorimotor behaviors). Further, we identified a meta-attentional parameter that controls these shifts by influencing cognitive attitudes toward internally generated beliefs across network modules spanning from the past to the future. By manipulating this parameter, we replicated mindfulness states (‘deep-being’ mode) in which attention remained focused on the present interoceptive state, despite minimal sensory prediction errors. This study offers insights into the computational dynamics of mindfulness and mind-wandering, laying the groundwork for the future exploration of consciousness and pure awareness, which is a state of consciousness devoid of content.

Reproduced under the paper's license (CC BY), from the paper cited above.

Repository

Its files are read in the Code ↔ Paper reader above.

h-idei/mbhrnn

License: Apache-2.0
State: the link answers, verified on 26 September 2026
Evidence: files inventoried
Commit: 1501fca90602149585e34266b81cfa9513d5108a, 3 April 2024
Languages: C++ (2), Python (2)
Size: 12 files, 4 scripts
Software Heritage: archived
Found in: “Data availability”
Holds: README, license file
Not found: CITATION.cff, environment file, tests, continuous integration, documentation
Tools: Matplotlib (2 files), NumPy (2 files)
Availability: 1 check, the latest on 26 September 2026: the link answers
  • 26 September 2026: the link answers
6 files

The paper's code and data availability statement is in the Data section.

Tracing map

Proposed by the machine: these links were found in the paper and verified at the source, without human review. The map will receive a Zenodo DOI once one of the paper's authors has validated it with their ORCID.

What the map holds:

  • 1 repository of the authors' code, each at its verified commit, with its license and how the link was found in the paper;
  • 4 scripts, each with its path and the digest of its content;
  • no match between paragraphs and code yet;
  • neither the text of the paper nor the code itself.

Its JSON (tracing-map.json) is deposited on Zenodo with its DOI once the map is validated.

Data

No dataset and no data link were found in the paper.

Data availability

All data are available in the manuscript and supplementary material. Computer code for the MBH-RNN was written using C++ and is available at https://github.com/h-idei/mbhrnn.git.

Reproduced under the paper's license (CC BY), from the paper cited above.

Versions

The history of this record: each version stored by the harvester or made by a correction of its authors or of the maintainers of its code, and what changed in its facts. The texts of the paper (its abstract, its availability statements) are not part of it; versions that changed only those are not listed.

Version 1, 27 September 2026: the first record

Recorded: type, language, journal, volume, issue, pages, dates, 3 authors, 6 keywords, 7 funders, 52 references.

Cite

This paper

Idei, H., Suzuki, K., & Yamashita, Y. (2026). Awareness of being: a computational neurophenomenological model of mindfulness, mind-wandering, and meta-attentional control. Neuroscience of consciousness, 2026(1), niag046. https://doi.org/10.1093/nc/niag046

BibTeX

@article{idei2026awareness,
author = {Idei, Hayato and Suzuki, Keisuke and Yamashita, Yuichi},
title = {{Awareness of being: a computational neurophenomenological model of mindfulness, mind-wandering, and meta-attentional control}},
journal = {Neuroscience of consciousness},
year = {2026},
month = sep,
volume = {2026},
number = {1},
pages = {niag046},
publisher = {Oxford University Press},
issn = {2057-2107},
doi = {10.1093/nc/niag046},
url = {https://doi.org/10.1093/nc/niag046},
pmid = {42701647},
pmcid = {PMC13545998}
}

RIS

TY - JOUR
AU - Idei, Hayato
AU - Suzuki, Keisuke
AU - Yamashita, Yuichi
TI - Awareness of being: a computational neurophenomenological model of mindfulness, mind-wandering, and meta-attentional control
T2 - Neuroscience of consciousness
J2 - Neurosci Conscious
PY - 2026
DA - 2026/09/05
VL - 2026
IS - 1
SP - niag046
SN - 2057-2107
PB - Oxford University Press
DO - 10.1093/nc/niag046
UR - https://doi.org/10.1093/nc/niag046
LA - en
ER -

CSL-JSON

{
"id": "10.1093/nc/niag046",
"type": "article-journal",
"title": "Awareness of being: a computational neurophenomenological model of mindfulness, mind-wandering, and meta-attentional control",
"container-title": "Neuroscience of consciousness",
"author": [
{
"family": "Idei",
"given": "Hayato"
},
{
"family": "Suzuki",
"given": "Keisuke"
},
{
"family": "Yamashita",
"given": "Yuichi"
}
],
"container-title-short": "Neurosci Conscious",
"volume": "2026",
"issue": "1",
"page": "niag046",
"DOI": "10.1093/nc/niag046",
"PMID": "42701647",
"PMCID": "PMC13545998",
"ISSN": "2057-2107",
"publisher": "Oxford University Press",
"URL": "https://doi.org/10.1093/nc/niag046",
"language": "en",
"issued": {
"date-parts": [
[
2026,
9,
5
]
]
}
}

The tracing map gets a citation of its own once an author has validated it and it has a DOI.

Similar papers

The papers with a page that share the most with this one: the tools found in their code, their categories, datasets, cited references and authors, the rarest counting most.

[1] doi:10.7554/elife.90013 [code]
Exposure to false cardiac feedback alters pain perception and anticipatory cardiac frequency.
Journal: eLife
In common: 6 references
[2] doi:10.1126/sciadv.aeb3326 [code]
Insular routing to orbitofrontal cortex enables breathing awareness.
Journal: Science advances
In common: 4 references
[3] doi:10.1038/s41467-026-74953-6 [code]
In-scanner thoughts contribute to resting-state functional connectivity.
Journal: Nature communications
In common: Matplotlib, NumPy, 2 references
[4] doi:10.1093/braincomms/fcag120 [code]
The heartbeat evoked potential and the prediction of functional seizure semiology.
Journal: Brain communications
In common: Matplotlib, NumPy, 2 references
[5] doi:10.1038/s41556-026-02009-4 [code]
An atlas of primate insular cortex reveals a signal-processing strategy in von Economo neurons.
Journal: Nature cell biology
In common: Matplotlib, NumPy, 2 references
[6] doi:10.1038/s42256-026-01254-4 [code]
Neural sampling from cognitive maps enables goal-directed imagination and planning.
Journal: Nature machine intelligence
In common: Matplotlib, NumPy, none (in silico), computational modeling (no new data), 1 reference
[7] doi:10.3389/fncom.2026.1813959 [code]
Coherent-resonant netting: disorder-enhanced selectivity from transient wave-like dynamics on biological connectomes.
Journal: Frontiers in computational neuroscience
In common: Matplotlib, NumPy, 2 references
[8] doi:10.1038/s41467-026-71568-9 [code]
Convergent and selective representations of pain, appetitive processes, aversive processes, and cognitive control in the insula.
Journal: Nature communications
In common: Matplotlib, NumPy, 2 references
[9] doi:10.1002/advs.77165
Perceived Time Shapes Physical Fatigue Accumulation and Its Neural Oscillatory Correlates.
Journal: Advanced science (Weinheim, Baden-Wurttemberg, Germany)
In common: 3 references
[10] doi:10.3390/e28060702 [code]
ICU Delirium as a Failure of Predictive Synchronization: A Two-Agent Active Inference Model.
Journal: Entropy (Basel, Switzerland)
In common: Matplotlib, NumPy, 2 references

Contribute

The authors of this paper can claim it, correct its record and validate its tracing map, and the maintainers of its code (its owner, or a public member of its organization) correct what it says of their repository; anyone signed in can ask for its removal. Every request goes to OSCR's own machine, which answers it; your account page follows them.

Sign in with ORCID to claim this paper as one of its authors, correct its record or validate its tracing map: when the paper's metadata lists your ORCID iD, you are recognized at once. Maintainers of its code: sign in with GitHub, then claim the repository on your account page.

Request its removal

To ask OSCR to remove this record, the copies of its authors' scripts or its tracing map, use the removal request page: signed in, you say who you are, what to remove and why, then review and confirm the request. Published rules decide every request (how).

Discussion, reproductions, activity

Discussion: questions and error reports about this paper and its code, from signed-in readers and its authors. It opens with sign-in.

Reproductions: reports from readers who ran the authors' code: what they reproduced, with which environment, commit and data. It opens with sign-in.

Activity: what happens around this paper: new versions of its record, its map's validation, discussions and reproductions. It opens with sign-in.