mirror of
https://github.com/wassname/multifit.git
synced 2026-10-30 13:30:16 +08:00
58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
import pytest
|
|
from fastai import *
|
|
from fastai.text import *
|
|
|
|
pytestmark = pytest.mark.integration
|
|
|
|
print(sys.path)
|
|
import fastai_contrib.data as contrib_data
|
|
|
|
from fastai_contrib.learner import bilm_learner, accuracy_fwd, bilm_text_classifier_learner
|
|
|
|
|
|
def read_file(fname):
|
|
texts = []
|
|
with open(fname, 'r') as f:
|
|
texts = f.readlines()
|
|
labels = [0] * len(texts)
|
|
df = pd.DataFrame({'labels':labels, 'texts':texts}, columns = ['labels', 'texts'])
|
|
return df
|
|
|
|
def prep_human_numbers():
|
|
path = untar_data(URLs.HUMAN_NUMBERS)
|
|
df_trn = read_file(path/'train.txt')
|
|
df_val = read_file(path/'valid.txt')
|
|
return path, df_trn, df_val
|
|
|
|
def manual_seed(seed=42):
|
|
torch.manual_seed(seed)
|
|
np.random.seed(seed)
|
|
if torch.cuda.is_available():
|
|
torch.cuda.manual_seed_all(seed)
|
|
torch.backends.cudnn.deterministic = True
|
|
torch.backends.cudnn.benchmark = False
|
|
|
|
@pytest.fixture(scope="module")
|
|
def learn():
|
|
path, df_trn, df_val = prep_human_numbers()
|
|
data = TextLMDataBunch.from_df(path, df_trn, df_val, tokenizer=Tokenizer(BaseTokenizer))
|
|
learn = language_model_learner(data, AWD_LSTM)
|
|
learn.fit_one_cycle(4, 5e-3)
|
|
return learn
|
|
|
|
def text_df(n_labels):
|
|
data = []
|
|
texts = ["fast ai is a cool project", "hello world"] * 20
|
|
for ind, text in enumerate(texts):
|
|
sample = {}
|
|
for label in range(n_labels): sample[label] = ind%2
|
|
sample["text"] = text
|
|
data.append(sample)
|
|
df = pd.DataFrame(data)
|
|
return df
|
|
|
|
###################### NEW CODE
|
|
|
|
def test_val_loss(learn):
|
|
assert learn.validate()[1] > 0.5
|