コード例 #1
0
ファイル: train.py プロジェクト: zhangyu68/dialogue-system
    def __init__(self):
        import os
        #实体追踪
        et = EntityTracker()
        #词袋 word2vec
        self.bow_enc = BoW_encoder()
        #加载word2vec embedding
        self.emb = UtteranceEmbed()
        #将实体追踪器添加到动作追踪器中
        at = ActionTracker(et)
        #得到数据集和对话开始 结束行数
        self.dataset, dialog_indices = Data(et, at).trainset
        #划分数据集:200对做训练 50对做测试
        self.dialog_indices_tr = dialog_indices
        self.dialog_indices_dev = dialog_indices
        #obs_size 300维的词向量 + 85个袋中的词 + 4个槽位
        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features

        #话术模板
        self.action_templates = at.get_action_templates()
        #动作个数
        action_size = at.action_size
        #隐藏层神经元个数
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size,
                            action_size=action_size,
                            nb_hidden=nb_hidden)
コード例 #2
0
    def __init__(self):
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        self.at = ActionTracker(interact=True)
        print(self.at.dialog_acts)
        _, _, self.other_da, self.other_da_id = self.at.get_data()
        self.action_templates = self.at.action_templates
        self.action_size = self.at.action_size
        #self.responses = [r[0] for r in self.at.responses]
        #print(self.emb.dim , self.bow_enc.vocab_size , self.action_size)
        self.obs_size = self.emb.dim + self.bow_enc.vocab_size + self.action_size
        nb_hidden = 128
        self.exercises = list(self.at.action_templates.keys())
        self.net = LSTM_net(obs_size=self.obs_size,
                            action_size=self.action_size,
                            nb_hidden=nb_hidden)
        # restore checkpoint
        self.net.restore()

        # setup remote interaction via zeromq
        context = zmq.Context.instance()
        # PUB socket for sending system-output
        self.pubSocket: zmq.Socket = context.socket(zmq.PUB)
        self.pubSocket.bind(Config.SYSTEM_OUTPUT_ADDR)
        # SUB socket for receiving user-input
        self.subSocket: zmq.Socket = context.socket(zmq.SUB)
        self.subSocket.connect(Config.USER_INPUT_ADDR)
        self.subSocket.setsockopt_string(zmq.SUBSCRIBE,
                                         Config.USER_INPUT_SUBJECT)
        self.lastOutputMessage: str = None
        self.requestHandler: threading.Thread = None
        self.requestInterrupted: threading.Lock = None
コード例 #3
0
    def __init__(self):
    
        et = EntityTracker()
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        
        at = ActionTracker(et)
        
        self.train_dataset, train_dialog_indices = Data(et, at).train_set
        self.test_dataset, test_dialog_indices = Data(et, at).test_set
        
        print('=========================\n')
        print('length of Train dialog indices : ', len(train_dialog_indices))
        print('=========================\n')

        print('=========================\n')
        print('length of Test dialog indices : ', len(test_dialog_indices))
        print('=========================\n')
        
        # Shuffle Training Dataset
        random.shuffle(train_dialog_indices)
        
        self.dialog_indices_tr = train_dialog_indices
        self.dialog_indices_dev = test_dialog_indices

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        
        # nb_hidden = 128
        nb_hidden = 150
        
        print('=========================\n')
        print('Action_templates: ', action_size)
        print('=========================\n')

        self.net = LSTM_net(obs_size=obs_size,
                       action_size=action_size,
                       nb_hidden=nb_hidden)
        
        self.et = et
        self.at = at
        
        action_projection = []
        for action in self.action_templates:
            action_projection.append(self.emb.encode(action))
        self.action_projection = np.transpose(action_projection)
        self.action_size = action_size
コード例 #4
0
ファイル: interact.py プロジェクト: cp4011/SL_Hospital
    def __init__(self):

        et = EntityTracker()
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        at = ActionTracker(et)

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size, action_size=action_size, nb_hidden=nb_hidden)

        # restore checkpoint
        self.net.restore()
コード例 #5
0
    def __init__(self):

        et = EntityTracker()
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        at = ActionTracker(et)

        self.dataset, dialog_indices = Data(et, at).trainset
        self.dialog_indices_tr = dialog_indices[:200]
        self.dialog_indices_dev = dialog_indices[200:250]

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size,
                            action_size=action_size,
                            nb_hidden=nb_hidden)
コード例 #6
0
        def __init__(self, train_ratio=15/20, \
                                                        epochs=100, \
                                                        train_whole=True, \
                                                        ):

                self.train_ratio = train_ratio
                self.epochs = epochs
                

                #flag für fehleranalyse
                self.startCountingErrors = False
                self.count_all_one_eval = 0 
                self.count_all_one_train = 0 
                self.count_all_train = 0
                self.count_all_eval = 0
                #data.trainset = [(u,da_indx), ...]
                
                #HIER acttemps und size
                self.at = ActionTracker()
                self.action_templates = self.at.action_templates
                self.at.dialog_acts = self.at.dialog_acts
                self.at.dialog_act_ids = self.at.dialog_act_ids
                self.dataset, self.dialog_indices, self.other_da, self.other_da_id = self.at.get_data()
                self.action_size = self.at.action_size
                self.accs = []
                self.loss = []
                self.dev_accs =[]
                self.dev_loss=[]
                self.countErrors = Counter()
                self.countPredicitions = 0
                self.errorLog = {}
                self.highest_accuracy = 0
                #pickle.load(open("data/errors/errorLog.p", "rb"))
                if train_whole:
                        self.bow_enc = BoW_encoder()
                        self.emb = UtteranceEmbed()
                        #HIER numfeats
                        obs_size = self.emb.dim + self.bow_enc.vocab_size + self.action_size# + self.et.size_context_features
                        nb_hidden = 128
                        self.net = LSTM_net(obs_size,
                                                                        self.action_size,
                                                                        nb_hidden=nb_hidden)
コード例 #7
0
    def __init__(self):

        et = EntityTracker()
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        at = ActionTracker(et)
        '''
        ['any preference on a type of cuisine', 'api_call <cuisine> <location> <party_size> <rest_type>', 'great let me do the reservation', 'hello what can i help you with today', 'here it is <info_address>', 'here it is <info_phone>', 'how many people would be in your party', "i'm on it", 'is there anything i can help you with', 'ok let me look into some options for you', 'sure is there anything else to update', 'sure let me find an other option for you', 'what do you think of this option: <restaurant>', 'where should it be', 'which price range are looking for', "you're welcome"]

        '''
        self.dataset, dialog_indices = Data(et, at).trainset
        self.dialog_indices_tr = dialog_indices[:200]
        self.dialog_indices_dev = dialog_indices[200:250]

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size,
                            action_size=action_size,
                            nb_hidden=nb_hidden)
コード例 #8
0
    def __init__(self):

        et = EntityTracker()
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        at = ActionTracker(et)

        self.dataset, dialog_indices = Data(et, at).trainset
        
        train_indices = joblib.load('data/train_test_list/train_indices_759')
        test_indices = joblib.load('data/train_test_list/test_indices_759_949')
        
        self.dialog_indices_tr = train_indices
        self.dialog_indices_dev = test_indices

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size,
                       action_size=action_size,
                       nb_hidden=nb_hidden)
コード例 #9
0
ファイル: interact.py プロジェクト: cp4011/SL_Hospital
class InteractiveSession:

    def __init__(self):

        et = EntityTracker()
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        at = ActionTracker(et)

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size, action_size=action_size, nb_hidden=nb_hidden)

        # restore checkpoint
        self.net.restore()

    def interact(self):
        # create entity tracker
        et = EntityTracker()
        # create action tracker
        at = ActionTracker(et)
        # reset network
        self.net.reset_state()

        # begin interaction loop
        while True:

            # get input from user
            u = input('User: '******'clear' or u == 'reset' or u == 'restart':
                self.net.reset_state()
                et = EntityTracker()
                at = ActionTracker(et)
                print('Bot: Reset successfully')

            # check for entrance and exit command
            elif u == 'exit' or u == 'stop' or u == 'quit' or u == 'q':
                print("Bot: Thank you for using")
                break

            elif u == 'hello' or u == 'hi':
                print("Bot: Hello, what can i do for you")

            elif u == 'thank you' or u == 'thanks' or u == 'thank you very much':
                print('Bot: You are welcome')
                break

            else:
                if not u:
                    continue

                u = u.lower()

                # encode
                u_ent = et.extract_entities(u)
                u_ent_features = et.context_features()  # 5

                # print(et.entities)
                # print(et.ctxt_features)

                u_emb = self.emb.encode(u)              # 300
                u_bow = self.bow_enc.encode(u)          # 60
                # concat features
                features = np.concatenate((u_ent_features, u_emb, u_bow), axis=0)
                # print(features.shape)
                # get action mask
                action_mask = at.action_mask()
                # action_mask = np.ones(self.net.action_size)

                # print("action_mask: ", action_mask)

                # forward
                prediction = self.net.forward(features, action_mask)
                response = self.action_templates[prediction]
                if prediction == 0:
                    slot_values = copy.deepcopy(et.entities)
                    slot_values.pop('<location>')
                    memory = []
                    count = 0
                    for k, v in slot_values.items():
                        memory.append('='.join([k, v]))
                        count += 1
                        if count == 2:
                            memory.append('\n')

                    response = response.replace("memory", ', '.join(memory))

                    # memory = ', '.join(slot_values.values())
                    # response = response.replace("memory", memory)
                    self.net.reset_state()
                    et = EntityTracker()
                    at = ActionTracker(et)
                    # print('Execute successfully and begin new session')
                if prediction == 1:
                    response = response.replace("location", '<location>=' + et.entities['<location>'])
                print('Bot: ', response)
コード例 #10
0
class InteractiveSession():
    def __init__(self):

        et = EntityTracker()
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        at = ActionTracker(et)

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size,
                            action_size=action_size,
                            nb_hidden=nb_hidden)

        # restore checkpoint
        self.net.restore()

    def interact(self):
        # create entity tracker
        et = EntityTracker()
        # create action tracker
        at = ActionTracker(et)
        # reset network
        self.net.reset_state()

        # begin interaction loop
        while True:

            # get input from user
            u = input(':: ')

            # check if user wants to begin new session
            if u == 'clear' or u == 'reset' or u == 'restart':
                self.net.reset_state()
                et = EntityTracker()
                at = ActionTracker(et)
                print('')

            # check for exit command
            elif u == 'exit' or u == 'stop' or u == 'quit' or u == 'q':
                break

            else:
                # ENTER press : silence
                if not u:
                    u = '<SILENCE>'

                # encode
                u_ent, u_entities = et.extract_entities(u, is_test=True)
                u_ent_features = et.context_features()

                u_emb = self.emb.encode(u)
                u_bow = self.bow_enc.encode(u)
                # concat features
                features = np.concatenate((u_ent_features, u_emb, u_bow),
                                          axis=0)
                # get action mask
                action_mask = at.action_mask()

                # forward
                prediction = self.net.forward(features, action_mask)
                print('prediction : ', prediction)
                print(u_entities)
                print('\n')
                if self.post_process(prediction, u_ent_features):
                    print(
                        '>>', 'api_call ' + u_entities['<cuisine>'] + ' ' +
                        u_entities['<location>'] + ' ' +
                        u_entities['<party_size>'] + ' ' +
                        u_entities['<rest_type>'])

                else:
                    prediction = self.action_post_process(
                        prediction, u_entities)
                    print('>>', self.action_templates[prediction])

                    # if all entities is satisfied and the user agree to make a reservation.
                    if all(u_ent_featur == 1
                           for u_ent_featur in u_ent_features) and (prediction
                                                                    == 10):
                        break

    def post_process(self, prediction, u_ent_features):
        if prediction == 0:
            return True
        attr_list = [9, 12, 6, 1]
        if all(u_ent_featur == 1
               for u_ent_featur in u_ent_features) and prediction in attr_list:
            return True
        else:
            return False

    def action_post_process(self, prediction, u_entities):
        attr_mapping_dict = {
            9: '<cuisine>',
            12: '<location>',
            6: '<party_size>',
            1: '<rest_type>'
        }

        # find exist and non-exist entity
        exist_ent_index = [
            key for key, value in u_entities.items() if value != None
        ]
        non_exist_ent_index = [
            key for key, value in u_entities.items() if value == None
        ]

        # if predicted key is already in exist entity index then find non exist entity index
        # and leads the user to input non exist entity.

        if prediction in attr_mapping_dict:
            pred_key = attr_mapping_dict[prediction]
            if pred_key in exist_ent_index:
                for key, value in attr_mapping_dict.items():
                    if value == non_exist_ent_index[0]:
                        return key
            else:
                return prediction
        else:
            return prediction
