Skip to content

Commit

Permalink
chore: remove old train logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yoptar committed Feb 8, 2018
1 parent ae3dfca commit 40d4c9e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 78 deletions.
60 changes: 1 addition & 59 deletions deeppavlov/core/commands/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,78 +26,20 @@
from deeppavlov.core.common.file import read_json
from deeppavlov.core.common.registry import model as get_model
from deeppavlov.core.common.metrics_registry import get_metrics_by_names
from deeppavlov.core.commands.infer import build_agent_from_config
from deeppavlov.core.common.params import from_params
from deeppavlov.core.data.dataset import Dataset
from deeppavlov.core.models.inferable import Inferable
from deeppavlov.core.models.trainable import Trainable
from deeppavlov.core.common import paths


# TODO pass paths to local model configs to agent config.


def train_agent_models(config_path: str):
usr_dir = paths.USR_PATH
a = build_agent_from_config(config_path)

for skill_config in a.skill_configs:
model_config = skill_config['model']
model_name = model_config['name']

if issubclass(get_model(model_name), Trainable):
reader_config = skill_config['dataset_reader']
reader = from_params(get_model(reader_config['name']), {})
data = reader.read(reader_config.get('data_path', usr_dir))

dataset_config = skill_config['dataset']
dataset_name = dataset_config['name']
dataset = from_params(get_model(dataset_name), dataset_config, data=data)

model = from_params(get_model(model_name), model_config)
model.train(dataset)
else:
print('Model {} is not an instance of Trainable, skip training.'.format(model_name),
file=sys.stderr)


def train_model_from_config(config_path: str, mode='train'):
usr_dir = paths.USR_PATH
config = read_json(config_path)

reader_config = config['dataset_reader']
# NOTE: Why there are no params for dataset reader? Because doesn't have __init__()
reader = from_params(get_model(reader_config['name']), {})
data = reader.read(reader_config.get('data_path', usr_dir))

dataset_config = config['dataset']
dataset_name = dataset_config['name']
dataset = from_params(get_model(dataset_name), dataset_config, data=data)

vocabs = {}
if 'vocabs' in config:
for vocab_param_name, vocab_config in config['vocabs'].items():
vocab_name = vocab_config['name']
v = from_params(get_model(vocab_name), vocab_config, mode=mode)
v.train(dataset.iter_all('train'))
vocabs[vocab_param_name] = v

model_config = config['model']
model_name = model_config['name']
model = from_params(get_model(model_name), model_config, vocabs=vocabs, mode=mode)

model.train(dataset)

# The result is a saved to user_dir trained model.


def _fit(model: Trainable, dataset: Dataset, train_config={}):
model.fit(dataset.iter_all('train'))
model.save()
return model


def train_experimental(config_path: str):
def train_model_from_config(config_path: str):
usr_dir = paths.USR_PATH
config = read_json(config_path)

Expand Down
4 changes: 2 additions & 2 deletions deeppavlov/deep.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
sys.path.append(str(p))

from deeppavlov.core.commands.utils import set_usr_dir, get_usr_dir
from deeppavlov.core.commands.train import train_experimental
from deeppavlov.core.commands.train import train_model_from_config
from deeppavlov.core.commands.infer import interact_model
from telegram_utils.telegram_ui import interact_model_by_telegram

Expand All @@ -44,7 +44,7 @@ def main():

try:
if args.mode == 'train':
train_experimental(pipeline_config_path)
train_model_from_config(pipeline_config_path)
elif args.mode == 'interact':
interact_model(pipeline_config_path)
elif args.mode == 'interactbot':
Expand Down
21 changes: 4 additions & 17 deletions deeppavlov/run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,22 @@
limitations under the License.
"""

from deeppavlov.core.commands.train import train_experimental
from deeppavlov.core.commands.train import train_model_from_config
from deeppavlov.core.commands.infer import interact_model
from deeppavlov.core.commands.utils import set_usr_dir, get_usr_dir

# HCN_new
# configs/go_bot/config.json

# Speller
# configs/error_model/config_en.json
# configs/error_model/config_ru.json
# configs/error_model/config_ru_custom_vocab.json

# Intents classifier
# configs/intents/config_dstc2.json

# NER
# configs/ner/slot_config.json

PIPELINE_CONFIG_PATH = 'configs/intents/config_train.json'
# PIPELINE_CONFIG_PATH = 'configs/intents/config_train.json'
# PIPELINE_CONFIG_PATH = 'configs/ner/ner_dstc2_train.json'
# PIPELINE_CONFIG_PATH = 'configs/ner/ner_conll2003_train.json'
# PIPELINE_CONFIG_PATH = 'configs/ner/slot_config_train.json'
# PIPELINE_CONFIG_PATH = 'configs/error_model/config_en.json'
# PIPELINE_CONFIG_PATH = 'configs/error_model/config_ru.json'
# PIPELINE_CONFIG_PATH = 'configs/go_bot/config_train.json'
PIPELINE_CONFIG_PATH = 'configs/go_bot/config_train.json'
set_usr_dir(PIPELINE_CONFIG_PATH)
try:
# train_model_from_config(PIPELINE_CONFIG_PATH)
train_experimental(PIPELINE_CONFIG_PATH)
train_model_from_config(PIPELINE_CONFIG_PATH)
interact_model(PIPELINE_CONFIG_PATH)
# remove if usr_dir is empty:
finally:
Expand Down

0 comments on commit 40d4c9e

Please sign in to comment.