Ejemplo n.º 1
0
    def gen_rand_stimuli(self, n_inputs, max_zero_block, p_zero):
        # generate random stimuli and return them as a list of (data_i, last_i) tuples
        data_decoded = []
        znz_stream = []
        nz_data = []
        i = 0
        while i < n_inputs:
            if (np.random.binomial(1, p_zero)):
                zero_len = min(random.randint(1, max_zero_block), n_inputs-i)
                znz_stream += [0] * zero_len
                i += zero_len
                data_decoded += [0]*zero_len
            else:
                znz_stream.append(1)
                dat = get_random_nonzero_in_range(-2**(self.data_w-1), 2**(self.data_w-1)-1)
                nz_data.append(dat)
                data_decoded.append(dat)
                i += 1
        nz_data = zero_pad_list(nz_data, self.block_size)
        self.inputs['bpc'] = bpc.BPC_words(np.array(nz_data),self.block_size,variant='paper',word_w=self.data_w)
        self.inputs['znz'] = bpc.ZRLE_words(data_decoded, max_burst_len=self.max_zrle_len, wordwidth=self.data_w)
        data_decoded_bin = valuesToBinary(np.array(data_decoded), wordwidth=self.data_w)
        self.outputs_exp += split_str(data_decoded_bin, self.data_w)

        # very pythonic.
        return int_list_to_binval_list(self.inputs['bpc'], self.data_w, True), int_list_to_binval_list(self.inputs['znz'], self.data_w, True)
Ejemplo n.º 2
0
 def gen_rand_stimuli(self):
     vals = random_vals(self.num_blocks * self.block_size, self.data_w)
     self.outputs_exp = int_list_to_binval_list(vals, self.data_w, True)
     vals = bpc.BPC_words(np.array(vals),
                          block_size=self.block_size,
                          variant='paper',
                          word_w=self.data_w,
                          dbg_fn=self.stim_dbg_file)
     self.inputs = int_list_to_binval_list(vals, self.data_w, True)
     self.report_stim(self.stim_report_file)
     return self.inputs
    def gen_rand_stimuli(self, n_inputs, max_zero_block, p_zero):
        # generate random stimuli and return them as a list of (data_i, last_i) tuples
        data_in = []
        last_in = []
        znz_stream = []
        nz_data = []
        i = 0
        while i < n_inputs:
            if (np.random.binomial(1, p_zero)):
                zero_len = min(random.randint(1, max_zero_block), n_inputs - i)
                znz_stream += [0] * zero_len
                i += zero_len
                data_in += [0] * zero_len
            else:
                znz_stream.append(1)
                dat = get_random_nonzero_in_range(-2**(self.data_w - 1),
                                                  2**(self.data_w - 1) - 1)
                nz_data.append(dat)
                data_in.append(dat)
                i += 1
        last_in += ([0] * (len(data_in) - 1) + [1])
        nz_data = zero_pad_list(nz_data, self.block_size)
        self.internal_inputs_exp['bpc'] += int_list_to_binval_list(
            nz_data, n_bits=self.data_w, signed=True)
        self.internal_inputs_exp['znz'] += int_list_to_binval_list(
            znz_stream, n_bits=1, signed=False)
        self.outputs_exp['bpc'] = bpc.BPC_words(np.array(nz_data),
                                                self.block_size,
                                                variant='paper',
                                                word_w=self.data_w)
        self.outputs_exp['znz'] = bpc.ZRLE_words(
            data_in, max_burst_len=self.max_zrle_len, wordwidth=self.data_w)
        self.inputs['data'] = int_list_to_binval_list(data_in,
                                                      n_bits=self.data_w,
                                                      signed=True)
        self.inputs['last'] = int_list_to_binval_list(last_in,
                                                      n_bits=1,
                                                      signed=False)

        # very pythonic.
        return list(zip(*self.inputs.values()))
 def gen_fmap_stimuli(self,
                      model,
                      dataset_path,
                      num_batches,
                      batch_size,
                      signed=True,
                      fmap_frac=0.01):
     assert self.data_w in [8, 16, 32]
     fms_q = data.getStimuli(model=model,
                             dataset_path=dataset_path,
                             data_w=self.data_w,
                             num_batches=num_batches,
                             batch_size=batch_size,
                             signed=signed,
                             fmap_frac=fmap_frac)
     nz_data = [el for el in fms_q if el != 0]
     nz_data = zero_pad_list(nz_data, self.block_size)
     znz_stream = [0 if el == 0 else 1 for el in fms_q]
     last_in = ([0] * (len(fms_q) - 1) + [1])
     self.inputs['data'] = int_list_to_binval_list(fms_q,
                                                   self.data_w,
                                                   signed=signed)
     self.inputs['last'] = int_list_to_binval_list(last_in,
                                                   n_bits=1,
                                                   signed=False)
     self.internal_inputs_exp['bpc'] += int_list_to_binval_list(
         nz_data, n_bits=self.data_w, signed=True)
     self.internal_inputs_exp['znz'] += int_list_to_binval_list(
         znz_stream, n_bits=1, signed=False)
     self.outputs_exp['bpc'] = bpc.BPC_words(np.array(nz_data),
                                             self.block_size,
                                             variant='paper',
                                             word_w=self.data_w)
     self.outputs_exp['znz'] = bpc.ZRLE_words(
         fms_q, max_burst_len=self.max_zrle_len, wordwidth=self.data_w)
     return list(zip(*self.inputs.values())), len(
         self.outputs_exp['bpc']), len(self.outputs_exp['znz'])
