Пример #1
0
    def generate_disk(self):
        GCdist = coord.Galactocentric.galcen_distance.value * 1000.

        h = self.h
        H = self.H
        rho0 = self.rho0

        theta = np.random.uniform(np.pi, 2 * np.pi, N)

        # the r component
        rho_r = lambda r: np.exp(-r / H) / np.exp(-8300. / H
                                                  )  # normalised density
        pdf_r = lambda r: 2 * np.pi * r * rho_r(r)  # cylindrical coordiantes
        cdf_r = lambda l: integrate.quad(pdf_r, 0., l)
        rgrid = np.hstack(([0], np.logspace(0, 7, 10000)))
        totr = cdf_r(np.inf)[0]
        icdf_r = lambda n: np.interp(n, [cdf_r(_r)[0] / totr
                                         for _r in rgrid], rgrid)

        # the z compoment
        rho_z = lambda z: np.exp(-z / h) / np.exp(-27. / h
                                                  )  # normalised density
        cdf_z = lambda l: integrate.quad(rho_z, 0, l)  # cdf
        rgrid = np.hstack(([0], np.logspace(-2, 5, 10000)))
        totz = cdf_z(np.inf)[0]
        icdf_z = lambda n: np.interp(n, [cdf_z(_r)[0] / totz
                                         for _r in rgrid], rgrid)

        # total number of points in disk
        N = rho0 * cdf_r(np.inf) * 2 * cdf_z(
            np.inf)  # factor 2 is from z and -z

        r = icdf_r[np.uniform(N)]  # make r
        z = icdf_z[np.uniform(N)]  # make z
        z *= (-1)**np.random.randint(0, 2,
                                     N)  # give z positive and negative values

        # make x,y
        x = r * np.sin(theta)
        y = r * np.cos(theta)

        # density around the sun:
        self.c = SkyCoord(x=x * u.pc,
                          y=y * u.pc,
                          z=z * u.pc,
                          frame='galactocentric')
        self.scale = scale
        self.eq = self.c.transform_to(coord.ICRS)
        self.gc = self.c.transform_to(coord.Galactic)
Пример #2
0
def generate_circleDuration(period, i, decay=0, radius=1, x0=0, y0=0):
    R = radius
    x_0 = x0
    y_0 = y0
    d = np.uniform(0.01, 0.1)
    # for t in range(0, 2 * pi, 0.01):
    dd = list()
    zz = list()
    for t in range(1000):
        t = t / 1000.0
        x = (R * np.cos(2 * pi * t / period) + R) / 2 * R + x_0
        y = (R * np.sin(2 * pi * t / period) + R) / 2 * R + y_0
        dd.append(y)
        zz.append(x)
    miy = min(dd)
    mix = min(zz)
    may = max(dd)
    max = max(zz)

    if i > period:
        return [-1, -1]

    x = (R * np.cos(2 * pi * i / period) + R) / 2 * R + x_0
    y = (R * np.sin(2 * pi * i / period) + R) / 2 * R + y_0

    return [x, y]
Пример #3
0
def generate_Heart(period, i, decay):
    x_0 = np.randint(0, 1)
    y_0 = np.randint(0, 1)
    d = np.uniform(0.01, 0.1)
    t = i
    dd = list()
    zz = list()
    for t in range(1000):
        t = t / 1000.0
        t = 2 * pi * t / period
        x = 16 * pow(np.sin(t), 3)
        y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(
            4 * t)
        dd.append(y)
        zz.append(x)
    miy = min(dd)
    mix = min(zz)
    may = max(dd)
    maxx = max(zz)

    i = 2 * pi * i / period
    x = 16 * pow(np.sin(i), 3)
    y = 13 * np.cos(i) - 5 * np.cos(2 * i) - 2 * np.cos(3 * i) - np.cos(4 * i)

    x = (x - mix) / (maxx - mix)
    y = (y - miy) / (may - miy)

    return [x, y]
