with open(corpus_file) as f: sents = [[twp.split('|')[0].lower() for twp in line.split()] for line in f] embeddings = helper.char_embeddings(sents) print('init network and compile theano functions...') h_size = hidden_layer_size # hidden size W_xi, W_hi, W_ci, b_i, \ W_xf, W_hf, W_cf, b_f, \ W_xc, W_hc, b_c, \ W_xo, W_ho, W_co, b_o, \ W_hy, b_y = helper.load_states(weights_file_name, work_dir) S_h = helper.init_zero_vec(h_size) # init values for hidden units S_c = helper.init_zero_vec(h_size) # init values for cell units S_x = T.matrix() # inputs # BEGIN code inspired by Christian Herta # http://christianherta.de/lehre/dataScience/machineLearning/neuralNetworks/LSTM.php def step(S_x, S_h, S_c, W_xi, W_hi, W_ci, b_i, W_xf, W_hf, W_cf, b_f, W_xc, W_hc, b_c, W_xo, W_ho, W_co, b_o, W_hy, b_y): S_i = sigm(dot(S_x, W_xi) + dot(S_h, W_hi) + dot(S_c, W_ci) + b_i) S_f = sigm(dot(S_x, W_xf) + dot(S_h, W_hf) + dot(S_c, W_cf) + b_f) S_c = S_f * S_c + S_i * tanh(dot(S_x, W_xc) + dot(S_h, W_hc) + b_c) S_o = sigm(dot(S_x, W_xo) + dot(S_h, W_ho) + dot(S_c, W_co) + b_o)
load_weights = None print('init new states...') print('timestamp: ', timestamp) print() # initialize lstm weights io_size = len(embeddings) # input/output size h_size = hidden_layer_size # hidden size if not load_weights: W_xi = helper.init_weights((io_size, h_size)) W_hi = helper.init_weights((h_size, h_size)) W_ci = helper.init_weights((h_size, h_size)) b_i = helper.init_zero_vec(h_size) W_xf = helper.init_weights((io_size, h_size)) W_hf = helper.init_weights((h_size, h_size)) W_cf = helper.init_weights((h_size, h_size)) b_f = helper.init_zero_vec(h_size) W_xc = helper.init_weights((io_size, h_size)) W_hc = helper.init_weights((h_size, h_size)) b_c = helper.init_zero_vec(h_size) W_xo = helper.init_weights((io_size, h_size)) W_ho = helper.init_weights((h_size, h_size)) W_co = helper.init_weights((h_size, h_size)) b_o = helper.init_zero_vec(h_size)
with open(corpus_file) as f: sents = [[twp.split('|')[0].lower() for twp in line.split()] for line in f] embeddings = helper.char_embeddings(sents) print('init network and compile theano functions...') h_size = hidden_layer_size # hidden size W_xi, W_hi, W_ci, b_i, \ W_xf, W_hf, W_cf, b_f, \ W_xc, W_hc, b_c, \ W_xo, W_ho, W_co, b_o, \ W_hy, b_y = helper.load_states(weights_file_name, work_dir) S_h = helper.init_zero_vec(h_size) # init values for hidden units S_c = helper.init_zero_vec(h_size) # init values for cell units S_x = T.matrix() # inputs # BEGIN code inspired by Christian Herta # http://christianherta.de/lehre/dataScience/machineLearning/neuralNetworks/LSTM.php def step(S_x, S_h, S_c, W_xi, W_hi, W_ci, b_i, W_xf, W_hf, W_cf, b_f, W_xc, W_hc, b_c, W_xo, W_ho, W_co, b_o, W_hy, b_y): S_i = sigm(dot(S_x, W_xi) + dot(S_h, W_hi) + dot(S_c, W_ci) + b_i)
print("init new states...") print("timestamp: ", timestamp) print() # initialize lstm weights inp = w2v_embeddings.embeddings_size # input/output size hid = hidden_layer_size # hidden size out = token_embeddings.num_tokens if not load_weights: W_xi = helper.init_weights((inp, hid)) W_hi = helper.init_weights((hid, hid)) W_ci = helper.init_weights((hid, hid)) b_i = helper.init_zero_vec(hid) W_xf = helper.init_weights((inp, hid)) W_hf = helper.init_weights((hid, hid)) W_cf = helper.init_weights((hid, hid)) b_f = helper.init_zero_vec(hid) W_xc = helper.init_weights((inp, hid)) W_hc = helper.init_weights((hid, hid)) b_c = helper.init_zero_vec(hid) W_xo = helper.init_weights((inp, hid)) W_ho = helper.init_weights((hid, hid)) W_co = helper.init_weights((hid, hid)) b_o = helper.init_zero_vec(hid)