コード例 #11
0
class InteractiveSession():
    def __init__(self):

        et = EntityTracker()
        self.bow_enc = BoW_encoder(et)
        self.emb = UtteranceEmbed()
        at = ActionTracker(et)

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size,
                            action_size=action_size,
                            nb_hidden=nb_hidden)

        # restore checkpoint
        self.net.restore()

    def interact(self, input):
        # create entity tracker
        et = EntityTracker()
        # create action tracker
        at = ActionTracker(et)
        # reset network
        self.net.reset_state()

        # begin interaction loop
        #while True:

        # get input from user
        #u = input(':: ')
        u = input
        # check if user wants to begin new session
        if u == 'clear' or u == 'reset' or u == 'restart':
            self.net.reset_state()
            et = EntityTracker()
            at = ActionTracker(et)
            print('')

        # check for exit command
        #elif u == 'exit' or u == 'stop' or u == 'quit' or u == 'q':
        #break

        else:
            # ENTER press : silence
            if not u:
                u = '<SILENCE>'

                # encode
            u_ent = et.extract_entities(u)
            u_ent_features = et.context_features()
            u_emb = self.emb.encode(u)
            u_bow = self.bow_enc.encode(u)
            # concat features
            features = np.concatenate((u_ent_features, u_emb, u_bow), axis=0)
            # get action mask
            action_mask = at.action_mask()

            # forward
            prediction = self.net.forward(features, action_mask)
            #print('>>', self.action_templates[prediction])
        return self.action_templates[prediction]
コード例 #12
0
ファイル: node_eng.py プロジェクト: ehwa009/dialogue_system
class Dialogue():
    def __init__(self):
        # stor whole dialogues
        self.story = []
        self.sp_confidecne = []
        self.file_path = os.path.join(
            rospkg.RosPack().get_path('dialogue_system'), 'log',
            'dialogue.txt')

        # count turn taking
        self.usr_count = 0
        self.sys_count = 0

        # paramaters
        self.network_type = rospy.get_param('~network_model', 'stacked_lstm')
        self.lang_type = rospy.get_param('~lang', 'eng')
        self.is_emb = rospy.get_param('~embedding', 'false')
        self.is_am = rospy.get_param('~action_mask', "true")
        self.user_num = rospy.get_param('~user_number', '0')

        # call rest of modules
        self.et = EntityTracker()
        self.at = ActionTracker(self.et)
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed(lang=self.lang_type)

        # select observation size for RNN
        if self.is_am and self.is_emb:
            obs_size = self.emb.dim + self.bow_enc.vocab_size + self.et.num_features + self.at.action_size
        elif self.is_am and not (self.is_emb):
            obs_size = self.bow_enc.vocab_size + self.et.num_features + self.at.action_size
        elif not (self.is_am) and self.is_emb:
            obs_size = self.emb.dim + self.bow_enc.vocab_size + self.et.num_features
        elif not (self.is_am) and not (self.is_emb):
            obs_size = self.bow_enc.vocab_size + self.et.num_features

        self.action_template = self.at.get_action_templates()
        self.at.do_display_template()
        # must clear entities space
        self.et.do_clear_entities()
        action_size = self.at.action_size
        nb_hidden = 128

        if self.network_type == 'gru':
            self.net = GRU(obs_size=obs_size,
                           nb_hidden=nb_hidden,
                           action_size=action_size,
                           lang=self.lang_type,
                           is_action_mask=self.is_am)
        elif self.network_type == 'reversed_lstm':
            self.net = ReversingLSTM(obs_size=obs_size,
                                     nb_hidden=nb_hidden,
                                     action_size=action_size,
                                     lang=self.lang_type,
                                     is_action_mask=self.is_am)
        elif self.network_type == 'reversed_gru':
            self.net = ReversingGRU(obs_size=obs_size,
                                    nb_hidden=nb_hidden,
                                    action_size=action_size,
                                    lang=self.lang_type,
                                    is_action_mask=self.is_am)
        elif self.network_type == 'stacked_gru':
            self.net = StackedGRU(obs_size=obs_size,
                                  nb_hidden=nb_hidden,
                                  action_size=action_size,
                                  lang=self.lang_type,
                                  is_action_mask=self.is_am)
        elif self.network_type == 'stacked_lstm':
            self.net = StackedLSTM(obs_size=obs_size,
                                   nb_hidden=nb_hidden,
                                   action_size=action_size,
                                   lang=self.lang_type,
                                   is_action_mask=self.is_am)
        elif self.network_type == 'lstm':
            self.net = LSTM(obs_size=obs_size,
                            nb_hidden=nb_hidden,
                            action_size=action_size,
                            lang=self.lang_type,
                            is_action_mask=self.is_am)
        elif self.network_type == 'bidirectional_lstm':
            self.net = BidirectionalLSTM(obs_size=obs_size,
                                         nb_hidden=nb_hidden,
                                         action_size=action_size,
                                         lang=self.lang_type,
                                         is_action_mask=self.is_am)
        elif self.network_type == 'bidirectional_gru':
            self.net = BidirectionalGRU(obs_size=obs_size,
                                        nb_hidden=nb_hidden,
                                        action_size=action_size,
                                        lang=self.lang_type,
                                        is_action_mask=self.is_am)

        # restore trained model
        self.net.restore()

        # rostopics
        self.pub_reply = rospy.Publisher('reply', Reply, queue_size=10)
        self.pub_complete = rospy.Publisher('complete_execute_scenario',
                                            Empty,
                                            queue_size=10)
        rospy.Subscriber('raising_events', RaisingEvents,
                         self.handle_raise_events)

        try:
            rospy.wait_for_service('reception_db/query_data')
            self.get_response_db = rospy.ServiceProxy(
                'reception_db/query_data', DBQuery)
            rospy.logwarn("waiting for reception DB module...")
        except rospy.exceptions.ROSInterruptException as e:
            rospy.logerr(e)
            quit()
        rospy.logwarn(
            "network: {}, lang: {}, action_mask: {}, embedding: {}, user_number: {}"
            .format(self.network_type, self.lang_type, self.is_am, self.is_emb,
                    self.user_num))
        self.story.append('user number: %s' % self.user_num)
        rospy.loginfo('\033[94m[%s]\033[0m initialized.' % rospy.get_name())

        # if utterance == 'clear':
        #     self.net.reset_state()
        #     self.et.do_clear_entities()
        #     response = 'context has been cleared.'

    def get_response(self, utterance):
        rospy.loginfo("actual input: %s" %
                      utterance)  # check actual user input

        # clean utterance
        # utterance = re.sub(r'[^ a-z A-Z 0-9]', " ", utterance)
        # utterance preprocessing
        u_ent, u_entities = self.et.extract_entities(utterance, is_test=True)
        u_ent_features = self.et.context_features()
        u_bow = self.bow_enc.encode(utterance)

        if self.is_emb:
            u_emb = self.emb.encode(utterance)
        try:
            if self.is_am:
                action_mask = self.at.action_mask()

            # concatenated features
            if self.is_am and self.is_emb:
                features = np.concatenate(
                    (u_ent_features, u_emb, u_bow, action_mask), axis=0)
            elif self.is_am and not (self.is_emb):
                features = np.concatenate((u_ent_features, u_bow, action_mask),
                                          axis=0)
            elif not (self.is_am) and self.is_emb:
                features = np.concatenate((u_ent_features, u_emb, u_bow),
                                          axis=0)
            elif not (self.is_am) and not (self.is_emb):
                features = np.concatenate((u_ent_features, u_bow), axis=0)

            # try:
            # predict template number
            if self.is_am:
                probs, prediction = self.net.forward(features, action_mask)
            else:
                probs, prediction = self.net.forward(features)

            # check response confidence
            if max(probs) > BOUNDARY_CONFIDENCE:
                response = self.action_template[prediction]
                prediction = self.pre_action_process(prediction, u_entities)

                # handle api call
                if self.post_process(prediction, u_entities):
                    if prediction == 1:
                        response = 'api_call appointment {} {} {} {} {} {} {}'.format(
                            u_entities['<first_name>'],
                            u_entities['<last_name>'],
                            u_entities['<address_number>'],
                            u_entities['<address_name>'],
                            u_entities['<address_type>'], u_entities['<time>'],
                            u_entities['<pm_am>'])
                    elif prediction == 2:
                        response = 'api_call location {}'.format(
                            u_entities['<location>'])
                    elif prediction == 3:
                        response = 'api_call prescription {} {} {} {} {}'.format(
                            u_entities['<first_name>'],
                            u_entities['<last_name>'],
                            u_entities['<address_number>'],
                            u_entities['<address_name>'],
                            u_entities['<address_type>'])
                    elif prediction == 4:
                        response = 'api_call waiting_time {} {} {} {} {} {} {}'.format(
                            u_entities['<first_name>'],
                            u_entities['<last_name>'],
                            u_entities['<address_number>'],
                            u_entities['<address_name>'],
                            u_entities['<address_type>'], u_entities['<time>'],
                            u_entities['<pm_am>'])
                    response = self.get_response_db(
                        response
                    )  # query knowledge base; here we use dynamo db
                    response = response.response
                elif prediction in [6, 9, 11]:
                    response = self.action_template[prediction]
                    response = response.split(' ')
                    response = [
                        word.replace('<first_name>',
                                     u_entities['<first_name>'])
                        for word in response
                    ]
                    response = ' '.join(response)
                else:
                    response = self.action_template[prediction]

            else:
                response = random.choice(
                    REPROMPT
                )  # if prediction confidence less than 40%, reprompt
        except:
            response = random.choice(REPROMPT)

        return prediction, probs, response

    def handle_raise_events(self, msg):
        utterance = msg.recognized_word
        try:
            # get confidence
            data = json.loads(msg.data[0])
            confidence = data['confidence']
        except:
            confidence = None

        if confidence > BOUNDARY_CONFIDENCE or confidence == None:
            if 'silency_detected' in msg.events:
                utterance = '<SILENCE>'
            else:
                try:
                    self.story.append(
                        "U%i: %s (sp_conf:%f)" %
                        (self.usr_count + 1, utterance, confidence))
                    self.sp_confidecne.append(confidence)
                except:
                    self.story.append("U%i: %s" %
                                      (self.usr_count + 1, utterance))
                self.usr_count += 1
                utterance = utterance.lower()
            # generate system response
            prediction, probs, response = self.get_response(utterance)
        else:
            prediction = -1
            probs = -1
            response = random.choice(REPROMPT)

        # add system turn
        self.story.append("A%i: %s" % (self.sys_count + 1, response))
        self.sys_count += 1

        # finish interaction
        if (prediction == 6):
            self.pub_complete.publish()
            # logging user and system turn
            self.story.append("user: %i, system: %i" %
                              (self.usr_count, self.sys_count))
            self.story.append("mean_sp_conf: %f" %
                              (reduce(lambda x, y: x + y, self.sp_confidecne) /
                               len(self.sp_confidecne)))
            self.story.append(
                '==================================================================='
            )
            self.write_file(self.file_path, self.story)

        # display system response
        rospy.loginfo(json.dumps(self.et.entities,
                                 indent=2))  # recognized entity values
        try:
            rospy.logwarn("System: [conf: %f, predict: %d] / %s\n" %
                          (max(probs), prediction, response))
        except:
            rospy.logwarn("System: [] / %s\n" % (response))

        reply_msg = Reply()
        reply_msg.header.stamp = rospy.Time.now()
        reply_msg.reply = response

        self.pub_reply.publish(reply_msg)

    def post_process(self, prediction, u_ent_features):
        api_call_list = [1, 2, 3, 4]
        if prediction in api_call_list:
            return True
        attr_list = [0, 9, 10, 11, 12]
        if all(u_ent_featur == 1
               for u_ent_featur in u_ent_features) and prediction in attr_list:
            return True
        else:
            return False

    def action_post_process(self, prediction, u_entities):

        attr_mapping_dict = {
            11: '<first_name>',
            11: '<last_name>',
            12: '<address_number>',
            12: '<address_name>',
            12: '<address_type>',
            10: '<time>',
            10: '<pm_am>',
        }

        # find exist and non-exist entity
        exist_ent_index = [
            key for key, value in u_entities.items() if value != None
        ]
        non_exist_ent_index = [
            key for key, value in u_entities.items() if value == None
        ]

        # if predicted key is already in exist entity index then find non exist entity index
        # and leads the user to input non exist entity.

        if prediction in attr_mapping_dict:
            pred_key = attr_mapping_dict[prediction]
            if pred_key in exist_ent_index:
                for key, value in attr_mapping_dict.items():
                    if value == non_exist_ent_index[0]:
                        return key
            else:
                return prediction
        else:
            return prediction

    def pre_action_process(self, prediction, u_entities):

        api_call_list = [1, 3, 4]

        attr_mapping_dict = {
            '<first_name>': 11,
            '<last_name>': 11,
            '<address_number>': 12,
            '<address_name>': 12,
            '<address_type>': 12,
            '<time>': 10,
            '<pm_am>': 10,
        }

        # find exist and non-exist entity
        non_exist_ent_index = [
            key for key, value in u_entities.items() if value == None
        ]

        if prediction in api_call_list:
            if '<first_name>' in non_exist_ent_index:
                prediction = attr_mapping_dict['<first_name>']

        return prediction

    ''' 
    writing story log file
    '''

    def write_file(self, path, story_list):
        with open(path, 'a') as f:
            for item in story_list:
                f.write("%s\n" % item)
        rospy.logwarn('save dialogue histories.')