def queries_answers_weights_sol():
    import opt
    import cenquery
    import opt.cvx as cvx
    shape=(6,4,5)
    q2_subset = (slice(3,5), slice(2,4), slice(0,2))
    q2_answer_shape = (2,2)
    prng = numpy.random.RandomState(seed)
    seed=23
    q1 = cenquery.Query(shape)
    q2 = cenquery.Query(shape, subset=q2_subset, add_over_margins=(0,))
    queries = [q1, q2]
    ans1 = numpy.uniform(low=-0.5, high=0.5, size=shape)
    ans2 = numpy.uniform(low=-0.5, high=0.5, size=q2_answer_shape)
    answers = [ans1, ans2]
    weights = [1.0, 2.0]
    solution = None
    return (queries, answers, weights, solution)
Пример #5
0
def generate_examplesX(length, n_patterns, output):
    X, y = list(), list()
    for _ in range(n_patterns):
        p = np.randint(10, 20)
        d = np.uniform(0.01, 0.1)
        sequence = [0, 1]  # generate_sequenceDampedSin(length + output, p, d)
        X.append(sequence[:-output])
        y.append(sequence[-output:])
    X = np.array(X).reshape(n_patterns, length, 1)
    y = np.array(y).reshape(n_patterns, output)
    return X, y
Пример #6
0
def mcerror(lmbda,nflux,wavelength1,wavelength2,m,c,rmse):
    trials  = 3000
    guess   = []
    ewindex = np.where((lmbda >= wavelength1) & (lmbda <= wavelength2))
    dlmbda  = lmbda[2]-lmbda[1]

    for i in range(trials):
        fitline = m * lmbda + c + np.uniform(-1,1) * rmse
        fx = np.sum(1-nflux[ewindex]) * dlmbda
        guess.append(fx)

    return np.mean(guess), np.std(guess)
Пример #7
0
 def run_kwargs(self):
     kwargs = dict(exe="../fast/fast",
                   persist_hits=False,
                   noise_if_no_signal=True)
     # Dice number of particles
     n = np.random.random_integers(self.n_min, self.n_max, self.step)
     if self.log:
         n = np.array(np.ceil(
             np.exp(
                 np.uniform(np.log(self.n_min), np.log(self.n_max),
                            self.step))),
                      dtype="int")
     return [clone(kwargs, n_particles=ni) for ni in n]
Пример #8
0
def generate_circle(period, i, decay=0, radius=1, x0=0, y0=0):
    R = radius
    x_0 = x0
    y_0 = y0
    d = np.uniform(0.01, 0.1)
    # for t in range(0, 2 * pi, 0.01):
    dd = list()
    zz = list()
    for t in range(1000):
        t = t / 1000.0
        x = (R * np.cos(2 * pi * t / period) + R) / 2 * R + x_0
        y = (R * np.sin(2 * pi * t / period) + R) / 2 * R + y_0
        dd.append(y)
        zz.append(x)

    x = (R * np.cos(2 * pi * i / period) + R) / 2 * R + x_0
    y = (R * np.sin(2 * pi * i / period) + R) / 2 * R + y_0

    return [x, y]
