-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgive_valid_test.py
54 lines (40 loc) · 1.74 KB
/
give_valid_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import torch
import os
import torch.nn as nn
batch_size = 128
def make_batch(train_path, word2number_dict, n_step):
def word2number(n):
try:
return word2number_dict[n]
except:
return 1 #<unk_word>
all_input_batch = []
all_target_batch = []
text = open(train_path, 'r', encoding='utf-8') #open the file
input_batch = []
target_batch = []
for sen in text:
word = sen.strip().split(" ") # space tokenizer
word = ["<sos>"] + word
word = word + ["<eos>"]
if len(word) <= n_step: #pad the sentence
word = ["<pad>"]*(n_step+1-len(word)) + word
for word_index in range(len(word)-n_step):
input = [word2number(n) for n in word[word_index:word_index+n_step]] # create (1~n-1) as input
target = word2number(word[word_index+n_step]) # create (n) as target, We usually call this 'casual language model'
input_batch.append(input)
target_batch.append(target)
if len(input_batch) == batch_size:
all_input_batch.append(input_batch)
all_target_batch.append(target_batch)
input_batch = []
target_batch = []
return all_input_batch, all_target_batch
def give_valid(data_path, word2number_dict, n_step):
valid_path = os.path.join(data_path, 'valid.txt')
all_input_batch, all_target_batch = make_batch(valid_path, word2number_dict, n_step)
return all_input_batch, all_target_batch
def give_test(data_path, word2number_dict, n_step):
test_psth = os.path.join(data_path, 'test.txt')
all_input_batch, all_target_batch = make_batch(test_psth, word2number_dict, n_step)
return all_input_batch, all_target_batch