コード例 #13
0
ファイル: train.py プロジェクト: zhangyu68/dialogue-system
class Trainer():
    def __init__(self):
        import os
        #实体追踪
        et = EntityTracker()
        #词袋 word2vec
        self.bow_enc = BoW_encoder()
        #加载word2vec embedding
        self.emb = UtteranceEmbed()
        #将实体追踪器添加到动作追踪器中
        at = ActionTracker(et)
        #得到数据集和对话开始 结束行数
        self.dataset, dialog_indices = Data(et, at).trainset
        #划分数据集:200对做训练 50对做测试
        self.dialog_indices_tr = dialog_indices
        self.dialog_indices_dev = dialog_indices
        #obs_size 300维的词向量 + 85个袋中的词 + 4个槽位
        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features

        #话术模板
        self.action_templates = at.get_action_templates()
        #动作个数
        action_size = at.action_size
        #隐藏层神经元个数
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size,
                            action_size=action_size,
                            nb_hidden=nb_hidden)

    def train(self):

        print('\n:: training started\n')
        epochs = 25
        for j in range(epochs):
            # iterate through dialogs
            #训练集个数
            num_tr_examples = len(self.dialog_indices_tr)
            loss = 0.
            for i, dialog_idx in enumerate(self.dialog_indices_tr):
                # get start and end index
                start, end = dialog_idx['start'], dialog_idx['end']
                # train on dialogue
                loss += self.dialog_train(self.dataset[start:end])
                # print #iteration
                sys.stdout.write('\r{}.[{}/{}]'.format(j + 1, i + 1,
                                                       num_tr_examples))

            print('\n\n:: {}.tr loss {}'.format(j + 1, loss / num_tr_examples))
            # evaluate every epoch
            accuracy = self.evaluate()
            print(':: {}.dev accuracy {}\n'.format(j + 1, accuracy))

            if accuracy > 0.4:
                self.net.save()
                continue

    #训练过程
    def dialog_train(self, dialog):
        # create entity tracker
        et = EntityTracker()
        # create action tracker
        at = ActionTracker(et)
        # reset network
        self.net.reset_state()

        loss = 0.
        # iterate through dialog
        #u 用户输入 r 对应的动作索引
        for (u, r) in dialog:
            #u_ent 分词后的字符串
            u_ent = et.extract_entities(u)
            #槽位填充情况 【0 0 0 0】
            u_ent_features = et.context_features()
            #word2vec
            u_emb = self.emb.encode(u)
            #multi-hot
            u_bow = self.bow_enc.encode(u)
            # concat features
            #300 + 85 + 4 = 389
            features = np.concatenate((u_ent_features, u_emb, u_bow), axis=0)
            # get action mask
            action_mask = at.action_mask()
            # forward propagation
            #  train step
            loss += self.net.train_step(features, r, action_mask)
        return loss / len(dialog)

    #评估acc
    def evaluate(self):
        # create entity tracker
        et = EntityTracker()
        # create action tracker
        at = ActionTracker(et)
        # reset network
        self.net.reset_state()

        dialog_accuracy = 0.
        #加载测试集
        for dialog_idx in self.dialog_indices_dev:

            start, end = dialog_idx['start'], dialog_idx['end']
            dialog = self.dataset[start:end]
            num_dev_examples = len(self.dialog_indices_dev)

            # create entity tracker
            et = EntityTracker()
            # create action tracker
            at = ActionTracker(et)
            # reset network
            self.net.reset_state()

            # iterate through dialog
            correct_examples = 0
            #对于每个dialog 提取出utterance 和 response
            for (u, r) in dialog:
                # encode utterance
                #提取出user中带有的实体
                u_ent = et.extract_entities(u)
                #提取当前槽位填充情况
                u_ent_features = et.context_features()
                u_emb = self.emb.encode(u)
                u_bow = self.bow_enc.encode(u)
                # concat features
                features = np.concatenate((u_ent_features, u_emb, u_bow),
                                          axis=0)
                # get action mask 16维的multi-hot 向量
                action_mask = at.action_mask()
                # forward propagation
                #  train step
                prediction = self.net.forward(features, action_mask)
                correct_examples += int(prediction == r)
            # get dialog accuracy
            dialog_accuracy += correct_examples / len(dialog)

        return dialog_accuracy / num_dev_examples
コード例 #14
0
ファイル: node_eng.py プロジェクト: ehwa009/dialogue_system
    def __init__(self):
        # stor whole dialogues
        self.story = []
        self.sp_confidecne = []
        self.file_path = os.path.join(
            rospkg.RosPack().get_path('dialogue_system'), 'log',
            'dialogue.txt')

        # count turn taking
        self.usr_count = 0
        self.sys_count = 0

        # paramaters
        self.network_type = rospy.get_param('~network_model', 'stacked_lstm')
        self.lang_type = rospy.get_param('~lang', 'eng')
        self.is_emb = rospy.get_param('~embedding', 'false')
        self.is_am = rospy.get_param('~action_mask', "true")
        self.user_num = rospy.get_param('~user_number', '0')

        # call rest of modules
        self.et = EntityTracker()
        self.at = ActionTracker(self.et)
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed(lang=self.lang_type)

        # select observation size for RNN
        if self.is_am and self.is_emb:
            obs_size = self.emb.dim + self.bow_enc.vocab_size + self.et.num_features + self.at.action_size
        elif self.is_am and not (self.is_emb):
            obs_size = self.bow_enc.vocab_size + self.et.num_features + self.at.action_size
        elif not (self.is_am) and self.is_emb:
            obs_size = self.emb.dim + self.bow_enc.vocab_size + self.et.num_features
        elif not (self.is_am) and not (self.is_emb):
            obs_size = self.bow_enc.vocab_size + self.et.num_features

        self.action_template = self.at.get_action_templates()
        self.at.do_display_template()
        # must clear entities space
        self.et.do_clear_entities()
        action_size = self.at.action_size
        nb_hidden = 128

        if self.network_type == 'gru':
            self.net = GRU(obs_size=obs_size,
                           nb_hidden=nb_hidden,
                           action_size=action_size,
                           lang=self.lang_type,
                           is_action_mask=self.is_am)
        elif self.network_type == 'reversed_lstm':
            self.net = ReversingLSTM(obs_size=obs_size,
                                     nb_hidden=nb_hidden,
                                     action_size=action_size,
                                     lang=self.lang_type,
                                     is_action_mask=self.is_am)
        elif self.network_type == 'reversed_gru':
            self.net = ReversingGRU(obs_size=obs_size,
                                    nb_hidden=nb_hidden,
                                    action_size=action_size,
                                    lang=self.lang_type,
                                    is_action_mask=self.is_am)
        elif self.network_type == 'stacked_gru':
            self.net = StackedGRU(obs_size=obs_size,
                                  nb_hidden=nb_hidden,
                                  action_size=action_size,
                                  lang=self.lang_type,
                                  is_action_mask=self.is_am)
        elif self.network_type == 'stacked_lstm':
            self.net = StackedLSTM(obs_size=obs_size,
                                   nb_hidden=nb_hidden,
                                   action_size=action_size,
                                   lang=self.lang_type,
                                   is_action_mask=self.is_am)
        elif self.network_type == 'lstm':
            self.net = LSTM(obs_size=obs_size,
                            nb_hidden=nb_hidden,
                            action_size=action_size,
                            lang=self.lang_type,
                            is_action_mask=self.is_am)
        elif self.network_type == 'bidirectional_lstm':
            self.net = BidirectionalLSTM(obs_size=obs_size,
                                         nb_hidden=nb_hidden,
                                         action_size=action_size,
                                         lang=self.lang_type,
                                         is_action_mask=self.is_am)
        elif self.network_type == 'bidirectional_gru':
            self.net = BidirectionalGRU(obs_size=obs_size,
                                        nb_hidden=nb_hidden,
                                        action_size=action_size,
                                        lang=self.lang_type,
                                        is_action_mask=self.is_am)

        # restore trained model
        self.net.restore()

        # rostopics
        self.pub_reply = rospy.Publisher('reply', Reply, queue_size=10)
        self.pub_complete = rospy.Publisher('complete_execute_scenario',
                                            Empty,
                                            queue_size=10)
        rospy.Subscriber('raising_events', RaisingEvents,
                         self.handle_raise_events)

        try:
            rospy.wait_for_service('reception_db/query_data')
            self.get_response_db = rospy.ServiceProxy(
                'reception_db/query_data', DBQuery)
            rospy.logwarn("waiting for reception DB module...")
        except rospy.exceptions.ROSInterruptException as e:
            rospy.logerr(e)
            quit()
        rospy.logwarn(
            "network: {}, lang: {}, action_mask: {}, embedding: {}, user_number: {}"
            .format(self.network_type, self.lang_type, self.is_am, self.is_emb,
                    self.user_num))
        self.story.append('user number: %s' % self.user_num)
        rospy.loginfo('\033[94m[%s]\033[0m initialized.' % rospy.get_name())