Пример #9
0
def findnextorbit(
        m_star, m_planet, disk_edge,
        iceline):  #Note disk_edge is orbit of planet before in ordinal number

    m_star = m_star  #solar masses
    m_star_kg = np.multiply(m_star, 1.989e30)
    m_planet = m_planet  # earth masses, preferable the isolation mass
    m_planet_kg = np.multiply(m_planet, 5.972e24)
    disk_edge = disk_edge  # in au
    rn.seed(rn.random())
    Klist = np.uniform(10, 15, 100).tolist()
    factor = np.power(np.divide(m_planet_kg, 3. * m_star_kg), 1. / 3.)

    d = disk_edge

    c = rn.choice(Klist) * d * factor
    b = d + c

    return b
 def run_kwargs(self):
     e = 4.135667516e-15 * 299792458.0 / (self.wavelength * 1e-9)
     kwargs = dict(exe="../fast/fast",
                   persist_hits=False,
                   noise_if_no_signal=True,
                   t_input="../sample/resources/led-waveform-12ns.txt",
                   persist_t_min=0,
                   persist_t_max=250,
                   e_min=e,
                   e_max=e,
                   simulate_shuntresistor=True,
                   shuntresistor_recovery_time=30.0)
     # Dice number of particles
     n = np.random.random_integers(self.n_min, self.n_max, self.step)
     if self.log:
         n = np.array(np.ceil(
             np.exp(
                 np.uniform(np.log(self.n_min), np.log(self.n_max),
                            self.step))),
                      dtype="int")
     return [clone(kwargs, n_particles=ni) for ni in n]
Пример #11
0
for i in xrange(20):
    all_lifters.append(common_model((10, )))

for _ in xrange(EPOCHS):
    az_ang = random.uniform(-np.pi, np.pi)
    ev_ang = random.uniform(-np.pi / 9, np.pi / 9)
    rot_ang = random.uniform(0, 2 * np.pi)
    x = np.cos(ev_ang) * np.cos(az_ang)
    y = np.cos(ev_ang) * np.sin(az_ang)
    z = np.sin(ev_ang)
    C = np.array([[0, -z, y], [z, 0, -x], [-y, x, 0]])

    R = np.identity(
        3) + np.sin(rot_ang) * C + (1 - np.cos(rot_ang)) * np.matmul(C, C)
    T = np.uniform(0, 1)

    for model in all_lifters:
        for naam in files:
            for i in xrange(20):
                X = files[naam][2 * i:2 * i + 2]
                X.extend(files[naam][-8:])
                X.extend([0., 0., 0.])
                z = all_lifters[i].predict(X)

                x_ = X[0] * z
                y_ = X[1] * z
                z_ = z

                x_new, y_new, z_new = np.matmul(R, [[x_], [y_], [z_]])
                x_new, y_new, z_new = x_new[0], y_new[0], z_new[0]
Пример #12
0
def concatenateStimuli(MatrixDir, OutDir, Length, n):
    # Get matrix wav file paths
    wavFiles = globDir(MatrixDir, '*.wav')

    stim_parts = os.path.join(MatrixDir, "stim_parts.csv")
    stim_words = os.path.join(MatrixDir, "stim_words.csv")
    stim_part_rows = []
    with open(stim_parts, 'r') as csvfile:
        stim_part_rows = [line for line in csv.reader(csvfile)]
    with open(stim_words, 'r') as csvfile:
        stim_word_rows = [line for line in csv.reader(csvfile)]

    wavFiles = natsorted(wavFiles)
    totalSize = 0
    y = []
    parts = []
    questions = []
    i = 0
    gapSize = np.uniform(0.8, 1.2, len(wavFiles))
    for wav, gap in zip(wavFiles, gapSize):
        if i == n:
            break
        wavObj = PySndfile(wav)
        fs = wavObj.samplerate()
        size = wavObj.frames()
        totalSize += size
        totalSize += int(gap * fs)
        if (totalSize / fs) > Length:
            # total size + 2 second silence at start
            y.append(np.zeros((totalSize + 2 * fs, 3)))
            parts.append([])
            questions.append([])
            i += 1
            totalSize = 0

    writePtr = 2 * fs
    idx = np.arange(0, writePtr)
    chunk = np.zeros(idx.size)
    chunk = np.vstack([chunk, chunk, chunk]).T
    trigger = gen_trigger(idx, 2., 0.01, fs)
    chunk[:, 2] = trigger
    for i, _ in enumerate(y):
        y[i][0:writePtr, :] = chunk

    i = 0
    for wav, word, part in zip(wavFiles, stim_word_rows, stim_part_rows):
        if writePtr >= y[i].shape[0]:
            i += 1
            writePtr = fs * 2
        if i == n:
            break
        x, fs, encStr, fmtStr = sndio.read(wav, return_format=True)
        threeMs = int(0.1 * fs)
        silence = np.zeros(threeMs)
        chunk = np.append(x, silence)

        idx = np.arange(writePtr, writePtr + chunk.shape[0])
        chunk = np.vstack([chunk, chunk, np.zeros(chunk.shape[0])]).T
        trigger = gen_trigger(idx, 2., 0.01, fs)
        chunk[:, 2] = trigger

        y[i][writePtr:writePtr + chunk.shape[0], :] = chunk
        questions[i].append(word)
        parts[i].append(part)

        writePtr += chunk.shape[0]

    for ind, (data, q, p) in enumerate(zip(y, questions, parts)):
        pysndfile.sndio.write(os.path.join(OutDir, 'stim_{}.wav'.format(ind)),
                              data,
                              format=fmtStr,
                              enc=encStr)
        with open('./out/stim/stim_words_{}.csv'.format(ind), 'w') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerows(q)
        with open('./out/stim/stim_parts_{}.csv'.format(ind), 'w') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerows(p)
 def select_rand(i):
     j = i
     while j == i:
         j = int(np.uniform(0, self.m))
     return j
