Skip to content

Commit 720a264

Browse files
committed
* rename DataSet.get_fields() into get_all_fields()
* add DataSet.get_field(), to fetch a FieldArray based on its name * remove old tutorials & add new tutorials
1 parent 447746d commit 720a264

13 files changed

+2215
-1004
lines changed

fastNLP/api/processor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def __init__(self, field_dict, default=False):
268268
self.default = default
269269

270270
def process(self, dataset):
271-
set_dict = {name: self.default for name in dataset.get_fields().keys()}
271+
set_dict = {name: self.default for name in dataset.get_all_fields().keys()}
272272
set_dict.update(self.field_dict)
273273
dataset._set_need_tensor(**set_dict)
274274
return dataset
@@ -282,7 +282,7 @@ def __init__(self, field_dict, default=False):
282282
self.default = default
283283

284284
def process(self, dataset):
285-
set_dict = {name: self.default for name in dataset.get_fields().keys()}
285+
set_dict = {name: self.default for name in dataset.get_all_fields().keys()}
286286
set_dict.update(self.field_dict)
287287
dataset.set_target(**set_dict)
288288
return dataset

fastNLP/core/batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __next__(self):
4343

4444
indices = self.idx_list[self.curidx:endidx]
4545

46-
for field_name, field in self.dataset.get_fields().items():
46+
for field_name, field in self.dataset.get_all_fields().items():
4747
if field.is_target or field.is_input:
4848
batch = field.get(indices)
4949
if not self.as_numpy:

fastNLP/core/dataset.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ def delete_field(self, name):
157157
"""
158158
self.field_arrays.pop(name)
159159

160-
def get_fields(self):
160+
def get_field(self, field_name):
161+
if field_name not in self.field_arrays:
162+
raise KeyError("Field name {} not found in DataSet".format(field_name))
163+
return self.field_arrays[field_name]
164+
165+
def get_all_fields(self):
161166
"""Return all the fields with their names.
162167
163168
:return dict field_arrays: the internal data structure of DataSet.

fastNLP/core/sampler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(self, num_buckets=10, batch_size=32, seq_lens_field_name='seq_lens'
5555

5656
def __call__(self, data_set):
5757

58-
seq_lens = data_set.get_fields()[self.seq_lens_field_name].content
58+
seq_lens = data_set.get_all_fields()[self.seq_lens_field_name].content
5959
total_sample_num = len(seq_lens)
6060

6161
bucket_indexes = []

fastNLP/models/cnn_text_classification.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def forward(self, word_seq):
4444
x = self.conv_pool(x) # [N,L,C] -> [N,C]
4545
x = self.dropout(x)
4646
x = self.fc(x) # [N,C] -> [N, N_class]
47-
return {'output': x}
47+
return {'pred': x}
4848

4949
def predict(self, word_seq):
5050
"""
@@ -53,5 +53,5 @@ def predict(self, word_seq):
5353
:return predict: dict of torch.LongTensor, [batch_size, seq_len]
5454
"""
5555
output = self(word_seq)
56-
_, predict = output['output'].max(dim=1)
57-
return {'predict': predict}
56+
_, predict = output['pred'].max(dim=1)
57+
return {'pred': predict}

test/core/test_dataset.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33

44
from fastNLP.core.dataset import DataSet
5+
from fastNLP.core.fieldarray import FieldArray
56
from fastNLP.core.instance import Instance
67

78

@@ -162,6 +163,21 @@ def test_save_load(self):
162163
ds_1 = DataSet.load("./my_ds.pkl")
163164
os.remove("my_ds.pkl")
164165

166+
def test_get_all_fields(self):
167+
ds = DataSet({"x": [[1, 2, 3, 4]] * 10, "y": [[5, 6]] * 10})
168+
ans = ds.get_all_fields()
169+
self.assertEqual(ans["x"].content, [[1, 2, 3, 4]] * 10)
170+
self.assertEqual(ans["y"].content, [[5, 6]] * 10)
171+
172+
def test_get_field(self):
173+
ds = DataSet({"x": [[1, 2, 3, 4]] * 10, "y": [[5, 6]] * 10})
174+
ans = ds.get_field("x")
175+
self.assertTrue(isinstance(ans, FieldArray))
176+
self.assertEqual(ans.content, [[1, 2, 3, 4]] * 10)
177+
ans = ds.get_field("y")
178+
self.assertTrue(isinstance(ans, FieldArray))
179+
self.assertEqual(ans.content, [[5, 6]] * 10)
180+
165181

166182
class TestDataSetIter(unittest.TestCase):
167183
def test__repr__(self):

test/data_for_tests/tutorial_sample_dataset.csv

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,43 @@ There 's very little sense to what 's going on here , but the makers serve up th
3535
Cattaneo should have followed the runaway success of his first film , The Full Monty , with something different . 2
3636
They 're the unnamed , easily substitutable forces that serve as whatever terror the heroes of horror movies try to avoid . 1
3737
It almost feels as if the movie is more interested in entertaining itself than in amusing us . 1
38-
The movie 's progression into rambling incoherence gives new meaning to the phrase ` fatal script error . ' 0
38+
The movie 's progression into rambling incoherence gives new meaning to the phrase ` fatal script error . ' 0
39+
I still like Moonlight Mile , better judgment be damned . 3
40+
A welcome relief from baseball movies that try too hard to be mythic , this one is a sweet and modest and ultimately winning story . 3
41+
a bilingual charmer , just like the woman who inspired it 3
42+
Like a less dizzily gorgeous companion to Mr. Wong 's In the Mood for Love -- very much a Hong Kong movie despite its mainland setting . 2
43+
As inept as big-screen remakes of The Avengers and The Wild Wild West . 1
44+
It 's everything you 'd expect -- but nothing more . 2
45+
Best indie of the year , so far . 4
46+
Hatfield and Hicks make the oddest of couples , and in this sense the movie becomes a study of the gambles of the publishing world , offering a case study that exists apart from all the movie 's political ramifications . 3
47+
It 's like going to a house party and watching the host defend himself against a frothing ex-girlfriend . 1
48+
That the Chuck Norris `` grenade gag '' occurs about 7 times during Windtalkers is a good indication of how serious-minded the film is . 2
49+
The plot is romantic comedy boilerplate from start to finish . 2
50+
It arrives with an impeccable pedigree , mongrel pep , and almost indecipherable plot complications . 2
51+
A film that clearly means to preach exclusively to the converted . 2
52+
I still like Moonlight Mile , better judgment be damned . 3
53+
A welcome relief from baseball movies that try too hard to be mythic , this one is a sweet and modest and ultimately winning story . 3
54+
a bilingual charmer , just like the woman who inspired it 3
55+
Like a less dizzily gorgeous companion to Mr. Wong 's In the Mood for Love -- very much a Hong Kong movie despite its mainland setting . 2
56+
As inept as big-screen remakes of The Avengers and The Wild Wild West . 1
57+
It 's everything you 'd expect -- but nothing more . 2
58+
Best indie of the year , so far . 4
59+
Hatfield and Hicks make the oddest of couples , and in this sense the movie becomes a study of the gambles of the publishing world , offering a case study that exists apart from all the movie 's political ramifications . 3
60+
It 's like going to a house party and watching the host defend himself against a frothing ex-girlfriend . 1
61+
That the Chuck Norris `` grenade gag '' occurs about 7 times during Windtalkers is a good indication of how serious-minded the film is . 2
62+
The plot is romantic comedy boilerplate from start to finish . 2
63+
It arrives with an impeccable pedigree , mongrel pep , and almost indecipherable plot complications . 2
64+
A film that clearly means to preach exclusively to the converted . 2
65+
I still like Moonlight Mile , better judgment be damned . 3
66+
A welcome relief from baseball movies that try too hard to be mythic , this one is a sweet and modest and ultimately winning story . 3
67+
a bilingual charmer , just like the woman who inspired it 3
68+
Like a less dizzily gorgeous companion to Mr. Wong 's In the Mood for Love -- very much a Hong Kong movie despite its mainland setting . 2
69+
As inept as big-screen remakes of The Avengers and The Wild Wild West . 1
70+
It 's everything you 'd expect -- but nothing more . 2
71+
Best indie of the year , so far . 4
72+
Hatfield and Hicks make the oddest of couples , and in this sense the movie becomes a study of the gambles of the publishing world , offering a case study that exists apart from all the movie 's political ramifications . 3
73+
It 's like going to a house party and watching the host defend himself against a frothing ex-girlfriend . 1
74+
That the Chuck Norris `` grenade gag '' occurs about 7 times during Windtalkers is a good indication of how serious-minded the film is . 2
75+
The plot is romantic comedy boilerplate from start to finish . 2
76+
It arrives with an impeccable pedigree , mongrel pep , and almost indecipherable plot complications . 2
77+
A film that clearly means to preach exclusively to the converted . 2

0 commit comments

Comments
 (0)