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