def train(self):
    print('Training model ...')
    self.maxlen_userUtter = self.train_data.maxlen_userUtter
    self.word_vocab_size = self.train_data.word_vocab_size
    self.userIntent_vocab_size = self.train_data.userIntent_vocab_size
    self.userTag_vocab_size = self.train_data.userTag_vocab_size
    self.id2word = self.train_data.id2word
    self.id2userTag = self.train_data.id2userTag
    self.id2userIntent = self.train_data.id2userIntent
    self.userTag2id = self.train_data.userTag2id
    other_npz = '{}/other_vars.npz'.format(self.model_folder)
    train_vars = {'id2userTag': self.id2userTag, 'id2word': self.id2word, 'id2userIntent': self.id2userIntent, 'userTag2id': self.userTag2id, 'userTag_vocab_size': self.userTag_vocab_size, 'userIntent_vocab_size': self.userIntent_vocab_size, 'word_vocab_size': self.word_vocab_size, 'maxlen_userUtter': self.maxlen_userUtter}
    np.savez_compressed(other_npz, **train_vars)
    self.params['maxlen_userUtter'] = self.maxlen_userUtter
    self.params['word_vocab_size'] = self.word_vocab_size
    self.params['userTag_vocab_size'] = self.userTag_vocab_size
    self.params['userIntent_vocab_size'] = self.userIntent_vocab_size
    print_params(self.params)
    self._build()
    self._plot_graph()
    graph_yaml = '{}/graph-arch.yaml'.format(self.model_folder)
    with open(graph_yaml, 'w') as fyaml:
        fyaml.write(self.model.to_yaml())
    X_train = self.train_data.userUtter_encodePad
    tag_train = self.train_data.userTag_1hotPad
    intent_train = self.train_data.userIntent_vecBin
    train_utter_txt = self.train_data.userUtter_txt
    train_intent_txt = self.train_data.userIntent_txt
    train_tag_txt = self.train_data.userTag_txt
    train_target_fname = '{}/train.target'.format(self.model_folder)
    writeUtterTagIntentTxt(train_utter_txt, train_tag_txt, train_intent_txt, train_target_fname)
    X_dev = self.dev_data.userUtter_encodePad
    tag_dev = self.dev_data.userTag_1hotPad
    intent_dev = self.dev_data.userIntent_vecBin
    dev_utter_txt = self.dev_data.userUtter_txt
    dev_intent_txt = self.dev_data.userIntent_txt
    dev_tag_txt = self.dev_data.userTag_txt
    dev_target_fname = '{}/dev.target'.format(self.model_folder)
    writeUtterTagIntentTxt(dev_utter_txt, dev_tag_txt, dev_intent_txt, dev_target_fname)
    mask_array_train = np.zeros_like(X_train)
    mask_array_train[(X_train != 0)] = 1
    mask_array_dev = np.zeros_like(X_dev)
    mask_array_dev[(X_dev != 0)] = 1
    for ep in xrange(self.epoch_nb):
        print('<Epoch {}>'.format(ep))
        self.model.fit(x=X_train, y={'slot_output': tag_train, 'intent_output': intent_train}, sample_weight={'slot_output': mask_array_train, 'intent_output': None}, batch_size=self.batch_size, nb_epoch=1, verbose=2)
        (tag_probs, intent_probs) = self.model.predict(X_dev)
        (precision_tag, recall_tag, fscore_tag, accuracy_frame_tag) = eval_slotTagging(tag_probs, mask_array_dev, tag_dev, self.userTag2id['tag-O'])
        print('SlotTagging: ep={}, precision={:.4f}, recall={:.4f}, fscore={:.4f}, accuracy_frame={:.4f}'.format(ep, precision_tag, recall_tag, fscore_tag, accuracy_frame_tag))
        (precision_intent, recall_intent, fscore_intent, accuracy_frame_intent, threshold) = eval_intentPredict(intent_probs, intent_dev)
        print('Intent Prediction: ep={}, precision={:.4f}, recall={:.4f}, fscore={:.4f}, accuracy_frame={:.4f}, threshold={:.4f}'.format(ep, precision_intent, recall_intent, fscore_intent, accuracy_frame_intent, threshold))
        accuracy_frame_both = getNLUframeAccuracy(tag_probs, mask_array_dev, tag_dev, intent_probs, intent_dev, threshold)
        print('NLU Frame: ep={}, accuracy={:.4f}'.format(ep, accuracy_frame_both))
        (dev_tag_pred_txt, dev_intent_pred_txt) = getNLUpred(tag_probs, mask_array_dev, self.id2userTag, intent_probs, threshold, self.id2userIntent)
        dev_results_fname = '{}/dev_results/dev_ep={}.pred'.format(self.model_folder, ep)
        writeUtterTagIntentTxt(dev_utter_txt, dev_tag_pred_txt, dev_intent_pred_txt, dev_results_fname)
        print('Write dev results: {}'.format(dev_results_fname))
        weights_fname = '{}/weights/ep={}_tagF1={:.4f}frameAcc={:.4f}_intentF1={:.4f}frameAcc={:.4f}th={:.4f}.h5'.format(self.model_folder, ep, fscore_tag, accuracy_frame_tag, fscore_intent, accuracy_frame_intent, threshold)
        print('Saving Model: {}'.format(weights_fname))
        self.model.save_weights(weights_fname, overwrite=True)
def train(self):
    print('Training model ...')
    self.maxlen_userUtter = self.train_data.maxlen_userUtter
    self.word_vocab_size = self.train_data.word_vocab_size
    self.userIntent_vocab_size = self.train_data.userIntent_vocab_size
    self.userTag_vocab_size = self.train_data.userTag_vocab_size
    self.id2word = self.train_data.id2word
    self.id2userTag = self.train_data.id2userTag
    self.id2userIntent = self.train_data.id2userIntent
    self.userTag2id = self.train_data.userTag2id
    other_npz = '{}/other_vars.npz'.format(self.model_folder)
    train_vars = {'id2userTag': self.id2userTag, 'id2word': self.id2word, 'id2userIntent': self.id2userIntent, 'userTag2id': self.userTag2id, 'userTag_vocab_size': self.userTag_vocab_size, 'userIntent_vocab_size': self.userIntent_vocab_size, 'word_vocab_size': self.word_vocab_size, 'maxlen_userUtter': self.maxlen_userUtter}
    np.savez_compressed(other_npz, **train_vars)
    self.params['maxlen_userUtter'] = self.maxlen_userUtter
    self.params['word_vocab_size'] = self.word_vocab_size
    self.params['userTag_vocab_size'] = self.userTag_vocab_size
    self.params['userIntent_vocab_size'] = self.userIntent_vocab_size
    print_params(self.params)
    self._build()
    self._plot_graph()
    graph_yaml = '{}/graph-arch.yaml'.format(self.model_folder)
    with open(graph_yaml, 'w') as fyaml:
        fyaml.write(self.model.to_yaml())
    X_train = self.train_data.userUtter_encodePad
    tag_train = self.train_data.userTag_1hotPad
    intent_train = self.train_data.userIntent_vecBin
    train_utter_txt = self.train_data.userUtter_txt
    train_intent_txt = self.train_data.userIntent_txt
    train_tag_txt = self.train_data.userTag_txt
    train_target_fname = '{}/train.target'.format(self.model_folder)
    writeUtterTagIntentTxt(train_utter_txt, train_tag_txt, train_intent_txt, train_target_fname)
    X_dev = self.dev_data.userUtter_encodePad
    tag_dev = self.dev_data.userTag_1hotPad
    intent_dev = self.dev_data.userIntent_vecBin
    dev_utter_txt = self.dev_data.userUtter_txt
    dev_intent_txt = self.dev_data.userIntent_txt
    dev_tag_txt = self.dev_data.userTag_txt
    dev_target_fname = '{}/dev.target'.format(self.model_folder)
    writeUtterTagIntentTxt(dev_utter_txt, dev_tag_txt, dev_intent_txt, dev_target_fname)
    mask_array_train = np.zeros_like(X_train)
    mask_array_train[(X_train != 0)] = 1
    mask_array_dev = np.zeros_like(X_dev)
    mask_array_dev[(X_dev != 0)] = 1
    for ep in xrange(self.epoch_nb):
        print('<Epoch {}>'.format(ep))
        self.model.fit(x=X_train, y={'slot_output': tag_train, 'intent_output': intent_train}, sample_weight={'slot_output': mask_array_train, 'intent_output': None}, batch_size=self.batch_size, nb_epoch=1, verbose=2)
        (tag_probs, intent_probs) = self.model.predict(X_dev)
        (precision_tag, recall_tag, fscore_tag, accuracy_frame_tag) = eval_slotTagging(tag_probs, mask_array_dev, tag_dev, self.userTag2id['tag-O'])
        print('SlotTagging: ep={}, precision={:.4f}, recall={:.4f}, fscore={:.4f}, accuracy_frame={:.4f}'.format(ep, precision_tag, recall_tag, fscore_tag, accuracy_frame_tag))
        (precision_intent, recall_intent, fscore_intent, accuracy_frame_intent, threshold) = eval_intentPredict(intent_probs, intent_dev)
        print('Intent Prediction: ep={}, precision={:.4f}, recall={:.4f}, fscore={:.4f}, accuracy_frame={:.4f}, threshold={:.4f}'.format(ep, precision_intent, recall_intent, fscore_intent, accuracy_frame_intent, threshold))
        accuracy_frame_both = getNLUframeAccuracy(tag_probs, mask_array_dev, tag_dev, intent_probs, intent_dev, threshold)
        print('NLU Frame: ep={}, accuracy={:.4f}'.format(ep, accuracy_frame_both))
        (dev_tag_pred_txt, dev_intent_pred_txt) = getNLUpred(tag_probs, mask_array_dev, self.id2userTag, intent_probs, threshold, self.id2userIntent)
        dev_results_fname = '{}/dev_results/dev_ep={}.pred'.format(self.model_folder, ep)
        writeUtterTagIntentTxt(dev_utter_txt, dev_tag_pred_txt, dev_intent_pred_txt, dev_results_fname)
        print('Write dev results: {}'.format(dev_results_fname))
        weights_fname = '{}/weights/ep={}_tagF1={:.4f}frameAcc={:.4f}_intentF1={:.4f}frameAcc={:.4f}th={:.4f}.h5'.format(self.model_folder, ep, fscore_tag, accuracy_frame_tag, fscore_intent, accuracy_frame_intent, threshold)
        print('Saving Model: {}'.format(weights_fname))
        self.model.save_weights(weights_fname, overwrite=True)