Ejemplo n.º 5
0
def genStimFiles(file_prefixes,
                 data_w,
                 *args,
                 modules=['encoder', 'decoder'],
                 max_zrle_len=16,
                 block_size=8,
                 debug_file=None,
                 num_words_w=24,
                 **kwargs):
    fms_q = getStimuli(*args, data_w=data_w, **kwargs)
    fms_q = np.array(fms_q)
    fms_bin = bpc.valuesToBinary(fms_q, data_w)
    fms_bin = split_str(fms_bin, data_w)
    last_bin = ['0'] * (len(fms_q) - 1) + ['1']
    nz_data = [el for el in fms_q if el != 0]
    nz_data_padded = zero_pad_list(nz_data, block_size)
    bpc_vals, mod_bpc_len = bpc.BPC_words(np.array(nz_data_padded),
                                          block_size=block_size,
                                          variant='paper',
                                          word_w=8,
                                          dbg_fn=debug_file,
                                          return_mod_len=True)
    while kwargs['model'] == 'last_test' and mod_bpc_len != 0:
        fms_q = getStimuli(*args, data_w=data_w, **kwargs)
        fms_q = np.array(fms_q)
        fms_bin = bpc.valuesToBinary(fms_q, data_w)
        fms_bin = split_str(fms_bin, data_w)
        last_bin = ['0'] * (len(fms_q) - 1) + ['1']
        nz_data = [el for el in fms_q if el != 0]
        nz_data_padded = zero_pad_list(nz_data, block_size)
        bpc_vals, mod_bpc_len = bpc.BPC_words(np.array(nz_data_padded),
                                              block_size=block_size,
                                              variant='paper',
                                              word_w=8,
                                              dbg_fn=debug_file,
                                              return_mod_len=True)

    znz_vals = bpc.ZRLE_words(fms_q,
                              max_burst_len=max_zrle_len,
                              wordwidth=data_w)
    nw_bin = bpc.valuesToBinary(np.array([len(fms_q) - 1]), num_words_w)
    compr_ratio = len(fms_q) / (len(bpc_vals) + len(znz_vals))
    sparsity = 1 - len(nz_data) / len(fms_q)
    znz_last = ['0'] * (len(znz_vals) - 1) + ['1']
    bpc_last = ['0'] * (len(bpc_vals) - 1) + ['1']
    stat_dict = {
        'Compression Ratio': compr_ratio,
        'Sparsity': sparsity,
        '# of Stimuli': len(fms_q)
    }
    if 'encoder' in modules:
        write_sim_file(data=zip(fms_bin, last_bin),
                       filename=file_prefixes['encoder'] + '_input.stim',
                       length=len(fms_bin))
        write_sim_file(zip(bpc_vals, bpc_last),
                       file_prefixes['encoder'] + '_bpc.expresp',
                       len(bpc_vals))
        write_sim_file(zip(znz_vals, znz_last),
                       file_prefixes['encoder'] + '_znz.expresp',
                       len(znz_vals))
        write_stats(file_prefixes['encoder'] + '_stats.log', stat_dict)
    if 'decoder' in modules:
        write_sim_file(data=zip([nw_bin]),
                       filename=file_prefixes['decoder'] +
                       '_num_words_input.stim',
                       length=1)
        write_sim_file(zip(bpc_vals, bpc_last),
                       file_prefixes['decoder'] + '_bpc_input.stim',
                       len(bpc_vals))
        write_sim_file(zip(znz_vals, znz_last),
                       file_prefixes['decoder'] + '_znz_input.stim',
                       len(znz_vals))
        write_sim_file(zip(fms_bin, last_bin),
                       file_prefixes['decoder'] + '_data_output.expresp',
                       len(fms_q))
        write_stats(file_prefixes['decoder'] + '_stats.log', stat_dict)