コード例 #15
0
class Trainer():
        def __init__(self, train_ratio=15/20, \
                                                        epochs=100, \
                                                        train_whole=True, \
                                                        ):

                self.train_ratio = train_ratio
                self.epochs = epochs
                

                #flag für fehleranalyse
                self.startCountingErrors = False
                self.count_all_one_eval = 0 
                self.count_all_one_train = 0 
                self.count_all_train = 0
                self.count_all_eval = 0
                #data.trainset = [(u,da_indx), ...]
                
                #HIER acttemps und size
                self.at = ActionTracker()
                self.action_templates = self.at.action_templates
                self.at.dialog_acts = self.at.dialog_acts
                self.at.dialog_act_ids = self.at.dialog_act_ids
                self.dataset, self.dialog_indices, self.other_da, self.other_da_id = self.at.get_data()
                self.action_size = self.at.action_size
                self.accs = []
                self.loss = []
                self.dev_accs =[]
                self.dev_loss=[]
                self.countErrors = Counter()
                self.countPredicitions = 0
                self.errorLog = {}
                self.highest_accuracy = 0
                #pickle.load(open("data/errors/errorLog.p", "rb"))
                if train_whole:
                        self.bow_enc = BoW_encoder()
                        self.emb = UtteranceEmbed()
                        #HIER numfeats
                        obs_size = self.emb.dim + self.bow_enc.vocab_size + self.action_size# + self.et.size_context_features
                        nb_hidden = 128
                        self.net = LSTM_net(obs_size,
                                                                        self.action_size,
                                                                        nb_hidden=nb_hidden)

        def train(self):
                print(self.other_da)
                print('\n:: training started', \
                                '\ntrain len :\t', round(len(self.dialog_indices) * self.train_ratio), \
                                '\ndev len :\t', len(self.dialog_indices) - round(len(self.dialog_indices) * self.train_ratio))
                print(self.epochs)
                random.shuffle(self.dialog_indices)
                self.dialog_indices_tr = self.dialog_indices[:round(len(self.dialog_indices) * self.train_ratio)]
                self.dialog_indices_dev = self.dialog_indices[round(len(self.dialog_indices) * self.train_ratio):]
                num_tr_examples = len(self.dialog_indices_tr)
                for j in range(self.epochs):

                       
                        #if j < 250:
                        #       self.startCountingErrors == True
                        # iterate through dialogs
                        
                        loss = 0.
                        random.shuffle(self.dialog_indices_tr)
                        for i, dialog_idx in enumerate(self.dialog_indices_tr):
                                # get start and end index
                                start, end = dialog_idx['start'], dialog_idx['end']
                                # train on dialogue
                                loss += self.dialog_train(self.dataset[start:end])
                                # print #iteration
                                sys.stdout.write('\r{}.[{}/{}]'.format(j+1, i+1, num_tr_examples))
                        print('\n\n:: {}.tr loss {}'.format(j+1, loss/num_tr_examples))
                        # evaluate every epoch
                        accuracy = self.evaluate()
                        self.accs.append(accuracy)
                        print(':: {}.dev accuracy {}\n'.format(j+1, accuracy))
                        #if accuracy > 0.98: # original threshold was 0.99
                        #       self.net.save() 
                        #       plt.plot(self.accs)
                        #       plt.xlabel("epochs")
                        #       plt.ylabel('accuracy')
                        #       plt.show()
                        #       break
                        if j == self.epochs-1:
                                #print("ACC: {}".format(float(sum(self.accs[349:]))/float(len(self.accs[249:]))))
                                #self.countErrors["ACCURACY"]= float(sum(self.accs[349:]))/float(len(self.accs[249:]))
                                #self.errorLog[dt.datetime.now()] = self.countErrors
                                #if accuracy > 0.95:
                                self.net.save()  # save any result after j iterations
                                #for key in self.countErrors.keys():
                        #               print("ACC: {}".format(float(sum(self.accs[349:]))/float(len(self.accs[249:]))))
                                #       self.countErrors[key] = self.countErrors[key]/ self.countPredicitions
                                #print(json.dumps(self.countErrors, indent=4))
                                #pickle.dump(self.accs, open("data/accuracies_trunc_09.p", "wb"))
                                #pickle.dump(self.loss, open("data/losses_trunc_09.p", "wb"))
                                #pickle.dump(self.at.dialog_acts, open("data/acts.p", "wb"))
                                #pickle.dump(self.at.dialog_act_ids, open("data/ids.p", "wb"))
                                #pickle.dump(self.dev_accs, open("data/dev_accuracies_00.p", "wb"))
                                #pickle.dump(self.dev_loss, open("data/dev_losses_00.p", "wb"))
                                print("Mean Accuracy: ", float(sum(self.accs[149:]))/float(len(self.accs[149:])))
                                print("Predictions LSTM Training: ", self.count_all_one_train)
                                print("LSTM ratio Training {}".format(float(self.count_all_one_train)/float(self.count_all_train)))
                                print("Predictions LSTM Evaluation: ", self.count_all_one_eval)
                                print("LSTM ratio Evaluation {}".format(float(self.count_all_one_eval)/float(self.count_all_eval)))


                                
                                plt.plot(self.accs)
                                plt.xlabel("epochs")
                                plt.ylabel('loss')
                                plt.show()
                
                                break


        def dialog_train(self, dialog):
                self.at.reset_action_mask()
                self.net.reset_state()
                try:
                        self.at.set_exercise(self.at.get_dialog_act_id(dialog[0][1],action_template=True))
                except:
                        print(dialog[0][1])
                        exit()
                self.at.automaton.reset_current_state()
                self.at.memory.reset_memory(self.at.exercise)
                loss = 0.
                self.at.initialize_am()
                self.at.automaton.reset_current_state()
                #error_code = re.compile(r"\[01\]+S")
                
                for (u,r) in dialog:
                        if r.startswith("intro"):
                                r = "intro"
                        r = self.at.get_dialog_act_id(r)
                        #print(r)
                        
                        u_emb = self.emb.encode(u)#HIER
                        #print('\nEmbedded Utterance, W2V:\n\n', u_emb)
                        u_bow = self.bow_enc.encode(u)#HIER
                        self.count_all_one_train += 1
                        
                        action_mask = self.at.am
                        # forward propagation
                        # train step
                        
                        features = np.concatenate((u_emb, u_bow, action_mask), axis=0)
                        lossValue , prediction = self.net.train_step(features, r, action_mask)
                        #current_state = self.at.automaton.current_state
                        #list_of_interest = [self.other_da[i] for i in range(len(self.other_da)) if action_mask[i] == 1]
                        #print("action mask: ",list_of_interest,"\ncurrent_state:",current_state.name,"\nprediction: ", self.other_da[prediction])
                        self.at.automaton.current_state,_ = self.at.walk(u, prediction)
                        #print(prediction)
                        #print(self.at.automaton.current_state.name)
                        
                        self.count_all_train +=1
                        #print("predicted: ", self.at.dialog_acts[prediction])
                        loss += lossValue
                        #loss += self.net.train_step(features, r)
                self.loss.append(loss/len(dialog))
                return loss/len(dialog)
        def evaluate(self):
                self.net.reset_state()
                dialog_accuracy = 0
                dialog_loss = 0
                errors = []
                
                for dialog_idx in self.dialog_indices_dev:
                        start, end = dialog_idx['start'], dialog_idx['end']
                        dialog = self.dataset[start:end]
                        num_dev_examples = len(self.dialog_indices_dev)
                        # reset network
                        self.net.reset_state()
                        try:
                                self.at.set_exercise(self.at.get_dialog_act_id(dialog[0][1],action_template=True))
                        except:
                                print(dialog)
                                exit()
                        self.at.memory.reset_memory(self.at.exercise)
                        self.at.initialize_am()
                        # iterate through dialog
                        correct_examples = 0
                        #last_action = np.zeros([self.action_size], dtype=np.float32)
                        prev_prediction = -1
                        self.at.automaton.reset_current_state()
                        prev_state = ""
                        i = 0
                        loss = 0
                        for (u,r) in dialog:
                                if r.startswith("intro"):
                                        r = "intro"
                                r = self.at.get_dialog_act_id(r)
                                i +=1
                                u_emb = self.emb.encode(u)#HIER
                                #print('\nEmbedded Utterance, W2V:\n\n', u_emb)
                                u_bow = self.bow_enc.encode(u)#HIER
                                prev_state = self.at.automaton.current_state.name
                                action_mask = self.at.am
                                features = np.concatenate((u_emb, u_bow, action_mask), axis=0)
                                prediction= self.net.forward(features,  action_mask, r)
                                #current_state = self.at.automaton.current_state
                                #list_of_interest = [self.other_da[i] for i in range(len(self.other_da)) if action_mask[i] == 1]
                                #print("action mask: ",list_of_interest,"\ncurrent_state:",current_state.name,"\nprediction: ", self.other_da[prediction])
                                self.at.automaton.current_state,_ = self.at.walk(u,prediction)
                                #self.count_all_eval +=1
                                #if prediction != r:
                                #       print("FROM: {}, with: {}, TO: {}, predicted: {}".format(prev_state, self.at.automaton.get_utterance_type(u), self.at.automaton.current_state.name, self.at.dialog_acts[prediction]))
                                #if self.startCountingErrors and prediction != r:
                                #       self.countErrors[self.at.dialog_acts[prediction] + ":" + self.at.dialog_acts[r]] +=1
                                #if self.startCountingErrors:
                                #       self.countPredicitions +=1
                                if prediction != r:
                                        errors.append("EXERCISE: {}, step {}, Previous State: {}, Previous Act: {}, Prediction: {} Actual Da: {}, Utterance: {}, Symbol: {}".format(self.at.exercise, i, prev_state,  self.at.dialog_acts[prev_prediction], self.at.dialog_acts[prediction], self.at.dialog_acts[r], u, self.at.automaton.get_utterance_type(u)))
                                prev_prediction = r
                                #print("Prediction: {} Actual Da: {}".format(self.at.dialog_acts[prediction],self.at.dialog_acts[r]))
                                #self.at.memory.update(prediction)
                                #last_action= np.zeros([self.action_size], dtype=np.float32)
                                #last_action[prediction-1] = 1

                                #prediction = self.net.forward(features)
                                
                                #print("predicted: ", self.at.dialog_acts[prediction])
                                #if prediction == r:
                                #       print(self.at.dialog_acts[r])
                                correct_examples += int(prediction == r)
                                self.count_all_eval += 1
                                #loss += loss_value[0]
                        # get dialog accuracy
                        dialog_accuracy += correct_examples/len(dialog)
                        #dialog_loss += loss / len(dialog)
                        
                if self.highest_accuracy > 0 and self.highest_accuracy -(dialog_accuracy/num_dev_examples) > 0.2:
                        for e in errors:
                                print(e)
                print(self.highest_accuracy -(dialog_accuracy/num_dev_examples))
                if self.highest_accuracy < dialog_accuracy/num_dev_examples:
                        self.highest_accuracy = dialog_accuracy/num_dev_examples
                self.dev_loss.append(dialog_loss/num_dev_examples)
                self.dev_accs.append(dialog_accuracy/num_dev_examples)
                print("dev_loss: {}".format(dialog_loss/num_dev_examples))
                return dialog_accuracy/num_dev_examples