Пример #14
0
import pandas as pd
import matplotlib.pyplot as plot
import numpy as np

filePath = ("c://dataTest.csv")
dataFile = pd.read_csv(filePath, header=None, prefix="V")

target = []
for i in range(200):
    if dataFile.iat[i, 10] >= 7:
        target.append(1.0 + np.uniform(-0.3, 0.3))
    else:
        target.append(0.0 + np.uniform(-0.3, 0.3))
dataRow = dataFile.iloc[0:200, 10]
plot.scatter(dataRow, target, alpha=0.5, s=100)
plot.xlabel("Attribute")
plot.ylabel("Target")
plot.show()
Пример #15
0
def random_site( seq_length, model_length ):
    return Site(
            start = numpy.randint( seq_length - model_length ),
            rev_comp = numpy.uniform() > 0.5,
            length = model_length
    )
Пример #16
0
def metaEvolution(trainDates, testDate, metaPopulationSize=15, useCrossover=False, useGenerationForcing=False, sendEmails=False, createGraphs=False):
    print("RUNNING META EVOLUTION")
    #set first generation genomes
    metaPopulation = []
    prevAvgFit = 0
    prevPopulation = []
    prevFitnesses = []
    prevTopGenome = []
    keys = list(metaGenomeDictionary.keys())
    for i in range(metaPopulationSize):
        currentGenome = {}
        for x in range(len(keys)):
            if type(metaGenomeDictionary[keys[x]]["start"][0]) == np.float64:
                currentGenome[keys[x]] = np.uniform(metaGenomeDictionary[keys[x]]["start"][0], metaGenomeDictionary[keys[x]]["start"][1])
            elif metaGenomeDictionary[keys[x]]["clip"][0] == None and metaGenomeDictionary[keys[x]]["clip"][1] == None:
                currentGenome[keys[x]] = random.randint(0, 1)
            else:
                currentGenome[keys[x]] = random.randint(int(metaGenomeDictionary[keys[x]]["start"][0]), int(metaGenomeDictionary[keys[x]]["start"][1]))
        metaPopulation.append(currentGenome)

    metaGenFitnesses = []
    metaGenTestFitnesses = []
    for genNum in range(100): #meta generations
        metaFitnesses = []
        rawMetaFitnesses = []

        #get fitnesses for each meta genome by evolving a model with them
        for i in range(len(metaPopulation)):
            tempMetaFitnesses = []
            for x in range(len(trainDates)):
                tempMetaFitnesses.append(float(modelEvolution(metaPopulation[i], [trainDates[x]])))
            currentMetaFitness = statistics.mean(tempMetaFitnesses)
            rawMetaFitnesses.append(currentMetaFitness)
            metaFitnesses.append(currentMetaFitness / 200)
            print("Gen " + str(genNum + 1) + " Genome " + str(i + 1) +": " + str(round(rawMetaFitnesses[-1], 2)))


        topGenome = copy.deepcopy(metaPopulation)[metaFitnesses.index(max(metaFitnesses))]
        #report the best fitness
        testMetaFitness = modelEvolution(topGenome, [testDate])
        printString = "Gen " + str(genNum + 1) + " | Top Fitness: " + str(max(rawMetaFitnesses)) + " | Avg Fitnesses: " + str(sum(rawMetaFitnesses) / len(metaFitnesses)) + " | Test Fitness: " + str(testMetaFitness) + " | Genome: " + str(topGenome)
        print(printString)

        if createGraphs:
            #make graph
            metaGenFitnesses.append(statistics.mean(rawMetaFitnesses))
            plt.figure(figsize=(10,10))
            plt.plot(metaGenFitnesses)
            plt.savefig("train.png")
            plt.close()
            metaGenTestFitnesses.append(testMetaFitness)
            plt.figure(figsize=(10,10))
            plt.plot(metaGenTestFitnesses)
            plt.savefig("test.png")
            plt.close()

        #make new generation
        #generation forcing
        if useGenerationForcing:
            if(genNum > 1):
                if(statistics.mean(rawMetaFitnesses) < prevAvgFit):
                    #use prev generation
                    metaPopulation = prevPopulation
                    metaFitnesses = prevFitnesses
                    topGenome = prevTopGenome
                    print("Using Previous Generation")
                else:
                    prevFitnesses = metaFitnesses
                    prevPopulation = metaPopulation
                    prevTopGenome = topGenome
                    prevAvgFit = statistics.mean(rawMetaFitnesses)
                    print("Using Current Generation")
            else:
                prevFitnesses = metaFitnesses
                prevPopulation = metaPopulation
                prevTopGenome = topGenome
                prevAvgFit = statistics.mean(rawMetaFitnesses)

        newMetaPopulation = [copy.deepcopy(topGenome)]
        for i in range(len(metaPopulation) - 1):
            if useCrossover:
                #crossover last generation
                tempPop = copy.deepcopy(metaPopulation)
                tempFit = copy.deepcopy(metaFitnesses)
                choices = [0,0]
                choices[0] = np.random.choice(len(tempPop), 1, p=softmax(copy.deepcopy(tempFit)), replace=True)[0]
                tempPop.pop(choices[0])
                tempFit.pop(choices[0])
                choices[1] = np.random.choice(len(tempPop), 1, p=softmax(copy.deepcopy(tempFit)), replace=True)[0]
                newMetaPopulation.append(metaCrossover(copy.deepcopy(metaPopulation)[choices[0]], copy.deepcopy(metaPopulation)[choices[1]], 0.25))
            else:
                #mutate top genome
                newMetaPopulation.append(metaMutation(copy.deepcopy(topGenome), 0.25))

        metaPopulation = newMetaPopulation

        if sendEmails:
            body = """\
            Generation {gen2} has finished at {time}. On to the next! Gen Stats: {genstats}""".format(gen1=str(genNum + 1), gen2=str(genNum + 1), time=str(datetime.datetime.now()), genstats = printString)

            CommCore.sendEmail(subject="Generation Finished!", body=body)
    def _reset_sim(self):

        #########################
        if self.episode > 0:
            self.success_rate = float(
                numpy.sum(self.results) / float(len(self.results)))
            print("Episode: {} Success Rate: {} ".format(
                self.episode, self.success_rate))
            if len(self.results) < 10:
                self.results.append(0)
            else:
                self.results.pop(0)
        #########################

        self.episode += 1

        if self.vary == True:
            deviation_x = numpy.random.uniform(-self.init_vary_range,
                                               self.init_vary_range)
            deviation_q = self.get_dq(deviation_x)
        else:
            deviation_q = numpy.array([0, 0, 0, 0, 0, 0])

        #if self.ft_drift:
        #    self.ft_drift_val = numpy.random.uniform(-self.ft_drift_range, self.ft_drift_range)
        #self.pos_drift_val = numpy.random.uniform(-self.pos_drift_range, self.pos_drift_range)

        self.ft_dr_cor_noise = numpy.concatenate([
            numpy.random.uniform(-self.f_var_dr_cor, self.f_var_dr_cor, 3),
            numpy.random.uniform(-self.t_var_dr_cor, self.t_var_dr_cor, 3)
        ])

        self.pos_dr_cor_noise = numpy.uniform(-self.pos_var_dr_cor,
                                              self.pos_var_dr_cor, 3)
        self.rot_dr_cor_noise = numpy.uniform(
            -self.rot_var_dr_cor, self.rot_var_dr_cor, 3) * (numpy.pi / 180)

        del self.sim
        self.offset = randomize.randomize_ur10_xml(worker_id=self.worker_id,
                                                   **self.randomize_kwargs)
        model = mujoco_py.load_model_from_path(self.model_path)
        self.sim = mujoco_py.MjSim(model, nsubsteps=self.n_substeps)
        self.goal = self._sample_goal()
        if not self.viewer is None:
            self._get_viewer('human').update_sim(self.sim)
        #self._get_viewer('human')._ncam = self.sim.model.ncam

        self.ctrl_buffer = np.repeat(
            (self.initial_qpos + deviation_q).reshape(1, 6), 4, axis=0)
        self.b, self.a = butter(2, 0.12)
        self.zi = [
            lfilter_zi(self.b, self.a) *
            (self.initial_qpos[i] + deviation_q[i]) for i in range(6)
        ]
        self.qi_diff = 0
        self.last_target_q = self.initial_qpos + deviation_q

        self.set_state(self.initial_qpos + deviation_q)
        self.sim.forward()

        self.sim.step()

        if self.sim.data.ncon == 0:
            for i in range(100):
                self.sim.data.ctrl[:] = self.sim.data.qfrc_bias.copy()
                self.sim.step()
            self.init_x = numpy.concatenate(
                (self.sim.data.get_body_xpos("gripper_dummy_heg"),
                 self.sim.data.get_body_xquat("gripper_dummy_heg")))
            self.set_force_for_q(self.initial_qpos + deviation_q)

        return self.sim.data.ncon == 0
