core.example

Main Example

This is an entrypoint file runnable via ./example.py

Run a sample setup

  • consiting of fine tuning or training each model,
  • evaluating the performance, and
  • running a sample prediction.

This is a dummy sample, use core.experiments to run real experiments.

=> It may be also interesting to take a look at this source →
=> This code heavily uses core.model.exec.Executor

View Source
#!/usr/bin/env python3

"""
	# Main Example

	> This is an *entrypoint file* runnable via ``./example.py``

	Run a sample setup
	- consiting of fine tuning or training each model, 
	- evaluating the performance, and  
	- running a sample prediction.

	**This is a dummy sample, use `core.experiments` to run *real*
	experiments.**

	=> It may be also interesting to take a look at this source →  
	=> This code heavily uses `core.model.exec.Executor`
"""

# set seed
from core.utils import Random, print_info
Random.set_seed(123)

# load corpora and models
from core.corpus import Wiktionary, TwentyNews
from core.model.transformer import IsSCDBert, IsNextSCDBert, SelectSCDBert, GivenSCDFindTextBert, GivenTextFindSCDBert
from core.model.scdmatrix import iSCDMatrix, MPSCDMatrix

# load executor
from core.model import Executor

if __name__ == "__main__":
	print_info()

	executions = [
		(iSCDMatrix, ["The bison is cool!"]),
		(IsSCDBert, ["The bison is cool!"]),
		(IsNextSCDBert, ["The bison is cool!", "The bison is an animal."]),
		(SelectSCDBert, [["The bison is cool!"] * 4, ["A car has an engine.", "The computer calculates results.", "The bison is an animal.", "Somewhere is someone."]]),
		(GivenTextFindSCDBert, ["The bison is cool!", "A car has an engine. The computer calculates results. The bison is an animal. Somewhere is someone."]),
		(GivenSCDFindTextBert, ["The bison is an animal.", "I see cars. We use the computer to do research. The bison is cool!"]),
		(MPSCDMatrix, ["The bison is cool!", ["A car has an engine.", "The computer calculates results.", "The bison is an animal.", "Somewhere is someone."]])
	]

	for model, args in executions:
		corpus, _ = TwentyNews(subgroups=['misc-forsale']).split(percentages=[0.05, 0.95])

		e = Executor(
			model,
			Wiktionary,
			corpus,
			percentage_train=0.8
		)
		print(e.exec(save_results=False))
		print(e.predict(*args))