@@ -0,0 +1,1129 @@
+import numpy as np%0Afrom singa import tensor%0Aimport pickle%0A%0Adef load_data(PATH=''):%0A    # read data control dictionaries%0A    with open(PATH + 'metadata.pkl', 'rb') as f:%0A        metadata = pickle.load(f)%0A    # read numpy arrays%0A    idx_q = np.load(PATH + 'idx_q.npy')%0A    idx_a = np.load(PATH + 'idx_a.npy')%0A    return metadata, idx_q, idx_a%0A%0A%0Adef numpy2tensors(num, dev):%0A    '''batch, seq, dim -- %3E seq, batch, dim'''%0A    tmpx = np.swapaxes(num, 0, 1)%0A    inputs = %5B%5D%0A    for t in range(tmpx.shape%5B0%5D):%0A        x = tensor.from_numpy(tmpx%5Bt%5D)%0A        x.to_device(dev)%0A        inputs.append(x)%0A    return inputs%0Adef convert(batch, dev):%0A    '''convert a batch of data into a sequence of input tensors'''%0A    return numpy2tensors(batch, dev)%0Adef labelconvert(batch, batch_size, seq_length, vocab_size, dev):%0A    x1 = batch%5B:, :seq_length%5D%0A    x = np.zeros((batch_size, seq_length, vocab_size), dtype=np.float32)%0A    for b in range(batch_size):%0A        for t in range(seq_length):%0A            c = x1%5Bb, t%5D%0A            x%5Bb, t, c%5D = 1%0A    return numpy2tensors(x, dev)%0Aif __name__ == '__main__':%0A    metadata, idx_q, idx_a=load_data()%0A

Create word2tensor.py