Пример #18
0
 def reset(self, stdv=None):
     if not stdv:
         stdv = 1. / np.sqrt(self.weight.shape[2])
     self.weight = np.uniform(-stdv, stdv, self.weight.shape)
     self.bias = np.uniform(-stdv, stdv, self.weight.shape[0])
def sample_bounded(bounds):
    d = len(bounds)
    x = np.zeros(shape=(d))
    for i in range(d):
        x[i] = np.uniform(bounds[i][0], bounds[i][1])
    return x
Пример #20
0
def random_site(seq_length, model_length):
    return Site(start=numpy.randint(seq_length - model_length),
                rev_comp=numpy.uniform() > 0.5,
                length=model_length)
Пример #21
0
 def reset(self, stdv=None):
     if not stdv:
         stdv = 1./np.sqrt(self.weight.shape[2])
     self.weight = np.uniform(-stdv, stdv, self.weight.shape)
     self.bias = np.uniform(-stdv, stdv, self.weight.shape[0])
Пример #22
0
def general_sampling(network, net_cls, dataset, args=None):  # pylint: disable=R0914,R0912,R0915
    """Query-by-committee (pool): disagreement between random inits of net."""
    # Batch-mode settings
    batch_size = int(dataset.online_len() / args.num_batches)
    label_batch_size = int(args.sample_prop * batch_size)
    labeled_ptrs = np.array([], dtype=np.int32)

    # Initialize committee
    committee = [network]
    if args.sampling_strategy in ["qbc"]:
        for i in range(args.vs_size - 1):
            this_network = net_cls(args.num_cls).to(args.device)
            this_network.train()
            train(this_network,
                  dataset,
                  epochs=args.initial_epochs,
                  args=args,
                  milestones=LONG_MILESTONES)
            this_network.eval()
            committee.append(this_network)

    # Map pointers to label
    if args.diversify == "cheat":
        ys = []
        for i in dataset.indices(split="online"):
            y, _ = dataset.train_labels[i]
            ys.append(y)
        ys = np.array(ys, dtype=np.int32)

    # Begin batch-mode sampling
    for batch_i in range(1, args.num_batches + 1):
        stats = []  # Smaller stats means higher priority
        sep_stats = []  # Bigger value means more important
        with torch.no_grad():
            for image, y, _ in dataset.iterate(
                    batch_size=args.infer_batch_size,
                    shuffle=False,
                    split="online"):
                image = image.to(args.device)

                # Aggregate domain sep
                if args.domainsep:
                    this_network = committee[0]
                    this_network.eval()
                    output = torch.exp(this_network(image))
                    p = output.cpu().data.numpy()
                    sep_stats.append(np.sum(-p * np.log(p + 1e-9), axis=1))

                # Produce stats depending on algorithm
                if args.sampling_strategy == "qbc":
                    predictions = []
                    for this_network in committee:
                        this_network.eval()
                        output = torch.exp(this_network(image))
                        p = output.cpu().data.numpy()
                        if not args.train_iw and not args.only_rlls_infer:
                            p = p * dataset.label_weights
                            p = p / np.sum(p, axis=1)[:, None]
                        predictions.append(np.argmax(p, axis=1))
                    predictions = np.stack(predictions)
                    stats.append(mode(predictions, axis=0).count[0])
                if args.sampling_strategy == "bald":
                    predictions = []
                    this_network = committee[0]
                    this_network.train()
                    with torch.no_grad():
                        for _ in range(args.bald_size):
                            output = torch.exp(this_network(image))
                            p = output.cpu().data.numpy()
                            if not args.train_iw and not args.only_rlls_infer:
                                p = p * dataset.label_weights
                                p = p / np.sum(p, axis=1)[:, None]
                            predictions.append(np.argmax(p, axis=1))
                    predictions = np.stack(predictions)
                    stats.append(mode(predictions, axis=0).count[0])
                if args.sampling_strategy == "cheat":
                    this_network = committee[0]
                    this_network.eval()
                    output = torch.exp(this_network(image))
                    p = output.cpu().data.numpy()
                    if not args.train_iw and not args.only_rlls_infer:
                        p = p * dataset.label_weights
                        p = p / np.sum(p, axis=1)[:, None]
                    stats.append(np.equal(np.argmax(p, axis=1), y))
                if args.sampling_strategy == "margin":
                    margin = []
                    this_network = committee[0]
                    this_network.eval()
                    output = torch.exp(this_network(image))
                    p = output.cpu().data.numpy()
                    if not args.train_iw and not args.only_rlls_infer:
                        p = p * dataset.label_weights
                        p = p / np.sum(p, axis=1)[:, None]
                    sorted_p = np.sort(p)
                    margin = sorted_p[:, -1] - sorted_p[:, -2]
                    stats.append(margin)
                if args.sampling_strategy == "maxent":
                    this_network = committee[0]
                    this_network.eval()
                    output = torch.exp(this_network(image))
                    p = output.cpu().data.numpy()
                    if not args.train_iw and not args.only_rlls_infer:
                        p = p * dataset.label_weights
                        p = p / np.sum(p, axis=1)[:, None]
                    stats.append(-np.sum(-p * np.log(p + 1e-9), axis=1))
                if args.sampling_strategy == "random":
                    stats.append(np.random.uniform(size=(len(image), )))

            if args.diversify in ["guess", "overguess"]:
                # Produce new ys
                ys = []
                network.eval()
                for image, _, _ in dataset.iterate(
                        batch_size=args.infer_batch_size,
                        shuffle=False,
                        split="online"):
                    image = image.to(args.device)
                    output = torch.exp(network(image))
                    p = output.cpu().data.numpy()
                    if not args.train_iw and not args.only_rlls_infer:
                        p = p * dataset.label_weights
                        p = p / np.sum(p, axis=1)[:, None]
                    ys.append(np.argmax(p, axis=1))
                ys = np.concatenate(ys)

        # Concatenate stats
        stats = np.concatenate(stats)

        if args.domainsep:
            sep_stats = np.concatenate(sep_stats)
            sep_stats[sep_stats < 0.5] = 0
            sep_odds = np.uniform(size=sep_stats.shape)
            selection = np.greater(sep_odds, np.uniform(size=sep_stats.shape))
            stats[~selection] = np.infty

        # Stack stats
        new_ptrs = np.setdiff1d(np.arange(len(stats)), labeled_ptrs)
        sorted_ptrs = new_ptrs[np.argsort(stats[new_ptrs])]

        if args.diversify == "none":
            labeled_ptrs = np.concatenate(
                [labeled_ptrs, sorted_ptrs[:label_batch_size]])
        elif args.diversify == "guess":
            # Take top examples from each label
            sorted_ptrs_by_label = {y: [] for y in range(args.num_cls)}
            for ptr in sorted_ptrs:
                sorted_ptrs_by_label[ys[ptr]].append(ptr)

            # Of remaining ptrs per label, find most equal allocation
            label_lens = sorted(
                [len(x) for x in sorted_ptrs_by_label.values()])
            for i, l in enumerate(label_lens):
                size = math.ceil((label_batch_size - sum(label_lens[:i])) /
                                 len(label_lens[i:]))
                if size <= l:
                    break
                size = -1
            if size == -1:
                raise ValueError()

            # Label pts per each
            for k, ptrs in sorted_ptrs_by_label.items():
                labeled_ptrs = np.concatenate([labeled_ptrs, ptrs[:size]])
            assert len(np.unique(labeled_ptrs)) == len(labeled_ptrs)
        elif args.diversify == "overguess":
            # Take top examples from each label
            sorted_ptrs_by_label = {y: [] for y in range(args.num_cls)}
            for ptr in sorted_ptrs:
                sorted_ptrs_by_label[ys[ptr]].append(ptr)

            # Label pts per each
            for k, ptrs in sorted_ptrs_by_label.items():
                size = math.ceil(dataset.label_weights[k] /
                                 sum(dataset.label_weights) * label_batch_size)
                labeled_ptrs = np.concatenate([labeled_ptrs, ptrs[:size]])
            assert len(np.unique(labeled_ptrs)) == len(labeled_ptrs)

        dataset.label_ptrs(labeled_ptrs)

        # Note sample proportion
        print("Sample proportion: ", len(labeled_ptrs) / dataset.online_len())

        # Train networks on current batch status
        if args.domainsep:
            committee[0].train()
            sep_train(committee[0],
                      dataset,
                      epochs=args.partial_epochs,
                      lr=args.finetune_lr,
                      args=args)
            committee[0].eval()

        for this_network in committee:
            this_network.train()
            train(this_network,
                  dataset,
                  epochs=args.partial_epochs,
                  lr=args.finetune_lr,
                  args=args)
            this_network.eval()

        # Handle reweighting procedure
        if args.iterative_iw:
            label_shift(committee[0], dataset, args)

        yield committee[0]
Пример #23
0
 def make (period, util, deadline = None):
 	return arbitrary (period * util, period, np.uniform(1,2) * period)