コード例 #16
0
class Trainer():

    def __init__(self):

        et = EntityTracker()
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        at = ActionTracker(et)

        self.dataset, dialog_indices = Data(et, at).trainset
        random.shuffle(dialog_indices)
        self.dialog_indices_tr = dialog_indices[:200]
        self.dialog_indices_dev = dialog_indices[200:250]

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        self.net = LSTM_wrapper(obs_size=obs_size,
                       action_size=action_size,
                       nb_hidden=nb_hidden)


    def train(self):

        print('\n:: training started\n')
        epochs = 80
        for j in range(epochs):
            # iterate through dialogs
            num_tr_examples = len(self.dialog_indices_tr)
            loss = 0.
            for i,dialog_idx in enumerate(self.dialog_indices_tr):
                # get start and end index
                start, end = dialog_idx['start'], dialog_idx['end']
                # train on dialogue
                loss += self.dialog_train(self.dataset[start:end])
                # print #iteration
                sys.stdout.write('\r{}.[{}/{}]'.format(j+1, i+1, num_tr_examples))

            print('\n\n:: {}.tr loss {}'.format(j+1, loss/num_tr_examples))
            # evaluate every epoch
            accuracy = self.evaluate()
            print(':: {}.dev accuracy {}\n'.format(j+1, accuracy))

            # if accuracy > 0.99:
                # self.net.save()
                # break

    def dialog_train(self, dialog):
        # create entity tracker
        et = EntityTracker()
        # create action tracker
        at = ActionTracker(et)
        # reset network
        self.net.reset_state()

        loss = 0.
        # iterate through dialog
        for (u,r) in dialog:
            # print("Here in the dialog loop")
            u_ent = et.extract_entities(u)
            u_ent_features = et.context_features()
            u_emb = self.emb.encode(u)
            u_bow = self.bow_enc.encode(u)
            # concat features
            features = torch.autograd.Variable(torch.from_numpy(np.concatenate((u_ent_features, u_emb, u_bow), axis=0))).float()
            # print(features)
            # get action mask
            action_mask = torch.autograd.Variable(torch.from_numpy(at.action_mask()))
            r = torch.autograd.Variable(torch.LongTensor([r]))
            # print(r)
            # forward propagation
            #  train step
            loss += self.net.train_step(features, r, action_mask)
        return loss/len(dialog)

    def evaluate(self):
        # create entity tracker
        et = EntityTracker()
        # create action tracker
        at = ActionTracker(et)
        # reset network
        self.net.reset_state()

        dialog_accuracy = 0.
        r_count = 0 #Count of task 15
        count = 0 # Total count of rewards
        for dialog_idx in self.dialog_indices_dev:

            start, end = dialog_idx['start'], dialog_idx['end']
            dialog = self.dataset[start:end]
            num_dev_examples = len(self.dialog_indices_dev)

            # create entity tracker
            et = EntityTracker()
            # create action tracker
            at = ActionTracker(et)
            # reset network
            self.net.reset_state()

            # iterate through dialog
            correct_examples = 0
            for (u,r) in dialog:
                # encode utterance
                u_ent = et.extract_entities(u)
                u_ent_features = et.context_features()
                u_emb = self.emb.encode(u)
                u_bow = self.bow_enc.encode(u)
                # concat features
                # get action mask
                features = torch.autograd.Variable(torch.from_numpy(np.concatenate((u_ent_features, u_emb, u_bow), axis=0))).float()
                # print(features)
                # get action mask
                action_mask = torch.autograd.Variable(torch.from_numpy(at.action_mask()))
                # r = torch.autograd.Variable(torch.LongTensor([r]))
                # forward propagation
                #  train step
                logits,probs,prediction = self.net.forward(features, action_mask)
                # print("logits", logits)
                # print("probs", probs)
                # print("prediction", logits,probs,prediction)
                # print(prediction,r)
                correct_examples += int(prediction == r)
                if r==15:
                    r_count += 1
                count += 1
            # get dialog accuracy
            dialog_accuracy += correct_examples/len(dialog)

        print("task 15 was the answer with freq",r_count/count)

        return dialog_accuracy/num_dev_examples
コード例 #17
0
    def __init__(self, args):

        self.response_accuracy = []
        self.dialog_accuracy = []
        try:
            ###################### selective import #############################
            if args[0] == 'am':
                self.is_action_mask = True
            else:
                self.is_action_mask = False
            if args[1] == 'emb':
                self.is_emb = True
            else:
                self.is_emb = False

            self.network_type = args[2]
            self.lang_type = args[3]

            if self.lang_type == 'eng':
                from modules.entities import EntityTracker
                from modules.data_utils import Data
                from modules.actions import ActionTracker
                from modules.bow import BoW_encoder

            elif self.lang_type == 'kor':
                from modules.entities_kor import EntityTracker
                from modules.data_utils_kor import Data
                from modules.actions_kor import ActionTracker
                from modules.bow_kor import BoW_encoder

            ###################################################################
        except:
            rospy.logwarn(
                "please try again. i.e. ... train.py <am> <emb> <bidirectional_lstm> <eng>"
            )

        if self.is_emb:
            if self.lang_type == 'eng':
                self.emb = UtteranceEmbed(lang=self.lang_type)
            elif self.lang_type == 'kor':
                self.emb = UtteranceEmbed(lang=self.lang_type)

        et = EntityTracker()
        self.bow_enc = BoW_encoder()

        at = ActionTracker(et)

        at.do_display_template()
        self.dataset, dialog_indices = Data(et, at).trainset
        # self.dialog_indices_tr = dialog_indices[:200]
        self.dialog_indices_tr = random.sample(dialog_indices, 200)
        # self.dialog_indices_dev = dialog_indices[200:250]
        self.dialog_indices_dev = random.sample(dialog_indices, 50)

        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        # set feature input
        if self.is_action_mask and self.is_emb:
            obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features + at.action_size
        elif self.is_action_mask and not (self.is_emb):
            obs_size = self.bow_enc.vocab_size + et.num_features + at.action_size
        elif not (self.is_action_mask) and self.is_emb:
            obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        elif not (self.is_action_mask) and not (self.is_emb):
            obs_size = self.bow_enc.vocab_size + et.num_features

        # set network_type type
        if self.network_type == 'gru':
            self.net = GRU(obs_size=obs_size,
                           nb_hidden=nb_hidden,
                           action_size=action_size,
                           lang=self.lang_type,
                           is_action_mask=self.is_action_mask)
        elif self.network_type == 'reversed_lstm':
            self.net = ReversingLSTM(obs_size=obs_size,
                                     nb_hidden=nb_hidden,
                                     action_size=action_size,
                                     lang=self.lang_type,
                                     is_action_mask=self.is_action_mask)
        elif self.network_type == 'reversed_gru':
            self.net = ReversingGRU(obs_size=obs_size,
                                    nb_hidden=nb_hidden,
                                    action_size=action_size,
                                    lang=self.lang_type,
                                    is_action_mask=self.is_action_mask)
        elif self.network_type == 'stacked_gru':
            self.net = StackedGRU(obs_size=obs_size,
                                  nb_hidden=nb_hidden,
                                  action_size=action_size,
                                  lang=self.lang_type,
                                  is_action_mask=self.is_action_mask)
        elif self.network_type == 'stacked_lstm':
            self.net = StackedLSTM(obs_size=obs_size,
                                   nb_hidden=nb_hidden,
                                   action_size=action_size,
                                   lang=self.lang_type,
                                   is_action_mask=self.is_action_mask)
        elif self.network_type == 'lstm':
            self.net = LSTM(obs_size=obs_size,
                            nb_hidden=nb_hidden,
                            action_size=action_size,
                            lang=self.lang_type,
                            is_action_mask=self.is_action_mask)
        elif self.network_type == 'bidirectional_lstm':
            self.net = BidirectionalLSTM(obs_size=obs_size,
                                         nb_hidden=nb_hidden,
                                         action_size=action_size,
                                         lang=self.lang_type,
                                         is_action_mask=self.is_action_mask)
        elif self.network_type == 'bidirectional_gru':
            self.net = BidirectionalGRU(obs_size=obs_size,
                                        nb_hidden=nb_hidden,
                                        action_size=action_size,
                                        lang=self.lang_type,
                                        is_action_mask=self.is_action_mask)

        file_path = os.path.join(rospkg.RosPack().get_path('dialogue_system'),
                                 'log', self.network_type)
        # init logging
        self.logger = self.get_logger(file_path)
        msg = "'\033[94m[%s trainer]\033[0m initialized - %s (action_mask: %s, embedding: %s, lang: %s, obs_size: %s, bow: %s, context_feature: %s, action_size: %s)" % (
            rospy.get_name(), self.net.__class__.__name__, self.is_action_mask,
            self.is_emb, self.lang_type, obs_size, self.bow_enc.vocab_size,
            et.num_features, action_size)
        rospy.loginfo(msg)
