📄 dlanguage.cpp
字号:
/** * @file dlanguage.cpp * @brief Language simulation based on the punnets library. * * See the paper ``<a href="http://www.snowelm.com/~t/research/pub/abst.en.html#Makino:2002a:xe">A Pulsed Neural Network for Language Understanding: Discrete-Event Simulation of a Short-Term Memory Mechanism and Sentence Understanding</a>'' for details. * In short, we assign one representer neuron (<var>exts</var>) to represent each word. * Once fired, a representer neuron periodically fires to keep short-term memory of the word. * Representer neurons are interconnected via two networks, named autoassociative network (sgates) * and heteroassociative network (dgates). * When the network receives several words in a sequence, the two networks computes the bindings * of the words, and as a result, the meaning of the input sentence is represented by the * activation patter of the representer neurons (synchronized neurons have been bound). * The connection weights of the two networks are obtained from the external files. * * @author Makino, Takaki <t-makino-punnets01@snowelm.com> * @date 2003-05-01 * @version $Id: dlanguage.cpp,v 1.7 2003/05/08 08:23:56 t Exp $ * * Copyright (C) 2003 Makino, Takaki. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ const bool test_applier = false; //const bool test_applier = true;#include <stdlib.h>#include <sstream>#include <fstream>#include <iomanip>#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <math.h>#include <string>#ifdef HAVE_HASH_MAP#include <hash_map>#endif#ifdef HAVE_EXT_HASH_MAP#include <ext/hash_map>#endif#include <mak/hash_string.h>#include "punnets.h"using namespace __gnu_cxx;using namespace std;using namespace mak;using namespace punnets;const real a1_offset = 1.0;const real weight_scale = 1.0;const ntime_t coindet_duration_period = 0.002;const real coindet_offset = 0.0015;const ntime_t inhibitor_delay = 0.00001;const ntime_t gates_delay = 0.00001;const real synchro_gate_delay = 0.001;const real delayed_gate_delay = 0.1;const real delayed_gate_gap = 0.009;const real synchro_hidden_offset = 0.0002;const real delayed_hidden_offset = 0.03;const ntime_t suppress_delay = -0.0001;const ntime_t coindet_delay = 0.00003;const ntime_t incoindet_delay = 0.003 - inhibitor_delay;const ntime_t sgate_sig_hv_period = 0.003;const ntime_t sgate_thr_hv_period = 0.003;const ntime_t dgate_sig_hv_period = 0.0045;const ntime_t dgate_thr_hv_period = 0.01;const real sgate_hidden_thr_raise = 16;const real dgate_hidden_thr_raise = 32;const real coindet_suppress_weight = 15.0;const real delay_weight_decrease = 0.5;const real synch_weight_decrease = 0.0;const real inhibitor_suppress_weight = 30.0;const ntime_t synch_minus_preceding = 0.00001;const ntime_t delay_minus_preceding = 0.00045;const ntime_t test_until = 3.25;enum pos_t { pos_n1, pos_n3, pos_n, pos_pn3, pos_vi, pos_vt, pos_vt_subj, pos_det, pos_max};const string posstrs[] = { "pronoun 1st", "pronoun 3rd", "noun", "proper noun", "vi", "vt", "vt_subj", "det" };enum flg_t{ flg_none = 0, flg_nom = 1, flg_acc = 2,// flg_poss = 4, flg_plu = 8, flg_male = 16, flg_female = 32, flg_food = 64, flg_reading = 128, flg_countable = 128, flg_love = 256,};struct word_t { string word; pos_t pos; int flag;};const word_t words[] = { { "I", pos_n1, flg_nom }, { "me", pos_n1, flg_acc }, { "he", pos_n3, flg_nom | flg_male}, { "him", pos_n3, flg_acc | flg_male }, { "she", pos_n3, flg_nom | flg_female }, { "her", pos_n3, flg_acc | flg_female },// { "they", pos_n3, flg_nom | flg_plu },// { "them", pos_n3, flg_acc | flg_plu }, { "Mary", pos_pn3, flg_female }, { "John", pos_pn3, flg_male }, { "Susan", pos_pn3, flg_female }, { "Mike", pos_pn3, flg_male }, { "man", pos_n, flg_male | flg_countable }, { "lady", pos_n, flg_female | flg_countable }, { "boy", pos_n, flg_male | flg_countable }, { "girl", pos_n, flg_female | flg_countable }, { "book", pos_n, flg_reading | flg_countable }, { "paper", pos_n, flg_reading }, { "mail", pos_n, flg_reading | flg_countable }, { "bread", pos_n, flg_food }, { "lemon", pos_n, flg_food | flg_countable }, { "banana", pos_n, flg_food | flg_countable }, { "runs", pos_vi, 0 }, { "sleeps", pos_vi, 0 }, { "smiles", pos_vi, 0 }, { "likes", pos_vt, flg_love | flg_reading | flg_food }, { "(liker)", pos_vt_subj, flg_love | flg_reading | flg_food }, { "has", pos_vt, flg_reading | flg_food }, { "(owner)", pos_vt_subj, flg_reading | flg_food }, { "loves", pos_vt, flg_love }, { "(lover)", pos_vt_subj, flg_love }, { "reads", pos_vt, flg_reading }, { "(reader)", pos_vt_subj, flg_reading }, { "eats", pos_vt, flg_food }, { "(eater)", pos_vt_subj, flg_food }, { "a", pos_det, flg_countable }, { "the", pos_det, 0 }};hash_map<string, unsigned int> str_to_wordid;const unsigned int NWORDS = sizeof(words) / sizeof(word_t);const unsigned int NELEMS = (int)pos_max;struct neuinfo { string layername; unsigned int nneuron; tneuron_base *ref; neuinfo() { } neuinfo(string il, unsigned int in, tneuron_base *ir) : layername(il), nneuron(in), ref(ir) { }}; void read_file(vector<tneuron_ext *> &neuin, vector<tneuron *> &neuhid, vector<tneuron *> &neuout, tneuron &inhibitor, tneuron &coindet, string fname, ntime_t delay1, ntime_t delay_input_gap, real decay_level, real hidden_offset, bool synch){ tneuron &incoindet = inhibitor; cout << "Read file " << fname << ", decay_level=" << decay_level << endl; const bool debug_readfile = false; ifstream ifs(fname.c_str()); string s; ifs >> s; hash_map<string, neuinfo, hash_string> nmap; while(ifs.good()) { if( s != "layer" ) { cout << "input file bad <" << s << ">" << endl; abort();} string layername; int layersize; ifs >> layername; if( debug_readfile ) cout << "layer " << layername << endl; ifs >> s; while( ifs.good() ) { if( s == "size" ) { ifs >> layersize; if( debug_readfile ) cout << "size " << layersize << endl; ifs >> s; } else if( s == "neurons" ) { if( debug_readfile ) cout << "neurons " << layersize << endl; for( int i=0; i<layersize; i++ ) { ifs >> s; if( layername == "a1_layer" )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -