def test_embedding_trainer_word2vec_local(self, mock_getcwd): document_schema_fields = [ 'description', 'experienceRequirements', 'qualifications', 'skills' ] with tempfile.TemporaryDirectory() as td: mock_getcwd.return_value = td model_storage = ModelStorage(FSStore(td)) job_postings_generator = JobPostingCollectionSample(num_records=30) corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields) w2v = Word2VecModel(size=10, min_count=3, iter=4, window=6, workers=3) trainer = EmbeddingTrainer(corpus_generator, w2v, model_storage) trainer.train() trainer.save_model() vocab_size = len(w2v.wv.vocab.keys()) assert w2v.model_name == trainer.model_name assert set(os.listdir(os.getcwd())) == set([trainer.model_name]) # Test Online Training job_postings_generator = JobPostingCollectionSample(num_records=50) corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields) w2v_loaded = model_storage.load_model(w2v.model_name) new_trainer = EmbeddingTrainer(corpus_generator, w2v_loaded, model_storage) new_trainer.train() new_trainer.save_model() new_vocab_size = len(w2v_loaded.wv.vocab.keys()) assert set(os.listdir(os.getcwd())) == set( [trainer.model_name, new_trainer.model_name]) assert new_trainer.metadata['embedding_trainer'][ 'model_name'] != trainer.metadata['embedding_trainer'][ 'model_name'] assert vocab_size <= new_vocab_size # Save as different name model_storage.save_model(w2v, 'other_name.model') assert set(os.listdir(os.getcwd())) == set([ trainer.model_name, new_trainer.model_name, 'other_name.model' ]) # Change the store directory new_path = os.path.join(td, 'other_directory') new_trainer.save_model(FSStore(new_path)) assert set(os.listdir(new_path)) == set([new_trainer.model_name])
def test_embedding_trainer_fasttext_local(self, mock_getcwd): document_schema_fields = [ 'description', 'experienceRequirements', 'qualifications', 'skills' ] with tempfile.TemporaryDirectory() as td: mock_getcwd.return_value = td model_storage = ModelStorage(FSStore(td)) job_postings_generator = JobPostingCollectionSample(num_records=30) corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields) fasttext = FastTextModel(size=10, min_count=3, iter=4, window=6, workers=3) trainer = EmbeddingTrainer(fasttext, model_storage=model_storage) trainer.train(corpus_generator) trainer.save_model() vocab_size = len(fasttext.wv.vocab.keys()) assert fasttext.model_name == trainer._models[0].model_name assert set(os.listdir(os.getcwd())) == set( [trainer._models[0].model_name]) # Test Online Training job_postings_generator = JobPostingCollectionSample(num_records=50) corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields) fasttext_loaded = model_storage.load_model(fasttext.model_name) new_trainer = EmbeddingTrainer(fasttext_loaded, model_storage=model_storage) new_trainer.train(corpus_generator) new_trainer.save_model() new_vocab_size = len(fasttext_loaded.wv.vocab.keys()) assert set(os.listdir(os.getcwd())) == set([ trainer._models[0].model_name, new_trainer._models[0].model_name ]) assert new_trainer.metadata['embedding_trainer'][ 'models'] != trainer.metadata['embedding_trainer']['models'] assert vocab_size <= new_vocab_size
def test_embedding_feature(self): document_schema_fields = [ 'description', 'experienceRequirements', 'qualifications', 'skills' ] job_postings_generator = JobPostingCollectionSample(num_records=30) corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields, raw=True) w2v = Word2VecModel(size=10, min_count=0, iter=4, window=6, workers=3) trainer = EmbeddingTrainer(w2v) trainer.train(corpus_generator) job_postings = RawCorpusCreator( JobPostingCollectionSample(num_records=50)) raw1, raw2 = tee(job_postings) fc = SequenceFeatureCreator(raw1, sentence_tokenizer=sentence_tokenize, word_tokenizer=word_tokenize, embedding_model=w2v, features=["EmbeddingFeature"]) fc = iter(fc) self.assertEqual( next(fc).shape[0], np.array( next(iter(word_tokenizer_gen( sentence_tokenizer_gen(raw2))))).shape[0]) self.assertEqual(next(fc)[0].shape[0], 10)
def test_combined_cls_local(self, mock_getcwd): with tempfile.TemporaryDirectory() as td: mock_getcwd.return_value = td model_storage = ModelStorage(FSStore(td)) jobpostings = JobPostingCollectionSample() corpus_generator = Word2VecGensimCorpusCreator(jobpostings, raw=True) w2v = Word2VecModel(size=10, min_count=0, alpha=0.025, min_alpha=0.025) trainer = EmbeddingTrainer(w2v, model_storage=model_storage) trainer.train(corpus_generator, lookup=True) matrix = DesignMatrix(jobpostings, self.major_group, self.pipe_x, self.pipe_y) matrix.build() X = matrix.X rf = ProxyObjectWithStorage(RandomForestClassifier(), None, None, matrix.target_variable) rf.fit(X, matrix.y) proxy_rf = ProxyObjectWithStorage(rf, None, None, matrix.target_variable) # Remove the last step in the pipe_x # the input of predict_soc should be tokenized words new_pipe_x = self.pipe_x new_pipe_x.generators.pop() new_matrix = DesignMatrix(JobPostingCollectionSample(), self.major_group, new_pipe_x) new_matrix.build() ccls = CombinedClassifier(w2v, rf) assert len(ccls.predict_soc([new_matrix.X[0]])[0]) == 2
def test_fasttext(self): document_schema_fields = [ 'description', 'experienceRequirements', 'qualifications', 'skills' ] job_postings_generator = JobPostingCollectionSample(num_records=50) corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields) fasttext = FastTextModel(size=16, min_count=3, window=6, iter=4) trainer = EmbeddingTrainer(fasttext) trainer.train(corpus_generator) v1 = fasttext.infer_vector(["media"]) v2 = fasttext.infer_vector(["media"]) assert_array_equal(v1, v2) # FastText models support vector lookups for out-of-vocabulary words by summing up character ngrams belonging to the word assert "sports" not in fasttext.wv.vocab assert fasttext["sports"].shape == (16, ) # test unseen word and none of the character ngrams of the word are present in the training data self.assertRaises(KeyError, lambda: fasttext["axe"]) # test a list that has some words not in vocab v1 = fasttext.infer_vector(["media", "sport", "axe"]) v2 = fasttext.infer_vector(["media", "sport"]) assert_array_equal(v1, v2)
def test_word2vec(self): document_schema_fields = [ 'description', 'experienceRequirements', 'qualifications', 'skills' ] job_postings_generator = JobPostingCollectionSample(num_records=50) corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields) w2v = Word2VecModel(size=16, min_count=3, iter=4, window=6, workers=3) trainer = EmbeddingTrainer(w2v) trainer.train(corpus_generator) v1 = w2v.infer_vector(["media"]) v2 = w2v.infer_vector(["media"]) assert_array_equal(v1, v2) # test unseen vocab assert w2v.infer_vector(["sports"]).shape[0] == 16 # test a list that has some words not in vocab sentence_with_unseen_word = ["sports", "news", "and", "media"] sentecne_without_unseen_word = ["news", "and", "media"] assert_array_equal(w2v.infer_vector(sentence_with_unseen_word), w2v.infer_vector(sentecne_without_unseen_word))
def test_embedding_trainer_multicore_local(self, mock_getcwd): document_schema_fields = [ 'description', 'experienceRequirements', 'qualifications', 'skills' ] with tempfile.TemporaryDirectory() as td: mock_getcwd.return_value = td model_storage = ModelStorage(FSStore(td)) job_postings_generator = JobPostingCollectionSample() corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields) trainer = EmbeddingTrainer(FastTextModel(size=10, min_count=3, iter=4, window=6, workers=3), FastTextModel(size=10, min_count=3, iter=4, window=10, workers=3), Word2VecModel(size=10, workers=3, window=6), Word2VecModel(size=10, min_count=10, window=10, workers=3), model_storage=model_storage) trainer.train(corpus_generator, n_processes=4) trainer.save_model() assert set(os.listdir(os.getcwd())) == set( [model.model_name for model in trainer._models])
def train_embedding(self): jobpostings = list(JobPostingCollectionSample()) corpus_generator = Word2VecGensimCorpusCreator(jobpostings, raw=True) w2v = Word2VecModel(size=10, min_count=0, alpha=0.025, min_alpha=0.025) trainer = EmbeddingTrainer(corpus_generator, w2v) trainer.train(True) self.embedding_model = w2v self.jobpostings = jobpostings
def test_word2vec_corpus_creator(): it = FakeJobPostingGenerator() corpus = [output for output in Word2VecGensimCorpusCreator(it)] assert len(corpus) == 1 assert corpus[ 0] == 'we are looking for a person to fill this job here are some experience and requirements here are some qualifications customer service consultant entry level'.split( )
def test_combined_cls_local(self, mock_getcwd): with tempfile.TemporaryDirectory() as td: mock_getcwd.return_value = td jobpostings = list(JobPostingCollectionSample()) corpus_generator = Word2VecGensimCorpusCreator(jobpostings, raw=True) w2v = Word2VecModel(storage=FSStore(td), size=10, min_count=0, alpha=0.025, min_alpha=0.025) trainer = EmbeddingTrainer(corpus_generator, w2v) trainer.train(True) matrix = create_training_set(jobpostings, SOCMajorGroup()) X = EmbeddingTransformer(w2v).transform(matrix.X) rf = RandomForestClassifier() rf.fit(X, matrix.y) ccls = CombinedClassifier(w2v, rf, matrix.target_variable) assert len(ccls.predict_soc([matrix.X[0]])[0]) == 2
def test_visualize_in_tensorboard(self): document_schema_fields = [ 'description', 'experienceRequirements', 'qualifications', 'skills' ] job_postings_generator = JobPostingCollectionSample(num_records=50) corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields) w2v = Word2VecModel(size=16, min_count=3, iter=4, window=6, workers=3) trainer = EmbeddingTrainer(w2v) trainer.train(corpus_generator) with tempfile.TemporaryDirectory() as td: with mock.patch('os.getcwd') as mock_getcwd: mock_getcwd.return_value = td visualize_in_tensorboard(w2v) assert len( set( os.listdir( os.path.join(os.getcwd(), w2v.model_name.split('.')[0])))) == 7
def test_embedding_trainer_multicore_s3(self): client = boto3.client('s3') client.create_bucket(Bucket='fake-open-skills', ACL='public-read-write') s3_path = f"s3://fake-open-skills/model_cache/embedding" s3_storage = S3Store(path=s3_path) model_storage = ModelStorage(s3_storage) document_schema_fields = [ 'description', 'experienceRequirements', 'qualifications', 'skills' ] job_postings_generator = JobPostingCollectionSample() corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields) trainer = EmbeddingTrainer(FastTextModel(size=10, min_count=3, iter=4, window=6, workers=3), FastTextModel(size=10, min_count=3, iter=4, window=10, workers=3), Word2VecModel(size=10, workers=3, window=6), Word2VecModel(size=10, min_count=10, window=10, workers=3), model_storage=model_storage) trainer.train(corpus_generator) trainer.save_model() s3 = s3fs.S3FileSystem() files = [f.split('/')[-1] for f in s3.ls(s3_path)] assert set(files) == set( [model.model_name for model in trainer._models])
def test_skill_feature(self): document_schema_fields = [ 'description', 'experienceRequirements', 'qualifications', 'skills' ] job_postings_generator = JobPostingCollectionSample(num_records=30) corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields, raw=True) w2v = Word2VecModel(size=10, min_count=0, iter=4, window=6, workers=3) trainer = EmbeddingTrainer(w2v) trainer.train(corpus_generator) raw = RawCorpusCreator(JobPostingCollectionSample()) raw1, raw2 = tee(raw) # default fc = SequenceFeatureCreator(raw1, embedding_model=w2v) self.assertEqual( fc.selected_features, ["StructuralFeature", "ContextualFeature", "EmbeddingFeature"]) self.assertEqual( fc.all_features, ["StructuralFeature", "ContextualFeature", "EmbeddingFeature"]) fc = iter(fc) self.assertEqual( next(fc).shape[0], np.array( next(iter(word_tokenizer_gen( sentence_tokenizer_gen(raw2))))).shape[0]) self.assertEqual(next(fc)[0].shape[0], 29) # Not Supported fc = SequenceFeatureCreator(raw1, features=["FeatureNotSupported"]) fc = iter(fc) self.assertRaises(TypeError, lambda: next(fc))
def test_tester(self): document_schema_fields = ['description','experienceRequirements', 'qualifications', 'skills'] corpus_generator = Word2VecGensimCorpusCreator(JobPostingCollectionSample(num_records=30), document_schema_fields=document_schema_fields) w2v = Word2VecModel(size=10, min_count=3, iter=4, window=6, workers=3) trainer = EmbeddingTrainer(w2v) trainer.train(corpus_generator) jp = JobPostingCollectionSample() train_gen = islice(jp, 30) test_gen = islice(jp, 30, None) train_matrix = DesignMatrix(train_gen, self.fullsoc, self.pipe_x, self.pipe_y) train_matrix.build() occ_trainer = OccupationClassifierTrainer(train_matrix, 2, grid_config=self.grid_config) occ_trainer.train(save=False) cc = CombinedClassifier(w2v, occ_trainer.best_estimators[0]) steps = self.pipe_x.generators[:-1] test_gen = (t for t in test_gen if t['onet_soc_code'] is not '') tester = OccupationClassifierTester(test_data_generator=test_gen, preprocessing=steps, classifier=cc) result = list(tester) assert len(tester) == len(result) == 18
def test_embedding_trainer_word2vec_s3(self): client = boto3.client('s3') client.create_bucket(Bucket='fake-open-skills', ACL='public-read-write') s3_path = f"s3://fake-open-skills/model_cache/embedding" s3_storage = S3Store(path=s3_path) document_schema_fields = [ 'description', 'experienceRequirements', 'qualifications', 'skills' ] job_postings_generator = JobPostingCollectionSample(num_records=30) corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields) w2v = Word2VecModel(storage=s3_storage, size=10, min_count=3, iter=4, window=6, workers=3) trainer = EmbeddingTrainer(corpus_generator, w2v) trainer.train() trainer.save_model() vocab_size = len(w2v.wv.vocab.keys()) s3 = s3fs.S3FileSystem() files = [f.split('/')[-1] for f in s3.ls(s3_path)] assert w2v.model_name == trainer.model_name assert set(files) == set([trainer.model_name]) # Test online training job_postings_generator = JobPostingCollectionSample(num_records=50) corpus_generator = Word2VecGensimCorpusCreator( job_postings_generator, document_schema_fields=document_schema_fields) w2v_loaded = Word2VecModel.load(s3_storage, w2v.model_name) new_trainer = EmbeddingTrainer(corpus_generator, w2v_loaded) new_trainer.train() new_trainer.save_model() new_vocab_size = len(w2v_loaded.wv.vocab.keys()) s3 = s3fs.S3FileSystem() files = [f.split('/')[-1] for f in s3.ls(s3_path)] assert set(files) == set([new_trainer.model_name, trainer.model_name]) assert new_trainer.metadata['embedding_trainer'][ 'model_name'] != trainer.metadata['embedding_trainer']['model_name'] assert vocab_size <= new_vocab_size # Save as different name w2v.save('other_name.model') s3 = s3fs.S3FileSystem() files = [f.split('/')[-1] for f in s3.ls(s3_path)] assert set(files) == set( [trainer.model_name, new_trainer.model_name, 'other_name.model']) # Change the store directory new_s3_path = "s3://fake-open-skills/model_cache/embedding/other_directory" new_trainer.save_model(S3Store(new_s3_path)) s3 = s3fs.S3FileSystem() files = [f.split('/')[-1] for f in s3.ls(new_s3_path)] assert set(files) == set([new_trainer.model_name])
from skills_ml.job_postings.common_schema import JobPostingGenerator from skills_ml.job_postings.corpora import Doc2VecGensimCorpusCreator, Word2VecGensimCorpusCreator from skills_ml.algorithms.embedding.train import EmbeddingTrainer def get_time_range(start='2011-01-01', freq='Q', periods=24): return list( map(lambda x: datetime_to_quarter(x), pd.date_range(start=start, freq=freq, periods=periods))) if __name__ == '__main__': time_range = get_time_range(start='2011-01-01', freq='Q', periods=1) job_postings_generator = JobPostingGenerator( s3_conn=s3_conn, quarters=time_range, s3_path='open-skills-private/job_postings_common', source="all") corpus_generator = Word2VecGensimCorpusCreator(job_postings_generator) trainer = EmbeddingTrainer( s3_conn=s3_conn, corpus_generator=corpus_generator, model_s3_path='open-skills-private/model_cache/embedding/', batch_size=4000, model_type='word2vec') # The train method takes whatever arugments gensim.models.word2vec.Word2Vec or gensim.model.doc2vec.Doc2Vec has trainer.train(size=100, iter=4, window=8, workers=cores)