def test(model): dim = 128 v_size = 7810 margin = 1.0 #load model f = open(model, 'rb') input_params = cPickle.load(f) emb, wx, wh, bh, wa = input_params f.close() embLayer = emb_layer(pre_train=emb, v = v_size, dim = dim) rnnLayer = rnn_layer(input=None, wx=wx, wh=wh, bh=bh, emb_layer = embLayer, nh = dim) att = attention_layer(input=None, rnn_layer=rnnLayer, margin = margin) q = T.lvector('q') a = T.lscalar('a') p = T.lvector('p') t = T.lscalar('t') inputs = [q,a,p,t] score = att.predict(inputs) pred = theano.function(inputs=inputs,outputs=score) pool = ThreadPool() f = open('./data/test-small.id','r') count = 1 print 'time_b:%s' %time.clock() to_pred = [] for line in f: if count % 10000 == 0: print count / 10000 count += 1 #print 'time_b:%s' %time.clock() line = line[:-1] tmp = line.split('\t') in_q = numpy.array(tmp[0].split(' ')).astype(numpy.int) - 1 in_a = int(tmp[1].split(' ')[2]) - 1 in_p = numpy.array(tmp[1].split(' ')).astype(numpy.int) - 1 in_t = int(tmp[2]) - 1 lis = (in_q, in_a, in_p, in_t) to_pred.append(lis) #print 'time_load:%s' %time.clock() #print 'time_score:%s' %time.clock() f.close() ay = numpy.asarray(to_pred) #results = map(pred, list(ay[:,0]), list(ay[:,1]),list(ay[:,2]),list(ay[:,3])) results = pool.map(pred, to_pred) #results = [] #for p in to_pred: # results.append(att.predict(p,params)) print 'time_e:%s' %time.clock() #print results pool.close() pool.join()
def test(model): dim = 128 v_size = 7810 margin = 1.0 #load model f = open(model, 'rb') input_params = cPickle.load(f) emb, wx, wh, bh, wa = input_params f.close() embLayer = emb_layer(pre_train=emb, v = v_size, dim = dim) rnnLayer = rnn_layer(input=None, wx=wx, wh=wh, bh=bh, emb_layer = embLayer, nh = dim) att = attention_layer(input=None, rnn_layer=rnnLayer, margin = margin) q = T.lvector('q') a = T.lscalar('a') p = T.lvector('p') t = T.lscalar('t') inputs = [q,a,p,t] #emb_num = T.lscalar('emb_num') #nh = T.scalar('nh') #dim = T.scalar('dim') score = att.predict(inputs) pred = theano.function(inputs=inputs,outputs=score) wf = open('./data/res','w') f = open('./data/test.id','r') count = 1 print 'time_b:%s' %time.clock() for line in f: if count % 10000 == 0: print count / 10000 print 'time_1w:%s' %time.clock() count += 1 #print 'time_b:%s' %time.clock() line = line[:-1] tmp = line.split('\t') in_q = numpy.array(tmp[0].split(' ')).astype(numpy.int) - 1 #x = emb[q].reshape((q.shape[0], emb.shape[1])) in_a = int(tmp[1].split(' ')[2]) - 1 in_p = numpy.array(tmp[1].split(' ')).astype(numpy.int) - 1 in_t = int(tmp[2]) - 1 #in_lis = [in_q, in_a, in_p, in_t] #print 'time_load:%s' %time.clock() s = pred(in_q, in_a, in_p, in_t) #print s wf.write(str(s) + '\n') #print 'time_score:%s' %time.clock() f.close() wf.close()