def train(eta, iters): for it in xrange(iters): i = random.randint(0, len(text)/seq_len) j = i * seq_len X = text[j:(j+seq_len)] Y = text[(j+1):(j+1+seq_len)] print "iteration: %s, cost: %s" % (str(it), str(rnn.train(one_hot(X), one_hot(Y), eta, 1.0)))
def train(eta, iters): for it in xrange(iters): i = random.randint(0, len(text) / seq_len) j = i * seq_len X = text[j:(j + seq_len)] Y = text[(j + 1):(j + 1 + seq_len)] print "iteration: %s, cost: %s" % ( str(it), str(rnn.train(one_hot(X), one_hot(Y), eta, 1.0)))
def non_personalized_train(dnodex, eta, iters, ntusers): for it in xrange(iters): i = random.randint(0,len(dnodex.plist)-1) while len(dnodex.plist[i])<=2 or i in dnodex.test_track: i=random.randint(0,len(dnodex.plist)-1) X = dnodex.plist[i][:-1] Y = dnodex.plist[i][1:] lossf=str(rnn.train(one_hot(X,len(format(dnodex.npoi,'b'))), one_hot(Y,len(format(dnodex.npoi,'b'))), eta, 1.0)) if it%500==0: print "iteration: %s, cost: %s" % (str(it), lossf)
def infer_stochastic(rnn, k, temperature, start_char=" "): x = [one_hot(start_char).flatten()] for i in xrange(k): probs = rnn.predict_char(x, temperature) p = np.asarray(probs[0], dtype="float64") p /= p.sum() sample = np.random.multinomial(1, p) sys.stdout.write(one_hot_to_string(sample)) x = [sample] rnn.reset_state()
def infer_stochastic(dnodex, rnn): precision=0 test_case=0.0000001 for test_index in dnodex.test_track: test=dnodex.plist[test_index] if test_index%100==0: sys.stdout.write('\r%d prediction finished, current precision: %4f,%f,%f...' % (test_index,precision/test_case, precision, test_case)) sys.stdout.flush() if test_index>=500: break if len(test)==1: continue for index in range(len(test)-1): x = [one_hot([test[index]],len(format(dnodex.npoi,'b'))).flatten()] probs = rnn.predict_char(x, 1) p = np.asarray(probs[0], dtype="float64") p /= p.sum() sample = np.random.multinomial(len(p), p) res=one_hot_to_string(sample) if res==test[index+1]: precision+=1.0 test_case+=1.0 rnn.reset_state() print 'Precision: ', precision/test_case