コード例 #18
0
class Trainer():

    def __init__(self):
    
        et = EntityTracker()
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        
        at = ActionTracker(et)
        
        self.train_dataset, train_dialog_indices = Data(et, at).train_set
        self.test_dataset, test_dialog_indices = Data(et, at).test_set
        
        print('=========================\n')
        print('length of Train dialog indices : ', len(train_dialog_indices))
        print('=========================\n')

        print('=========================\n')
        print('length of Test dialog indices : ', len(test_dialog_indices))
        print('=========================\n')
        
        # Shuffle Training Dataset
        random.shuffle(train_dialog_indices)
        
        self.dialog_indices_tr = train_dialog_indices
        self.dialog_indices_dev = test_dialog_indices

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        
        # nb_hidden = 128
        nb_hidden = 150
        
        print('=========================\n')
        print('Action_templates: ', action_size)
        print('=========================\n')

        self.net = LSTM_net(obs_size=obs_size,
                       action_size=action_size,
                       nb_hidden=nb_hidden)
        
        self.et = et
        self.at = at
        
        action_projection = []
        for action in self.action_templates:
            action_projection.append(self.emb.encode(action))
        self.action_projection = np.transpose(action_projection)
        self.action_size = action_size

    def train(self, exp_name, model_name):

        print('\n:: training started\n')
        epochs = 100
        
        import joblib
        
        per_response_list = []
        per_dialogue_list = []
        
        early_stop = False
        early_stop_count = 0
        for j in range(epochs):
            # iterate through dialogs
            num_tr_examples = len(self.dialog_indices_tr)
            loss = 0.
            for i,dialog_idx in enumerate(self.dialog_indices_tr):
                # get start and end index
                start, end = dialog_idx['start'], dialog_idx['end']
                # train on dialogue
                loss += self.dialog_train(self.train_dataset[start:end])
                
                # print #iteration
                sys.stdout.write('\r{}.[{}/{}]'.format(j+1, i+1, num_tr_examples))
            
            print('\n\n:: {}.tr loss {}'.format(j+1, loss/num_tr_examples))
            # evaluate every epoch
            accuracy = self.evaluate()
            per_response_list.append(accuracy[0])
            per_dialogue_list.append(accuracy[1])
            max_dialogue = max(per_dialogue_list)
            
            if len(per_dialogue_list) > 1:
                prev_max_dialogue = sorted(per_dialogue_list, reverse=True)[1]
            
            if max_dialogue > 2.0 and accuracy[1] > prev_max_dialogue:
                early_stop_count += 1
                self.net.save(model_name)
           
            print(':: {}.dev accuracy {}\n'.format(j+1, accuracy))
            print('current max dialogue accuracy : {}\n'.format(sorted(per_dialogue_list, reverse=True)[0]))
        
        print('Max Dialogue Accuracy : ', max(per_dialogue_list))
        joblib.dump(per_response_list, 'emnlp_performance/with_slot/per_response_list_' + exp_name)
        joblib.dump(per_dialogue_list, 'emnlp_performance/with_slot/per_dialogue_list_' + exp_name)
        
        # self.net.save()

    def dialog_train(self, dialog):
        # create entity tracker
        et = self.et
        et.init_entities()
        # create action tracker
        at = self.at
        # reset network
        self.net.reset_state()
        self.net.reset_attention()
        
        loss = 0.
        i = 0
        pred_list = []
        # iterate through dialog
        for (u,r) in dialog:
            i += 1
            
            if r == '<UNK>':
                continue
                
            u_ent = et.extract_entities(u)
            u_ent_features = et.context_features()
            u_emb = self.emb.encode(u)
            u_bow = self.bow_enc.encode(u)
            # concat features
            features = np.concatenate((u_ent_features, u_emb, u_bow), axis=0)
            
            if i ==1:
                loss += self.net.train_step(features, r, self.action_projection)
                pred_list.append(r)
            else:
                action_one_hot = np.zeros(self.action_size)
                action_one_hot[pred_list[-1]] = 1
                loss += self.net.train_step(features, r, self.action_projection, action_one_hot)
                pred_list.append(r)
        return loss / len(dialog)

    def evaluate(self):

        dialog_accuracy = 0.
        correct_dialogue_count = 0
        
        # for each dialog
        for dialog_idx in self.dialog_indices_dev:

            start, end = dialog_idx['start'], dialog_idx['end']
            dialog = self.test_dataset[start:end]
            num_dev_examples = len(self.dialog_indices_dev)

            # create entity tracker
            et = self.et
            et.init_entities()
            # create action tracker
            at = self.at
            # reset network
            self.net.reset_state()
            self.net.reset_attention()

            # iterate through dialog
            correct_examples = 0
            
            pred_list = []
            i = 0
            for (u,r) in dialog:
                i += 1
                
                if u == 'api_call no result':
                    correct_examples += 1
                    continue
                
                if r == '<UNK>':
                    # correct_examples += 1
                    continue
                
                # encode utterance
                u_ent = et.extract_entities(u)
                u_ent_features = et.context_features()
                u_emb = self.emb.encode(u)
                u_bow = self.bow_enc.encode(u)
                # concat features
                features = np.concatenate((u_ent_features, u_emb, u_bow), axis=0)
                
                if i == 1:
                    prediction, user_attention_weights, action_weights = self.net.forward(features, self.action_projection)
                    pred_list.append(prediction)
                else:
                    action_one_hot = np.zeros(self.action_size)
                    action_one_hot[pred_list[-1]] = 1
                    prediction, user_attention_weights, action_weights = self.net.forward(features, self.action_projection, action_one_hot)
                    pred_list.append(prediction)
                    
                correct_examples += int(prediction == r)

            if correct_examples == len(dialog):
                correct_dialogue_count += 1
                
            # get dialog accuracy
            dialog_accuracy += correct_examples / len(dialog)
        
        per_response_accuracy = dialog_accuracy / num_dev_examples * 100
        per_dialogue_accuracy = correct_dialogue_count / num_dev_examples * 100
        
        print('=============================')
        print('correct dialogue count')
        print(correct_dialogue_count)
        print('=============================\n')
        
        return per_response_accuracy, per_dialogue_accuracy
コード例 #19
0
class InteractiveSession:
    def __init__(self):

        self.et = EntityTracker()
        self.at = ActionTracker(self.et)

        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()

        obs_size = self.emb.dim + self.bow_enc.vocab_size + self.et.num_features
        self.action_templates = self.at.get_action_templates()
        action_size = self.at.action_size
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size,
                            action_size=action_size,
                            nb_hidden=nb_hidden)

        # restore checkpoint
        self.net.restore()
        self.net.reset_state()

    def reset(self):
        self.net.reset_state()
        self.et = EntityTracker()
        self.at = ActionTracker(self.et)

    def interact(self, utterance, intent, slot_values):
        # get input from user
        u = utterance.lower()

        # check if user wants to begin new session
        if u == 'clear' or u == 'reset' or u == 'restart':
            self.reset()
            return "reset successfully"

        # check for entrance and exit command
        elif u == 'exit' or u == 'stop' or u == 'quit' or u == 'q':
            self.reset()
            return "Thank you for using"

        elif u == 'hello' or u == 'hi':
            self.reset()
            return "what can i do for you"

        elif u == 'thank you' or u == 'thanks' or u == 'thank you very much':
            self.reset()
            return 'you are welcome'

        else:

            # encode
            u_ent = self.et.extract_entities(u, intent, slot_values)
            u_ent_features = self.et.context_features()  # 5
            u_emb = self.emb.encode(u)  # 300
            u_bow = self.bow_enc.encode(u)  # 60
            # concat features
            features = np.concatenate((u_ent_features, u_emb, u_bow), axis=0)

            # get action mask
            action_mask = self.at.action_mask()
            # action_mask = np.ones(self.net.action_size)

            # forward
            prediction = self.net.forward(features, action_mask)
            response = self.action_templates[prediction]
            if prediction == 0:
                slot_values = copy.deepcopy(self.et.entities)
                slot_values.pop('location')
                memory = ', '.join(slot_values.values())
                response = response.replace("memory", memory)
                self.reset()
                print('API CALL execute successfully and begin new session')
            if prediction == 1:
                response = response.replace("location",
                                            self.et.entities['location'])
            return response
コード例 #20
0
class Trainer():
    def __init__(self):

        et = EntityTracker()
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        at = ActionTracker(et)
        '''
        ['any preference on a type of cuisine', 'api_call <cuisine> <location> <party_size> <rest_type>', 'great let me do the reservation', 'hello what can i help you with today', 'here it is <info_address>', 'here it is <info_phone>', 'how many people would be in your party', "i'm on it", 'is there anything i can help you with', 'ok let me look into some options for you', 'sure is there anything else to update', 'sure let me find an other option for you', 'what do you think of this option: <restaurant>', 'where should it be', 'which price range are looking for', "you're welcome"]

        '''
        self.dataset, dialog_indices = Data(et, at).trainset
        self.dialog_indices_tr = dialog_indices[:200]
        self.dialog_indices_dev = dialog_indices[200:250]

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size,
                            action_size=action_size,
                            nb_hidden=nb_hidden)

    def train(self):

        print('\n:: training started\n')
        epochs = 20
        for j in range(epochs):
            # iterate through dialogs
            num_tr_examples = len(self.dialog_indices_tr)
            loss = 0.
            for i, dialog_idx in enumerate(self.dialog_indices_tr):
                # get start and end index
                start, end = dialog_idx['start'], dialog_idx['end']
                # train on dialogue
                loss += self.dialog_train(self.dataset[start:end])
                # print #iteration
                sys.stdout.write('\r{}.[{}/{}]'.format(j + 1, i + 1,
                                                       num_tr_examples))

            print('\n\n:: {}.tr loss {}'.format(j + 1, loss / num_tr_examples))
            # evaluate every epoch
            accuracy = self.evaluate()
            print(':: {}.dev accuracy {}\n'.format(j + 1, accuracy))

            if accuracy > 0.99:
                self.net.save()
                break

    def dialog_train(self, dialog):
        # create entity tracker
        et = EntityTracker()
        # create action tracker
        at = ActionTracker(et)
        # reset network
        self.net.reset_state()

        loss = 0.
        # iterate through dialog
        for (u, r) in dialog:
            u_ent = et.extract_entities(u)
            u_ent_features = et.context_features()
            u_emb = self.emb.encode(u)
            u_bow = self.bow_enc.encode(u)
            # concat features
            features = np.concatenate((u_ent_features, u_emb, u_bow), axis=0)
            # get action mask
            action_mask = at.action_mask()
            # forward propagation
            #  train step
            loss += self.net.train_step(features, r, action_mask)
        return loss / len(dialog)

    def evaluate(self):
        # create entity tracker
        et = EntityTracker()
        # create action tracker
        at = ActionTracker(et)
        # reset network
        self.net.reset_state()

        dialog_accuracy = 0.
        for dialog_idx in self.dialog_indices_dev:

            start, end = dialog_idx['start'], dialog_idx['end']
            dialog = self.dataset[start:end]
            num_dev_examples = len(self.dialog_indices_dev)

            # create entity tracker
            et = EntityTracker()
            # create action tracker
            at = ActionTracker(et)
            # reset network
            self.net.reset_state()

            # iterate through dialog
            correct_examples = 0
            for (u, r) in dialog:
                # encode utterance
                u_ent = et.extract_entities(u)
                u_ent_features = et.context_features()
                u_emb = self.emb.encode(u)
                u_bow = self.bow_enc.encode(u)
                # concat features
                features = np.concatenate((u_ent_features, u_emb, u_bow),
                                          axis=0)
                # get action mask
                action_mask = at.action_mask()
                # forward propagation
                #  train step
                prediction = self.net.forward(features, action_mask)
                correct_examples += int(prediction == r)
            # get dialog accuracy
            dialog_accuracy += correct_examples / len(dialog)

        return dialog_accuracy / num_dev_examples
