MCPcopy Create free account
hub / github.com/apache/singa / pad_batch_2vec

Function pad_batch_2vec

examples/rnn/imdb_data.py:68–93  ·  view source on GitHub ↗

convert a batch of encoded sequence to pretrained word vectors from the embed weights (lookup dictionary)

(b, seq_limit, embed_weights)

Source from the content-addressed store, hash-verified

66
67
68def 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
96def check_exist_or_download(url):

Callers 1

imdb_train.pyFile · 0.90

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected