Example #1
0
    def do_alter_resnet_mutation(self, position, indi):
        """
        ----out_channel of resnet
        ----amount in one resnet
        """
        mutation_p_type = ''
        mutation_p_count = 0

        u_ = random.random()
        if u_ < 0.5:
            mutation_p_type = 'RESNET_OUT_CHANNEL'
            channel_list = StatusUpdateTool().get_output_channel()
            index_ = int(np.floor(np.random.random() * len(channel_list)))
            if indi.units[position].out_channel != channel_list[index_]:
                self.log.info(
                    'Unit at %d changes its output channel from %d to %d' %
                    (position, indi.units[position].out_channel,
                     channel_list[index_]))
                indi.units[position].out_channel = channel_list[index_]

                keep_out_channel = channel_list[index_]
                for i in range(position + 1, len(indi.units)):
                    if indi.units[i].type == 1 or indi.units[i].type == 3:
                        self.log.info(
                            'Due to above, the unit at %d should change its input channel from %d to %d'
                            % (i, indi.units[i].in_channel, keep_out_channel))
                        indi.units[i].in_channel = keep_out_channel
                        if indi.units[i].type == 1:
                            break
                        elif indi.units[i].type == 3:
                            estimated_out_channel = indi.units[
                                i].k * indi.units[i].amount + indi.units[
                                    i].in_channel
                            if estimated_out_channel > indi.units[
                                    i].out_channel:
                                break
                            else:
                                self.log.info(
                                    'Due to the above mutation, unit at %d changes its output channel from %d to %d'
                                    % (i, indi.units[i].out_channel,
                                       estimated_out_channel))
                                indi.units[
                                    i].out_channel = estimated_out_channel
                                keep_out_channel = estimated_out_channel

                mutation_p_count = 1
                indi.reset_acc()
        else:
            mutation_p_type = 'RESNET_AMOUNT'
            min_resnet_unit, max_resnet_unit = StatusUpdateTool.get_resnet_unit_length_limit(
            )
            amount = np.random.randint(min_resnet_unit, max_resnet_unit)
            if amount != indi.units[position].amount:
                self.log.info('Unit at %d changes its amount from %d to %d' %
                              (position, indi.units[position].amount, amount))
                indi.units[position].amount = amount
                mutation_p_count = 1
                indi.reset_acc()
        return mutation_p_type, mutation_p_count
    def do_modify_conv_mutation(self, indi):
        self.log.info('Do the CHANNEL mutation for indi:%s'%(indi.id))
        conv_index_list = []
        for i, unit in enumerate(indi.units):
            if unit.type == 1:
                conv_index_list.append(i)
        if len(conv_index_list) == 0:
            self.log.warn('No CONV unit exist in current individual, no mutation occurs')
        else:
            selected_index = int(np.floor(np.random.rand()*len(conv_index_list)))
            self.log.info('Mutation position %d'%(conv_index_list[selected_index]))

            channel_list = StatusUpdateTool().get_output_channel()
            index_ = int(np.floor(np.random.random()*len(channel_list)))
            if indi.units[conv_index_list[selected_index]].in_channel != channel_list[index_]:
                indi.reset_acc()
                if selected_index > 0:
                    self.log.info('Unit at %d changes its input channel from %d to %d'%(conv_index_list[selected_index], indi.units[conv_index_list[selected_index]].in_channel, channel_list[index_]))
                    indi.units[conv_index_list[selected_index]].in_channel = channel_list[index_]
                    self.log.info('Due to above, the unit at %d should change its output channel from %d to %d'%(conv_index_list[selected_index-1], indi.units[conv_index_list[selected_index-1]].out_channel, channel_list[index_]))
                    indi.units[conv_index_list[selected_index-1]].out_channel = channel_list[index_]
                else:
                    self.log.warn('Mutation position is 0, the input channel should not be changed')
            else:
                self.log.info('Unit at %d changes its input channel from %d to %d'%(conv_index_list[selected_index], indi.units[conv_index_list[selected_index]].in_channel, channel_list[index_]))

            index_ = int(np.floor(np.random.random()*len(channel_list)))
            if indi.units[conv_index_list[selected_index]].out_channel != channel_list[index_]:
                indi.reset_acc()
                self.log.info('Unit at %d changes its out channel from %d to %d'%(conv_index_list[selected_index], indi.units[conv_index_list[selected_index]].out_channel, channel_list[index_]))
                indi.units[conv_index_list[selected_index]].out_channel = channel_list[index_]
                if selected_index < len(conv_index_list)-1:
                    self.log.info('Due to above, the unit at %d should change its input channel from %d to %d'%(conv_index_list[selected_index+1], indi.units[conv_index_list[selected_index+1]].in_channel, channel_list[index_]))
                    indi.units[conv_index_list[selected_index+1]].in_channel = channel_list[index_]
                else:
                    self.log.info('Unit at %d is the last unit in the individual, therefore no need to change the input channel of the next unit')
            else:
                self.log.info('Unit at %d changes its out channel from %d to %d'%(conv_index_list[selected_index], indi.units[conv_index_list[selected_index]].out_channel, channel_list[index_]))