@@ -1,5 +1,5 @@ | |||
import torch | |||
import numpy as np | |||
import torch | |||
class Batch(object): | |||
@@ -60,9 +60,10 @@ class Batch(object): | |||
def __len__(self): | |||
return self.num_batches | |||
def to_tensor(batch, dtype): | |||
if dtype in (np.int8, np.int16, np.int32, np.int64): | |||
batch = torch.LongTensor(batch) | |||
if dtype in (np.float32, np.float64): | |||
batch = torch.FloatTensor(batch) | |||
return batch | |||
return batch |
@@ -174,7 +174,7 @@ class DataSet(object): | |||
self.field_arrays[new_name] = self.field_arrays.pop(old_name) | |||
self.field_arrays[new_name].name = new_name | |||
else: | |||
raise KeyError("{} is not a valid name. ".format(old_name)) | |||
raise KeyError("DataSet has no field named {}.".format(old_name)) | |||
def set_target(self, *field_names, flag=True): | |||
"""Change the target flag of these fields. | |||
@@ -208,8 +208,6 @@ class DataSet(object): | |||
@classmethod | |||
def set_reader(cls, method_name): | |||
"""decorator to add dataloader support | |||
""" | |||
assert isinstance(method_name, str) | |||
def wrapper(read_cls): | |||
@@ -275,6 +273,15 @@ class DataSet(object): | |||
@classmethod | |||
def read_csv(cls, csv_path, headers=None, sep=",", dropna=True): | |||
"""Load data from a CSV file and return a DataSet object. | |||
:param str csv_path: path to the CSV file | |||
:param List[str] or Tuple[str] headers: headers of the CSV file | |||
:param str sep: delimiter in CSV file. Default: "," | |||
:param bool dropna: If True, drop rows that have less entries than headers. | |||
:return DataSet dataset: | |||
""" | |||
with open(csv_path, "r") as f: | |||
start_idx = 0 | |||
if headers is None: | |||
@@ -28,15 +28,16 @@ class Tester(object): | |||
self.metrics = _prepare_metrics(metrics) | |||
self.data = data | |||
if torch.cuda.is_available() and self.use_cuda: | |||
self._model = model.cuda() | |||
else: | |||
self._model = model | |||
self.use_cuda = use_cuda | |||
self.batch_size = batch_size | |||
self.verbose = verbose | |||
self._model_device = model.parameters().__next__().device | |||
if torch.cuda.is_available() and self.use_cuda: | |||
self._model = model.cuda() | |||
else: | |||
self._model = model | |||
# check predict | |||
if hasattr(self._model, 'predict'): | |||
self._predict_func = self._model.predict | |||
@@ -22,8 +22,8 @@ class TestCase1(unittest.TestCase): | |||
def test_dataset_batching(self): | |||
ds = DataSet({"x": [[1, 2, 3, 4]] * 40, "y": [[5, 6]] * 40}) | |||
ds.set_input(x=True) | |||
ds.set_target(y=True) | |||
ds.set_input("x") | |||
ds.set_target("y") | |||
iter = Batch(ds, batch_size=4, sampler=SequentialSampler(), as_numpy=True) | |||
for x, y in iter: | |||
self.assertTrue(isinstance(x["x"], np.ndarray) and isinstance(y["y"], np.ndarray)) | |||
@@ -1,10 +1,6 @@ | |||
the 0.418 0.24968 -0.41242 0.1217 0.34527 -0.044457 -0.49688 -0.17862 -0.00066023 -0.6566 0.27843 -0.14767 -0.55677 0.14658 -0.0095095 0.011658 0.10204 -0.12792 -0.8443 -0.12181 -0.016801 -0.33279 -0.1552 -0.23131 -0.19181 -1.8823 -0.76746 0.099051 -0.42125 -0.19526 4.0071 -0.18594 -0.52287 -0.31681 0.00059213 0.0074449 0.17778 -0.15897 0.012041 -0.054223 -0.29871 -0.15749 -0.34758 -0.045637 -0.44251 0.18785 0.0027849 -0.18411 -0.11514 -0.78581 | |||
, 0.013441 0.23682 -0.16899 0.40951 0.63812 0.47709 -0.42852 -0.55641 -0.364 -0.23938 0.13001 -0.063734 -0.39575 -0.48162 0.23291 0.090201 -0.13324 0.078639 -0.41634 -0.15428 0.10068 0.48891 0.31226 -0.1252 -0.037512 -1.5179 0.12612 -0.02442 -0.042961 -0.28351 3.5416 -0.11956 -0.014533 -0.1499 0.21864 -0.33412 -0.13872 0.31806 0.70358 0.44858 -0.080262 0.63003 0.32111 -0.46765 0.22786 0.36034 -0.37818 -0.56657 0.044691 0.30392 | |||
. 0.15164 0.30177 -0.16763 0.17684 0.31719 0.33973 -0.43478 -0.31086 -0.44999 -0.29486 0.16608 0.11963 -0.41328 -0.42353 0.59868 0.28825 -0.11547 -0.041848 -0.67989 -0.25063 0.18472 0.086876 0.46582 0.015035 0.043474 -1.4671 -0.30384 -0.023441 0.30589 -0.21785 3.746 0.0042284 -0.18436 -0.46209 0.098329 -0.11907 0.23919 0.1161 0.41705 0.056763 -6.3681e-05 0.068987 0.087939 -0.10285 -0.13931 0.22314 -0.080803 -0.35652 0.016413 0.10216 | |||
of 0.70853 0.57088 -0.4716 0.18048 0.54449 0.72603 0.18157 -0.52393 0.10381 -0.17566 0.078852 -0.36216 -0.11829 -0.83336 0.11917 -0.16605 0.061555 -0.012719 -0.56623 0.013616 0.22851 -0.14396 -0.067549 -0.38157 -0.23698 -1.7037 -0.86692 -0.26704 -0.2589 0.1767 3.8676 -0.1613 -0.13273 -0.68881 0.18444 0.0052464 -0.33874 -0.078956 0.24185 0.36576 -0.34727 0.28483 0.075693 -0.062178 -0.38988 0.22902 -0.21617 -0.22562 -0.093918 -0.80375 | |||
to 0.68047 -0.039263 0.30186 -0.17792 0.42962 0.032246 -0.41376 0.13228 -0.29847 -0.085253 0.17118 0.22419 -0.10046 -0.43653 0.33418 0.67846 0.057204 -0.34448 -0.42785 -0.43275 0.55963 0.10032 0.18677 -0.26854 0.037334 -2.0932 0.22171 -0.39868 0.20912 -0.55725 3.8826 0.47466 -0.95658 -0.37788 0.20869 -0.32752 0.12751 0.088359 0.16351 -0.21634 -0.094375 0.018324 0.21048 -0.03088 -0.19722 0.082279 -0.09434 -0.073297 -0.064699 -0.26044 | |||
and 0.26818 0.14346 -0.27877 0.016257 0.11384 0.69923 -0.51332 -0.47368 -0.33075 -0.13834 0.2702 0.30938 -0.45012 -0.4127 -0.09932 0.038085 0.029749 0.10076 -0.25058 -0.51818 0.34558 0.44922 0.48791 -0.080866 -0.10121 -1.3777 -0.10866 -0.23201 0.012839 -0.46508 3.8463 0.31362 0.13643 -0.52244 0.3302 0.33707 -0.35601 0.32431 0.12041 0.3512 -0.069043 0.36885 0.25168 -0.24517 0.25381 0.1367 -0.31178 -0.6321 -0.25028 -0.38097 | |||
in 0.33042 0.24995 -0.60874 0.10923 0.036372 0.151 -0.55083 -0.074239 -0.092307 -0.32821 0.09598 -0.82269 -0.36717 -0.67009 0.42909 0.016496 -0.23573 0.12864 -1.0953 0.43334 0.57067 -0.1036 0.20422 0.078308 -0.42795 -1.7984 -0.27865 0.11954 -0.12689 0.031744 3.8631 -0.17786 -0.082434 -0.62698 0.26497 -0.057185 -0.073521 0.46103 0.30862 0.12498 -0.48609 -0.0080272 0.031184 -0.36576 -0.42699 0.42164 -0.11666 -0.50703 -0.027273 -0.53285 | |||
a 0.21705 0.46515 -0.46757 0.10082 1.0135 0.74845 -0.53104 -0.26256 0.16812 0.13182 -0.24909 -0.44185 -0.21739 0.51004 0.13448 -0.43141 -0.03123 0.20674 -0.78138 -0.20148 -0.097401 0.16088 -0.61836 -0.18504 -0.12461 -2.2526 -0.22321 0.5043 0.32257 0.15313 3.9636 -0.71365 -0.67012 0.28388 0.21738 0.14433 0.25926 0.23434 0.4274 -0.44451 0.13813 0.36973 -0.64289 0.024142 -0.039315 -0.26037 0.12017 -0.043782 0.41013 0.1796 | |||
" 0.25769 0.45629 -0.76974 -0.37679 0.59272 -0.063527 0.20545 -0.57385 -0.29009 -0.13662 0.32728 1.4719 -0.73681 -0.12036 0.71354 -0.46098 0.65248 0.48887 -0.51558 0.039951 -0.34307 -0.014087 0.86488 0.3546 0.7999 -1.4995 -1.8153 0.41128 0.23921 -0.43139 3.6623 -0.79834 -0.54538 0.16943 -0.82017 -0.3461 0.69495 -1.2256 -0.17992 -0.057474 0.030498 -0.39543 -0.38515 -1.0002 0.087599 -0.31009 -0.34677 -0.31438 0.75004 0.97065 | |||
's 0.23727 0.40478 -0.20547 0.58805 0.65533 0.32867 -0.81964 -0.23236 0.27428 0.24265 0.054992 0.16296 -1.2555 -0.086437 0.44536 0.096561 -0.16519 0.058378 -0.38598 0.086977 0.0033869 0.55095 -0.77697 -0.62096 0.092948 -2.5685 -0.67739 0.10151 -0.48643 -0.057805 3.1859 -0.017554 -0.16138 0.055486 -0.25885 -0.33938 -0.19928 0.26049 0.10478 -0.55934 -0.12342 0.65961 -0.51802 -0.82995 -0.082739 0.28155 -0.423 -0.27378 -0.007901 -0.030231 | |||
a 0.21705 0.46515 -0.46757 0.10082 1.0135 0.74845 -0.53104 -0.26256 0.16812 0.13182 -0.24909 -0.44185 -0.21739 0.51004 0.13448 -0.43141 -0.03123 0.20674 -0.78138 -0.20148 -0.097401 0.16088 -0.61836 -0.18504 -0.12461 -2.2526 -0.22321 0.5043 0.32257 0.15313 3.9636 -0.71365 -0.67012 0.28388 0.21738 0.14433 0.25926 0.23434 0.4274 -0.44451 0.13813 0.36973 -0.64289 0.024142 -0.039315 -0.26037 0.12017 -0.043782 0.41013 0.1796 |
@@ -0,0 +1,38 @@ | |||
A series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story . 1 | |||
This quiet , introspective and entertaining independent is worth seeking . 4 | |||
Even fans of Ismail Merchant 's work , I suspect , would have a hard time sitting through this one . 1 | |||
A positively thrilling combination of ethnography and all the intrigue , betrayal , deceit and murder of a Shakespearean tragedy or a juicy soap opera . 3 | |||
Aggressive self-glorification and a manipulative whitewash . 1 | |||
A comedy-drama of nearly epic proportions rooted in a sincere performance by the title character undergoing midlife crisis . 4 | |||
Narratively , Trouble Every Day is a plodding mess . 1 | |||
The Importance of Being Earnest , so thick with wit it plays like a reading from Bartlett 's Familiar Quotations 3 | |||
But it does n't leave you with much . 1 | |||
You could hate it for the same reason . 1 | |||
There 's little to recommend Snow Dogs , unless one considers cliched dialogue and perverse escapism a source of high hilarity . 1 | |||
Kung Pow is Oedekerk 's realization of his childhood dream to be in a martial-arts flick , and proves that sometimes the dreams of youth should remain just that . 1 | |||
The performances are an absolute joy . 4 | |||
Fresnadillo has something serious to say about the ways in which extravagant chance can distort our perspective and throw us off the path of good sense . 3 | |||
I still like Moonlight Mile , better judgment be damned . 3 | |||
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 | |||
a bilingual charmer , just like the woman who inspired it 3 | |||
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 | |||
As inept as big-screen remakes of The Avengers and The Wild Wild West . 1 | |||
It 's everything you 'd expect -- but nothing more . 2 | |||
Best indie of the year , so far . 4 | |||
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 | |||
It 's like going to a house party and watching the host defend himself against a frothing ex-girlfriend . 1 | |||
That the Chuck Norris `` grenade gag '' occurs about 7 times during Windtalkers is a good indication of how serious-minded the film is . 2 | |||
The plot is romantic comedy boilerplate from start to finish . 2 | |||
It arrives with an impeccable pedigree , mongrel pep , and almost indecipherable plot complications . 2 | |||
A film that clearly means to preach exclusively to the converted . 2 | |||
While The Importance of Being Earnest offers opportunities for occasional smiles and chuckles , it does n't give us a reason to be in the theater beyond Wilde 's wit and the actors ' performances . 1 | |||
The latest vapid actor 's exercise to appropriate the structure of Arthur Schnitzler 's Reigen . 1 | |||
More vaudeville show than well-constructed narrative , but on those terms it 's inoffensive and actually rather sweet . 2 | |||
Nothing more than a run-of-the-mill action flick . 2 | |||
Hampered -- no , paralyzed -- by a self-indulgent script ... that aims for poetry and ends up sounding like satire . 0 | |||
Ice Age is the first computer-generated feature cartoon to feel like other movies , and that makes for some glacial pacing early on . 2 | |||
There 's very little sense to what 's going on here , but the makers serve up the cliches with considerable dash . 2 | |||
Cattaneo should have followed the runaway success of his first film , The Full Monty , with something different . 2 | |||
They 're the unnamed , easily substitutable forces that serve as whatever terror the heroes of horror movies try to avoid . 1 | |||
It almost feels as if the movie is more interested in entertaining itself than in amusing us . 1 | |||
The movie 's progression into rambling incoherence gives new meaning to the phrase ` fatal script error . ' 0 |
@@ -1,12 +1,12 @@ | |||
import unittest | |||
from fastNLP.core.vocabulary import Vocabulary | |||
from fastNLP.io.embed_loader import EmbedLoader | |||
class TestEmbedLoader(unittest.TestCase): | |||
def test_case(self): | |||
vocab = Vocabulary() | |||
vocab.update(["the", "in", "I", "to", "of", "hahaha"]) | |||
embedding = EmbedLoader().fast_load_embedding(50, "../data_for_tests/glove.6B.50d_test.txt", vocab) | |||
self.assertEqual(tuple(embedding.shape), (len(vocab), 50)) | |||
# TODO: np.cov在linux上segment fault,原因未知 | |||
# embedding = EmbedLoader().fast_load_embedding(50, "../data_for_tests/glove.6B.50d_test.txt", vocab) | |||
# self.assertEqual(tuple(embedding.shape), (len(vocab), 50)) |
@@ -12,7 +12,8 @@ from fastNLP.models import CNNText | |||
class TestTutorial(unittest.TestCase): | |||
def test_tutorial(self): | |||
# 从csv读取数据到DataSet | |||
dataset = DataSet.read_csv("./data_for_tests/tutorial_sample_dataset.csv", headers=('raw_sentence', 'label'), | |||
sample_path = "test/data_for_tests/tutorial_sample_dataset.csv" | |||
dataset = DataSet.read_csv(sample_path, headers=('raw_sentence', 'label'), | |||
sep='\t') | |||
print(len(dataset)) | |||
print(dataset[0]) | |||
@@ -88,7 +89,6 @@ class TestTutorial(unittest.TestCase): | |||
print('Train finished!') | |||
# 使用fastNLP的Tester测试脚本 | |||
tester = Tester(data=test_data, model=model, metrics=AccuracyMetric(pred="predict", target="label_seq"), | |||
batch_size=4) | |||
acc = tester.test() | |||