NMT.cpp 1.77 KB
Newer Older
1 2
/* NiuTrans.NMT - an open-source neural machine translation system.
 * Copyright (C) 2020 NiuTrans Research. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2018-07-31
19
 * $Modified by: HU Chi (huchinlp@gmail.com) 2020-06, 2020-07
20 21
 */

22 23
#include <ctime>

24 25 26
#include "NMT.h"
#include "train/Trainer.h"
#include "translate/Translator.h"
27

28
namespace nmt
29 30
{

31
int NMTMain(int argc, const char** argv)
32
{
33
    if (argc == 0)
34
        return 1;
35

36
    /* load configurations */
37
    Config config(argc, argv);
38

39
    srand(1);
40

41
    /* training */
42
    if (strcmp(config.trainFN, "") != 0) {
43 44
        
        Model model;
45
        model.InitModel(config);
46
        Trainer trainer;
47 48
        trainer.Init(config);
        trainer.Train(config.trainFN, config.validFN, config.modelFN, &model);
xiaotong committed
49
    }
50

51
    /* translating */
52
    if (strcmp(config.testFN, "") != 0 && strcmp(config.outputFN, "") != 0) {
53 54
        
        /* disable grad flow */
55
        DISABLE_GRAD;
56 57

        Model model;
58
        model.InitModel(config);
59
        Translator translator;
60 61 62 63
        translator.Init(config);
        translator.Translate(config.testFN, config.srcVocabFN, 
                             config.tgtVocabFN, config.outputFN, &model);
    }
64

65 66 67
    return 0;
}

68
}