コード例 #21
0
class Trainer():

    def __init__(self):

        et = EntityTracker()
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        at = ActionTracker(et)

        self.dataset, dialog_indices = Data(et, at).trainset
        self.dialog_indices_tr = dialog_indices[:200]
        self.dialog_indices_dev = dialog_indices[200:250]

        obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        self.net = LSTM_net(obs_size=obs_size,
                            action_size=action_size,
                            nb_hidden=nb_hidden)


    def train(self):

        print('\n---training started---\n')
        epochs = 20
        for j in range(epochs):
            # iterate through dialogs
            num_tr_examples = len(self.dialog_indices_tr)
            loss = 0.
            for i,dialog_idx in enumerate(self.dialog_indices_tr):
                # get start and end index
                start, end = dialog_idx['start'], dialog_idx['end']
                # train on dialogue
                loss += self.dialog_train(self.dataset[start:end])
                # print #iteration
                sys.stdout.write('\r{}.[{}/{}]'.format(j+1, i+1, num_tr_examples))

            print('\n\n--- {}.tr loss {} ---'.format(j+1, loss/num_tr_examples))
            # evaluate every epoch
            accuracy = self.evaluate()
            print('--- {}.dev accuracy {} ---\n'.format(j+1, accuracy))

            if accuracy > 0.99:
                self.net.save()
                break

    def dialog_train(self, dialog):
        # create entity tracker
        et = EntityTracker()
        # create action tracker
        at = ActionTracker(et)
        # reset network
        self.net.reset_state()

        loss = 0.
        # iterate through dialog
        for (u, r) in dialog:
            u_ent = et.extract_entities(u)
            u_ent_features = et.context_features()
            u_emb = self.emb.encode(u)
            u_bow = self.bow_enc.encode(u)
            # concat features
            features = np.concatenate((u_ent_features, u_emb, u_bow), axis=0)
            # get action mask
            action_mask = at.action_mask()
            # forward propagation
            #  train step
            loss += self.net.train_step(features, r, action_mask)
        return loss/len(dialog)

    def evaluate(self):
        # create entity tracker
        et = EntityTracker()
        # create action tracker
        at = ActionTracker(et)
        # reset network
        self.net.reset_state()

        dialog_accuracy = 0.
        for dialog_idx in self.dialog_indices_dev:

            start, end = dialog_idx['start'], dialog_idx['end']
            dialog = self.dataset[start:end]
            num_dev_examples = len(self.dialog_indices_dev)

            # create entity tracker
            et = EntityTracker()
            # create action tracker
            at = ActionTracker(et)
            # reset network
            self.net.reset_state()

            # iterate through dialog
            correct_examples = 0
            for (u,r) in dialog:
                # encode utterance
                u_ent = et.extract_entities(u)
                u_ent_features = et.context_features()
                u_emb = self.emb.encode(u)
                u_bow = self.bow_enc.encode(u)
                # concat features
                features = np.concatenate((u_ent_features, u_emb, u_bow), axis=0)
                # get action mask
                action_mask = at.action_mask()
                # forward propagation
                #  train step
                prediction = self.net.forward(features, action_mask)
                correct_examples += int(prediction == r)
            # get dialog accuracy
            dialog_accuracy += correct_examples/len(dialog)

        return dialog_accuracy/num_dev_examples
