コード例 #1
0
def main():
    sess = tf.Session(config=config)
    max_seq_len = 40
    epoch = 30
    batch_size = 128

    data = slu_data()
    total_intent = data.total_intent
    total_word = data.total_word
    model = slu_model(max_seq_len, total_intent)
    sess.run(model.init_model)
    # read in the glove embedding matrix
    sess.run(model.init_embedding,
             feed_dict={model.read_embedding_matrix: data.embedding_matrix})
    test_f1_scores = list()

    # Train
    for cur_epoch in range(epoch):
        pred_outputs = list()
        labels = list()
        total_loss = 0.0
        for cnt in range(50):
            # get the data
            batch_nl, batch_intent, batch_distance = data.get_train_batch(
                batch_size, "Guide")
            history_nl, history_nl_len, current_nl, current_nl_len, history_intent, ground_truth, history_distance = \
            process_batch(batch_nl, batch_intent, max_seq_len, total_intent-1, total_word-1, total_intent, batch_distance)

            _, intent_output, loss = sess.run(
                [model.train_op, model.intent_output, model.loss],
                feed_dict={
                    model.labels: ground_truth,
                    model.current_nl: current_nl,
                    model.current_nl_len: current_nl_len,
                    model.history_intent: history_intent,
                    model.hist_nl: history_nl,
                    model.hist_nl_len: history_nl_len,
                    model.dropout_keep_prob: 0.5
                })

            total_loss += loss
            pred, truth = calculate_score(intent_output, ground_truth)
            pred_outputs = np.append(pred_outputs, pred)
            labels = np.append(labels, truth)

        #print "Epoch:", cur_epoch
        #print "training f1 score:", f1_score(pred_outputs, labels, average="binary")
        #print "training loss:", total_loss

        # Test
        batch_nl, batch_intent, batch_distance = data.get_test_batch()
        history_nl, history_nl_len, current_nl, current_nl_len, history_intent, ground_truth, history_distance = \
        process_batch(batch_nl, batch_intent, max_seq_len, total_intent-1, total_word-1, total_intent, batch_distance)
        # need to avoid OOM, so test only a batch one time
        test_output = None
        for i in range(0, len(batch_nl), batch_size):
            temp_test_output = sess.run(model.intent_output,
                                        feed_dict={
                                            model.labels:
                                            ground_truth[i:i + batch_size],
                                            model.current_nl:
                                            current_nl[i:i + batch_size],
                                            model.current_nl_len:
                                            current_nl_len[i:i + batch_size],
                                            model.history_intent:
                                            history_intent[i:i + batch_size],
                                            model.hist_nl:
                                            history_nl[i:i + batch_size],
                                            model.hist_nl_len:
                                            history_nl_len[i:i + batch_size],
                                            model.dropout_keep_prob:
                                            1.0
                                        })
            if i == 0:
                test_output = temp_test_output
            else:
                test_output = np.vstack((test_output, temp_test_output))

        test_pred, test_label = calculate_score(test_output, ground_truth,
                                                "Guide")
        f1sc = f1_score(test_pred, test_label, average="binary")
        #print "testing f1 score:", f1sc
        test_f1_scores.append(f1sc)

    #print "max test f1 score:", max(test_f1_scores)
    print max(test_f1_scores)
    sess.close()
コード例 #2
0
        act_logit = one_hot(np.argmax(pred_act), "act")
        attribute_logit = binary.fit_transform([pred_attribute])
        if np.sum(attribute_logit) == 0:
            attribute_logit = one_hot(np.argmax(pred_attribute), "attribute")
        label = binary.fit_transform([label])
        ret_pred_outputs = np.append(ret_pred_outputs, np.append(act_logit, attribute_logit))
        ret_ground_truth = np.append(ret_ground_truth, label)
    return ret_pred_outputs, ret_ground_truth

if __name__ == '__main__':
    sess = tf.Session(config=config)
    max_seq_len = 60
    epoch = 30
    batch_size = 128

    data = slu_data()
    total_intent = data.total_intent
    total_word = data.total_word
    model = slu_model(max_seq_len, total_intent)
    sess.run(model.init_model)
    # read in the glove embedding matrix
    sess.run(model.init_embedding, feed_dict={model.read_embedding_matrix:data.embedding_matrix})
    test_f1_scores = list()

    # Train
    for cur_epoch in range(epoch):
        pred_outputs = list()
        labels = list()
        total_loss = 0.0
        for cnt in range(50):
            # get the data