convert a batch of encoded sequence to pretrained word vectors from the embed weights (lookup dictionary)
(b, seq_limit, embed_weights)
| 66 | |
| 67 | |
| 68 | def pad_batch_2vec(b, seq_limit, embed_weights): |
| 69 | ''' convert a batch of encoded sequence |
| 70 | to pretrained word vectors from the embed weights (lookup dictionary) |
| 71 | ''' |
| 72 | batch_seq = [] |
| 73 | batch_senti_onehot = [] |
| 74 | batch_senti = [] |
| 75 | for r in b: |
| 76 | # r[0] encoded sequence |
| 77 | # r[1] label 1 or 0 |
| 78 | encoded = None |
| 79 | if len(r[0]) >= seq_limit: |
| 80 | encoded = r[0][:seq_limit] |
| 81 | else: |
| 82 | encoded = r[0] + [0] * (seq_limit - len(r[0])) |
| 83 | |
| 84 | batch_seq.append([embed_weights[idx] for idx in encoded]) |
| 85 | batch_senti.append(r[1]) |
| 86 | if r[1] == 1: |
| 87 | batch_senti_onehot.append([0, 1]) |
| 88 | else: |
| 89 | batch_senti_onehot.append([1, 0]) |
| 90 | batch_senti = np.array(batch_senti).astype(np.float32) |
| 91 | batch_senti_onehot = np.array(batch_senti_onehot).astype(np.float32) |
| 92 | batch_seq = np.array(batch_seq).astype(np.float32) |
| 93 | return batch_seq, batch_senti_onehot, batch_senti |
| 94 | |
| 95 | |
| 96 | def check_exist_or_download(url): |