コード例 #22
0
class Train():
    def __init__(self, args):

        self.response_accuracy = []
        self.dialog_accuracy = []
        try:
            ###################### selective import #############################
            if args[0] == 'am':
                self.is_action_mask = True
            else:
                self.is_action_mask = False
            if args[1] == 'emb':
                self.is_emb = True
            else:
                self.is_emb = False

            self.network_type = args[2]
            self.lang_type = args[3]

            if self.lang_type == 'eng':
                from modules.entities import EntityTracker
                from modules.data_utils import Data
                from modules.actions import ActionTracker
                from modules.bow import BoW_encoder

            elif self.lang_type == 'kor':
                from modules.entities_kor import EntityTracker
                from modules.data_utils_kor import Data
                from modules.actions_kor import ActionTracker
                from modules.bow_kor import BoW_encoder

            ###################################################################
        except:
            rospy.logwarn(
                "please try again. i.e. ... train.py <am> <emb> <bidirectional_lstm> <eng>"
            )

        if self.is_emb:
            if self.lang_type == 'eng':
                self.emb = UtteranceEmbed(lang=self.lang_type)
            elif self.lang_type == 'kor':
                self.emb = UtteranceEmbed(lang=self.lang_type)

        et = EntityTracker()
        self.bow_enc = BoW_encoder()

        at = ActionTracker(et)

        at.do_display_template()
        self.dataset, dialog_indices = Data(et, at).trainset
        # self.dialog_indices_tr = dialog_indices[:200]
        self.dialog_indices_tr = random.sample(dialog_indices, 200)
        # self.dialog_indices_dev = dialog_indices[200:250]
        self.dialog_indices_dev = random.sample(dialog_indices, 50)

        self.action_templates = at.get_action_templates()
        action_size = at.action_size
        nb_hidden = 128

        # set feature input
        if self.is_action_mask and self.is_emb:
            obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features + at.action_size
        elif self.is_action_mask and not (self.is_emb):
            obs_size = self.bow_enc.vocab_size + et.num_features + at.action_size
        elif not (self.is_action_mask) and self.is_emb:
            obs_size = self.emb.dim + self.bow_enc.vocab_size + et.num_features
        elif not (self.is_action_mask) and not (self.is_emb):
            obs_size = self.bow_enc.vocab_size + et.num_features

        # set network_type type
        if self.network_type == 'gru':
            self.net = GRU(obs_size=obs_size,
                           nb_hidden=nb_hidden,
                           action_size=action_size,
                           lang=self.lang_type,
                           is_action_mask=self.is_action_mask)
        elif self.network_type == 'reversed_lstm':
            self.net = ReversingLSTM(obs_size=obs_size,
                                     nb_hidden=nb_hidden,
                                     action_size=action_size,
                                     lang=self.lang_type,
                                     is_action_mask=self.is_action_mask)
        elif self.network_type == 'reversed_gru':
            self.net = ReversingGRU(obs_size=obs_size,
                                    nb_hidden=nb_hidden,
                                    action_size=action_size,
                                    lang=self.lang_type,
                                    is_action_mask=self.is_action_mask)
        elif self.network_type == 'stacked_gru':
            self.net = StackedGRU(obs_size=obs_size,
                                  nb_hidden=nb_hidden,
                                  action_size=action_size,
                                  lang=self.lang_type,
                                  is_action_mask=self.is_action_mask)
        elif self.network_type == 'stacked_lstm':
            self.net = StackedLSTM(obs_size=obs_size,
                                   nb_hidden=nb_hidden,
                                   action_size=action_size,
                                   lang=self.lang_type,
                                   is_action_mask=self.is_action_mask)
        elif self.network_type == 'lstm':
            self.net = LSTM(obs_size=obs_size,
                            nb_hidden=nb_hidden,
                            action_size=action_size,
                            lang=self.lang_type,
                            is_action_mask=self.is_action_mask)
        elif self.network_type == 'bidirectional_lstm':
            self.net = BidirectionalLSTM(obs_size=obs_size,
                                         nb_hidden=nb_hidden,
                                         action_size=action_size,
                                         lang=self.lang_type,
                                         is_action_mask=self.is_action_mask)
        elif self.network_type == 'bidirectional_gru':
            self.net = BidirectionalGRU(obs_size=obs_size,
                                        nb_hidden=nb_hidden,
                                        action_size=action_size,
                                        lang=self.lang_type,
                                        is_action_mask=self.is_action_mask)

        file_path = os.path.join(rospkg.RosPack().get_path('dialogue_system'),
                                 'log', self.network_type)
        # init logging
        self.logger = self.get_logger(file_path)
        msg = "'\033[94m[%s trainer]\033[0m initialized - %s (action_mask: %s, embedding: %s, lang: %s, obs_size: %s, bow: %s, context_feature: %s, action_size: %s)" % (
            rospy.get_name(), self.net.__class__.__name__, self.is_action_mask,
            self.is_emb, self.lang_type, obs_size, self.bow_enc.vocab_size,
            et.num_features, action_size)
        rospy.loginfo(msg)

    def train(self, cont=False):
        # logging and print
        msg = "training started."
        rospy.loginfo(msg)
        # call previous trained model
        if cont:
            self.net.restore()

        epochs = 20
        # start measuring time
        for j in range(epochs):
            num_tr_examples = len(self.dialog_indices_tr)
            loss = 0.

            for i, dialog_idx in enumerate(self.dialog_indices_tr):
                start, end = dialog_idx['start'], dialog_idx['end']
                # train dialog
                loss += self.dialog_train(self.dataset[start:end])
                sys.stdout.write('\r{}.[{}/{}]'.format(j + 1, i + 1,
                                                       num_tr_examples))

            # logging and print
            msg = '\n\n {}.tr loss {}'.format(j + 1, loss / num_tr_examples)
            rospy.loginfo(msg)

            turn_accuracy, dialog_accuracy = self.evaluate()
            msg = '\n{}.dev turn_accuracy {}, dialog_accuracy {}'.format(
                j + 1, turn_accuracy, dialog_accuracy)
            rospy.loginfo(msg)

            if dialog_accuracy > 0.999:
                self.net.save()
                break
        # save checkpoint
        self.net.save()

    def dialog_train(self, dialog):
        ###################################################################
        if self.lang_type == 'eng':
            from modules.entities import EntityTracker
            from modules.data_utils import Data
            from modules.actions import ActionTracker
            from modules.bow import BoW_encoder
        elif self.lang_type == 'kor':
            from modules.entities_kor import EntityTracker
            from modules.data_utils_kor import Data
            from modules.actions_kor import ActionTracker
            from modules.bow_kor import BoW_encoder
        ###################################################################

        et = EntityTracker()
        at = ActionTracker(et)
        # reset state in network_type
        self.net.reset_state()

        loss = 0.
        for (u, r) in dialog:
            u_ent = et.extract_entities(u)
            u_ent_features = et.context_features()
            u_bow = self.bow_enc.encode(u)
            if self.is_emb:
                u_emb = self.emb.encode(u)
            if self.is_action_mask:
                action_mask = at.action_mask()

            # print(u, r)
            # print(u_ent_features)
            # print('================================')
            # print(u_emb)
            # print('================================')
            # print(u_bow)
            # print('================================')
            # print(action_mask)

            # concatenated features
            if self.is_action_mask and self.is_emb:
                features = np.concatenate(
                    (u_ent_features, u_emb, u_bow, action_mask), axis=0)
            elif self.is_action_mask and not (self.is_emb):
                features = np.concatenate((u_ent_features, u_bow, action_mask),
                                          axis=0)
            elif not (self.is_action_mask) and self.is_emb:
                features = np.concatenate((u_ent_features, u_emb, u_bow),
                                          axis=0)
            elif not (self.is_action_mask) and not (self.is_emb):
                features = np.concatenate((u_ent_features, u_bow), axis=0)

            # forward propagation with cumulative loss
            if self.is_action_mask:
                loss += self.net.train_step(features, r, action_mask)
            else:
                loss += self.net.train_step(features, r)

        return loss / len(dialog)

    def evaluate(self, eval=False):
        ###################################################################
        if self.lang_type == 'eng':
            from modules.entities import EntityTracker
            from modules.data_utils import Data
            from modules.actions import ActionTracker
            from modules.bow import BoW_encoder

        elif self.lang_type == 'kor':
            from modules.entities_kor import EntityTracker
            from modules.data_utils_kor import Data
            from modules.actions_kor import ActionTracker
            from modules.bow_kor import BoW_encoder
        ###################################################################

        et = EntityTracker()
        at = ActionTracker(et)
        # only for evaluation purpose
        if eval:
            self.net.restore()
        # reset entities extractor

        turn_accuracy = 0.
        dialog_accuracy = 0.
        for dialog_idx in self.dialog_indices_dev:
            start, end = dialog_idx['start'], dialog_idx['end']
            dialog = self.dataset[start:end]
            num_dev_examples = len(self.dialog_indices_dev)

            et = EntityTracker()
            at = ActionTracker(et)
            # reset network_type before evaluate.
            self.net.reset_state()

            correct_examples = 0
            for (u, r) in dialog:
                u_ent = et.extract_entities(u)
                u_ent_features = et.context_features()
                u_bow = self.bow_enc.encode(u)
                if self.is_emb:
                    u_emb = self.emb.encode(u)
                if self.is_action_mask:
                    action_mask = at.action_mask()

                # concatenated features
                if self.is_action_mask and self.is_emb:
                    features = np.concatenate(
                        (u_ent_features, u_emb, u_bow, action_mask), axis=0)
                elif self.is_action_mask and not (self.is_emb):
                    features = np.concatenate(
                        (u_ent_features, u_bow, action_mask), axis=0)
                elif not (self.is_action_mask) and self.is_emb:
                    features = np.concatenate((u_ent_features, u_emb, u_bow),
                                              axis=0)
                elif not (self.is_action_mask) and not (self.is_emb):
                    features = np.concatenate((u_ent_features, u_bow), axis=0)

                if self.is_action_mask:
                    probs, prediction = self.net.forward(features, action_mask)
                else:
                    probs, prediction = self.net.forward(features)

                correct_examples += int(prediction == r)

            turn_accuracy += correct_examples / len(dialog)

            accuracy = correct_examples / len(dialog)
            if (accuracy == 1.0):
                dialog_accuracy += 1

        turn_accuracy = turn_accuracy / num_dev_examples
        dialog_accuracy = dialog_accuracy / num_dev_examples

        return turn_accuracy, dialog_accuracy

    def get_logger(self, filename):
        """Return a logger instance that writes in filename

        Args:
            filename: (string) path to log.txt

        Returns:
            logger: (instance of logger)

        """
        logger = logging.getLogger('logger')
        logger.setLevel(logging.DEBUG)
        logging.basicConfig(format='%(message)s', level=logging.DEBUG)
        handler = logging.FileHandler(filename)
        handler.setLevel(logging.DEBUG)
        handler.setFormatter(
            logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
        logging.getLogger().addHandler(handler)

        return logger
コード例 #23
0
class InteractiveSession():
    def __init__(self):
        self.bow_enc = BoW_encoder()
        self.emb = UtteranceEmbed()
        self.at = ActionTracker(interact=True)
        print(self.at.dialog_acts)
        _, _, self.other_da, self.other_da_id = self.at.get_data()
        self.action_templates = self.at.action_templates
        self.action_size = self.at.action_size
        #self.responses = [r[0] for r in self.at.responses]
        #print(self.emb.dim , self.bow_enc.vocab_size , self.action_size)
        self.obs_size = self.emb.dim + self.bow_enc.vocab_size + self.action_size
        nb_hidden = 128
        self.exercises = list(self.at.action_templates.keys())
        self.net = LSTM_net(obs_size=self.obs_size,
                            action_size=self.action_size,
                            nb_hidden=nb_hidden)
        # restore checkpoint
        self.net.restore()

        # setup remote interaction via zeromq
        context = zmq.Context.instance()
        # PUB socket for sending system-output
        self.pubSocket: zmq.Socket = context.socket(zmq.PUB)
        self.pubSocket.bind(Config.SYSTEM_OUTPUT_ADDR)
        # SUB socket for receiving user-input
        self.subSocket: zmq.Socket = context.socket(zmq.SUB)
        self.subSocket.connect(Config.USER_INPUT_ADDR)
        self.subSocket.setsockopt_string(zmq.SUBSCRIBE,
                                         Config.USER_INPUT_SUBJECT)
        self.lastOutputMessage: str = None
        self.requestHandler: threading.Thread = None
        self.requestInterrupted: threading.Lock = None

    def has_exercise(self, exercise_name: str) -> bool:
        return exercise_name in self.at.action_templates

    def get_current_exercise(self) -> str:
        return self.at.exercise

    def send(self, message, utteranceType, state, dialogAct):
        print(">>", message)
        data = SystemOutputData(text=message,
                                utterance_type=utteranceType,
                                state=state,
                                dialogue_act=dialogAct)
        msg = SystemOutputMessage(type=MessageType.OUTPUT,
                                  timestamp=int(time.time() * 1000),
                                  data=data)
        jsonMsg = json.dumps(msg.to_dict())
        self.lastOutputMessage = jsonMsg
        rawMsg = "%s %s" % (to_enum(MessageType, msg.type), jsonMsg)
        # print("  ", rawMsg)#DEBUG
        self.pubSocket.send_string(rawMsg)

    def receive(self) -> 'UserInputMessage':
        rawMsg = self.subSocket.recv_string()
        # print("  ", rawMsg)#DEBUG
        # FIXME detect & handle when message has other type than "input"
        # TODO handle message types other than "input"
        msgTypeStr = to_enum(MessageType, MessageType.INPUT)
        # extract JSON message by removing subject/subscription string from beginning of message:
        msgStr = rawMsg.replace(msgTypeStr, "", 1).strip()
        return user_input_message_from_dict(json.loads(msgStr))

    def start_processing_requests(self, verbose: bool = False):
        self.stop_processing_requests()
        self.requestInterrupted = threading.Lock()
        self.requestInterrupted.acquire()
        self.requestHandler = threading.Thread(
            target=request_worker,
            args=(Config.QUERY_SYSTEM_OUTPUT_ADDR, self,
                  self.requestInterrupted, verbose))
        self.requestHandler.start()

    def stop_processing_requests(self):
        if self.requestInterrupted is not None and self.requestHandler is not None and self.requestHandler.isAlive(
        ):
            self.requestInterrupted.release()
            # print("should stop request_worker thread now!")
            self.requestHandler.join()
            print("stopped request_worker thread.")

    def interact_anew(self,
                      verbose,
                      update,
                      out_file,
                      new_state_name: str = None):
        prev_prediction = -1
        self.net.reset_state()
        if new_state_name is None:
            new_state_name = random.choice(self.exercises)
        self.at.set_exercise_string(new_state_name)
        self.at.memory.reset_memory(self.at.exercise)
        self.at.initialize_am()

        u = "start"
        if verbose == True:
            print("\n*******************\nÄUßERUNGSTYP: ",
                  self.at.automaton.get_utterance_type(u))

        action_mask = self.at.am
        u_emb = self.emb.encode(u)  # HIER
        u_bow = self.bow_enc.encode(u)  # HIER
        features = np.concatenate((u_emb, u_bow, action_mask), axis=0)
        # forward
        prediction = self.net.forward(features,
                                      action_mask,
                                      self.other_da,
                                      print_top=True)
        self.at.automaton.current_state, response = self.at.walk(u, prediction)
        prev_prediction = prediction

        if verbose == True:
            print("AKTUELLER ZUSTAND: ", self.at.automaton.current_state.name,
                  "\nDIALOGAKT: ", self.other_da[prediction],
                  "\n*******************\n"
                  )  # print("CURRENT Exercise: ", self.at.exercise)

        # self.net.train_step(features, prediction, action_mask)
        # response = self.at.get_text(prediction)
        self.send(response, self.at.automaton.get_utterance_type(u),
                  self.at.automaton.current_state.name,
                  self.at.dialog_acts[prediction])  # print('>>', response)
        if out_file:
            out_file.write(u + '\t' + response + '\t' +
                           self.other_da[prediction] + '\n')
        return prev_prediction

    def interact(self, verbose, update, write_to_file):
        print(
            "How do you do? I'm glad you're here. Let's do some exercises together."
        )
        # open file to write around here
        # only if write_to_file !
        with open('data/dialogs_from_interaction', 'a') as out:
            with open("data/utterances_from_interaction", "a") as uttout:
                self.start_processing_requests(verbose)
                out_file = None
                if write_to_file:
                    out_file = out
                prev_prediction = self.interact_anew(verbose, update, out_file)
                # begin interaction loop
                while True:
                    if verbose == True:
                        print([
                            self.other_da[i] for i in range(len(self.at.am))
                            if self.at.am[i] == 1
                        ])
                    #print("prediction: ",self.at.dialog_acts[prediction])
                    #print("response: ",response)

                    # get input from user; utterance isn't saved!
                    message = self.receive()  # u = input(':: ')
                    u = message.data.strip()
                    print(":: %s" % (u))

                    # check if user wants to begin new session
                    if u == 'clear' or u == 'reset' or u == 'restart' or u == 'von vorne' or u == 'neustart':
                        if write_to_file:
                            out.write("WARNING: Interaction was reset.\n\n")
                        prev_prediction = self.interact_anew(
                            verbose, update, out_file)
                        if write_to_file:
                            uttout.write(
                                u + "\t" +
                                self.at.automaton.get_utterance_type(u) + "\n")

                    elif self.other_da[prev_prediction] in [
                            "closing", "finish"
                    ]:
                        if write_to_file:
                            out.write('\n')
                        prev_prediction = self.interact_anew(
                            verbose, update, out_file)

                    # check for exit command
                    elif u == 'exit' or u == 'quit':

                        if update == True:
                            self.net.save()
                        self.send(
                            'Ok, wir brechen ab. Bis zum nächsten Mal!',
                            'EXIT COMMAND',
                            self.at.automaton.current_state.name, "UNKNOWN"
                        )  # print('>> Ok, wir brechen ab. Bis zum nächsten Mal!')
                        if write_to_file:
                            out.write("WARNING: Dialog may not be finished.\n")
                        break

                    else:
                        if u == "\n" or u == "":
                            u = '<SILENCE>'
                        #self.at.reset_action_mask()

                        if self.at.automaton.current_state.name == "all_one":
                            if verbose == True:
                                print("\n*******************\nÄUßERUNGSTYP: ",
                                      self.at.automaton.get_utterance_type(u))
                            ##print(self.at.automaton.current_state.name, self.at.dialog_acts[prev_prediction])
                        else:
                            if verbose == True:
                                print("\n*******************\nÄUßERUNGSTYP: ",
                                      self.at.automaton.get_utterance_type(u))
                        action_mask = self.at.am
                        u_emb = self.emb.encode(u)  #HIER
                        u_bow = self.bow_enc.encode(u)  #HIER
                        features = np.concatenate((u_emb, u_bow, action_mask),
                                                  axis=0)
                        # forward

                        prediction = self.net.forward(features,
                                                      action_mask,
                                                      self.other_da,
                                                      print_top=True)
                        self.at.automaton.current_state, response = self.at.walk(
                            u, prediction)
                        if prediction != "all_one":
                            prev_prediction = prediction

                        #self.at.memory.update(prediction)
                        if verbose == True:
                            print(
                                "AKTUELLER ZUSTAND: ",
                                self.at.automaton.current_state.name,
                                "\nDIALOGAKT: ", self.other_da[prediction],
                                "\n*******************\n"
                            )  #print("CURRENT Exercise: ", self.at.exercise)

                        self.net.train_step(features, prediction, action_mask)
                        self.send(response,
                                  self.at.automaton.get_utterance_type(u),
                                  self.at.automaton.current_state.name,
                                  self.at.dialog_acts[prediction]
                                  )  # print('>>', response)
                        if write_to_file:
                            out.write(u + '\t' + response + '\t' +
                                      self.other_da[prediction] + '\n')
                            uttout.write(
                                u + "\t" +
                                self.at.automaton.get_utterance_type(u) + "\n")
                if write_to_file:
                    out.write('\n')
                self.stop_processing_requests()