def get_data( fn, n_vis, n_val ): if fn.isdigit(): #random num_data = int(fn) prob = nd.ones([num_data * n_vis, n_val]) / n_val dat_lst = nd.sample_multinomial(prob) sys.stderr.write("Generating random data: nv= %d, nd= %d\n" % (n_vis, num_data)) else: #read from file with open(fn, 'r') as fp: lines = fp.readlines() es = lines[0].split() nl = int(es[0]) nv = int(es[1]) dat_lst = [] sys.stderr.write("Loading data: nv= %d, nd= %d\n" % (nv, nl)) for l in range(1, nl+1): dat_lst.append([]) for i in range(nv): bin_val = int2binary(lines[l][i]) for j in range(2): dat_lst[l-1].append(int(bin_val[j])) data = nd.array(dat_lst) #data = nd.one_hot(dat_lst, n_val).reshape([-1, n_vis * n_val]) return data
def sample_v_given_h(self, h0): v1_prob = self.propdown(h0).reshape([-1, self.n_val]) v1_prob = nd.softmax(v1_prob) v1_args = nd.sample_multinomial(v1_prob) v1 = nd.one_hot(v1_args, self.n_val) return [ v1_prob.reshape([-1, self.n_node]), v1.reshape([-1, self.n_node]) ]
if step % 100 == 0: print('Epoch [%d/%d], Step[%d/%d], Loss: %.3f, Perplexity: %5.2f' % (epoch + 1, num_epochs, step, num_batches, (loss / 1000).sum().asscalar(), (loss / 1000).sum().exp().asscalar())) # Sampling with open(sample_path, 'w') as f: # Set intial hidden ane memory states states = [ nd.zeros((num_layers, 1, hidden_size), ctx=ctx), nd.zeros((num_layers, 1, hidden_size), ctx=ctx) ] # Select one word id randomly prob = nd.ones((vocab_size, )) / vocab_size input1 = nd.sample_multinomial(prob).reshape((1, 1)).as_in_context(ctx) for i in range(num_samples): output, state = model(input1, states) # Sample a word id prob = nd.exp(output).reshape((-1, )) prob = prob / prob.sum() word_id = nd.sample_multinomial(prob, 1) input1 = word_id.reshape((1, 1)) word = corpus.dictionary.idx2word[word_id.asscalar()] word = '\n' if word == '<eos>' else word + ' ' f.write(word) if (i + 1) % 100 == 0: print('Sampled [%d/%d] words and save to %s' % (i + 1, num_samples, sample_path))