예제 #1
0
파일: app.py 프로젝트: nol-alb/1e0a
def learn():
    pattern = session['pattern']
    cntr = l4.index(pattern)
    cntr = cntr + 1
    cycle = session['cycle']
    name = session['username']
    pattern = str_int_list(pattern)
    x, onsetgen, fs = patterngen(160, cycle, pattern)
    pattern = session['pattern']
    path = pro.audiowrite(x, fs, name, pattern)
    path = '../' + path
    outPath = os.path.join(path)
    if request.method == "POST":
        audiocheck = './static/Data/Users/' + name + '/recordings/audio' + str(
            pattern) + '.wav'
        isExist = os.path.exists(audiocheck)
        if (isExist == False):
            return render_template('StartLearning.html',
                                   songout=outPath,
                                   name=name,
                                   pattern=pattern,
                                   isExist=isExist)

        pattern = str_int_list(pattern)
        averagebeat, averagecycle, n = pro.errordet(audiocheck, fs, onsetgen,
                                                    pattern)

        pattern = session['pattern']
        j = pro.plotter(averagebeat, averagecycle, name, [str(pattern)])
        j = '../' + j
        session['plotpath'] = j
        return redirect(url_for('dashboard', n=n))
    return render_template('StartLearning.html',
                           songout=outPath,
                           name=name,
                           pattern=pattern,
                           cntr=cntr)
        desired_signal = np.matmul(wH, hdx) / np.sqrt(M)
        ampl[a, b] = abs(desired_signal)
    print(a)

pl = ' (' + plaatsnaam[plek] + ')'
title = './result plots/' + '(' + datetime.datetime.now().strftime(
    "%Y-%m-%d_%H-%M") + ')-geo_model--M=' + str(M) + '_S=' + str(
        S) + '_dM=' + str(dM) + '_K=' + str(1) + '_resolution=' + str(
            resolution) + '_middel.html'
functions.plotter(ampl,
                  title,
                  header + 'mid',
                  pl,
                  resolution,
                  S,
                  M,
                  x_setup,
                  y_setup,
                  x,
                  y,
                  wH,
                  all_in=True)

UE_quad_x = [
    np.random.randint(int(len(x) / 2), len(x)),
    np.random.randint(0, int(len(x) / 2)),
    np.random.randint(0, int(len(x) / 2)),
    np.random.randint(int(len(x) / 2), len(x))
]
UE_quad_y = [
    np.random.randint(int(len(y) / 2), len(y)),
예제 #3
0
def training(model: torch.nn.Module,
             batch_size,
             epochs,
             loss_function,
             initial_lr,
             save_dir=None,
             model_dir=None,
             data_path=None,
             gradients=False,
             testing=False):

    # =============================================================================
    #   Setup the model for training on the GPU
    # =============================================================================

    model = model.to(device).to(torch.float)

    num_cores = 14
    pool = mp.Pool(num_cores)

    # =============================================================================
    #   Load in the WISDOM data if needed for training
    # =============================================================================

    if data_path is not None:
        Wmom0s, Wmom1s, Wpos, cdelts, sizes, vcircs, barolo = load(
            data_path=data_path)
        Wmom0s = Wmom0s.to(device).to(torch.float)
        Wmom1s = Wmom1s.to(device).to(torch.float)
        Wpos = Wpos.to(device).to(torch.float)

    for epoch in range(epochs):

        model.train(True)

        running_loss = []

        print("Epoch {0} of {1}".format((epoch + 1), epochs))
        optim = torch.optim.Adam(model.parameters(),
                                 learning_rate(initial_lr, epoch))

        # =============================================================================
        #       (Optional) push through the WISDOM data every 5th epoch
        # =============================================================================

        if (data_path is not None) and (epoch % 5 == 0):

            print('\n', '=' * 73,
                  "\n [>>>   Pushing REAL data through the network   <<<]",
                  '\n', '=' * 73)

            for _ in tqdm(range(100)):

                prediction1, prediction2, inc, pos, vmax = model(
                    Wmom0s, Wmom1s, Wpos)
                loss1 = loss_function(prediction1, Wmom0s)
                loss2 = loss_function(prediction2, Wmom1s)
                loss = loss1 + loss2
                prediction1.retain_grad()
                prediction2.retain_grad()
                optim.zero_grad()
                loss.backward()
                optim.step()
                running_loss.append(loss.detach().cpu())

# =============================================================================
#       Create the mini batches for pushing through the network
# =============================================================================

        else:

            index = np.arange(0, batch_size, 1)
            results = list(pool.imap(return_cube, index))
            batch = np.array([r[0] for r in results])
            targets = np.array([r[1:] for r in results])

            batch = torch.tensor(batch)
            batch[batch != batch] = 0
            targets = torch.tensor(targets)

            mom0s, mom1s = batch[:, 0, :, :].unsqueeze(
                1), batch[:, 1, :, :].unsqueeze(1)  # Resize for B,C,H,W format

            targets = targets.squeeze(1).squeeze(-1)

            mom0s = mom0s.to(device).to(torch.float)  # Cast inputs to GPU
            mom1s = mom1s.to(device).to(torch.float)

            targets = targets.to(device).to(torch.float)

            pos_t = targets[:, 0]

            for _ in tqdm(range(100)):

                prediction1, prediction2, inc, pos, vmax = model(
                    mom0s, mom1s, pos_t)
                loss1 = loss_function(prediction1, mom0s)
                loss2 = loss_function(prediction2, mom1s)
                loss = loss1 + loss2
                prediction1.retain_grad()
                prediction2.retain_grad()
                optim.zero_grad()
                loss.backward()
                optim.step()
                running_loss.append(loss.detach().cpu())

            del batch
            del targets

# =============================================================================
#       Print the running average loss and spread
# =============================================================================

        print("\n Mean loss: %.6f" % np.mean(running_loss),
              "\t Loss std: %.6f" % np.std(running_loss),
              "\t Learning rate: %.6f:" % learning_rate(initial_lr, epoch))
        print('_' * 73)

        # =============================================================================
        #       (Optional) print the layer gradients
        # =============================================================================

        if gradients is True:
            layers = []
            av_grads = []
            for n, p in model.named_parameters():
                if (p.requires_grad) and ("bias" not in n):
                    layers.append(n)
                    av_grads.append(p.grad.abs().mean())
            for i in range(len(layers)):
                print(layers[i], ' grad: ', av_grads[i])

# =============================================================================
#       (Optional) save the model's state dictionary
# =============================================================================

        if (model_dir is not None) and (epoch == epochs - 1):
            torch.save(model.state_dict(),
                       model_dir + 'semantic_KinMS_ONLY.pt')

# =============================================================================
#       (Optional) create plots
# =============================================================================

        if (save_dir is not None) and (epoch == epochs - 1):
            plotter(prediction1, prediction2, mom0s, mom1s, inc, pos,
                    save_directory)

# =============================================================================
#       (Optional) test mode to check embeddings work and there are no bugs
# =============================================================================

        if (epoch == 0) and (testing == True):
            print('TARGETS: ', targets)
            model.train(False)
            encodings = model.test_encode(batch)
            print('ENCODINGS: ', encodings)
                    dist = distances_a[m][s] + dist_b
                    pathloss = 1 / (16 * np.pi**2 * dist_b * distances_a[m][s])
                    phaseshift = 2 * np.pi * ((dist % 1) + random_phases[s])
                    contribution.append(pathloss * np.exp(1j * phaseshift))
                hdx.append(sum(contribution))
            desired_signal = np.matmul(wH, hdx) / np.sqrt(M)
            ampl[a, b] = abs(desired_signal)
        print(str(r) + '-' + str(a))
    title = './result plots/' + '(' + datetime.datetime.now().strftime(
        "%Y-%m-%d_%H-%M") + ')-geo_model_gem--' + str(r + 1) + titel + '.html'
    functions.plotter(ampl,
                      title,
                      header,
                      pl,
                      resolution,
                      S,
                      M,
                      x_setup,
                      y_setup,
                      x,
                      y,
                      all_in=True)

    gem_ampli += ampl / aantal_runs
    title = './result plots/' + '(' + datetime.datetime.now().strftime(
        "%Y-%m-%d_%H-%M") + ')-geo_model_gem--g' + str(r + 1) + titel + '.html'
    functions.plotter(gem_ampli,
                      title,
                      header,
                      pl,
                      resolution,
                      S,
예제 #5
0
                phaseshift = 2 * np.pi * ((dist % 1))  #+ random_phases[s])
                contribution.append(pathloss * np.exp(-1j * phaseshift))
            hdx.append(sum(contribution))  #/(256 * np.pi ** 4))
        HDX.append(hdx)
        desired_signal = np.matmul(wH, hdx) / np.sqrt(
            M)  # pwr normalisation with sqrt(M)
        ampl[a, b] = abs(desired_signal)
    print(a)

title = './result plots/' + '(' + datetime.datetime.now().strftime(
    "%Y-%m-%d_%H-%M") + ')-geo_model--M=' + str(M) + '_S=' + str(
        S) + '_dM=' + str(dM) + '_K=' + str(1) + '_resolution=' + str(
            resolution) + comment + '.html'
UE_x = int(len(ampl) / 2)
UE_y = int(len(ampl[0]) / 2)
size_vars, size_gem, height_vars, height_gem = functions.plotter(ampl,
                                                                 title,
                                                                 header,
                                                                 resolution,
                                                                 S,
                                                                 M,
                                                                 x_setup,
                                                                 y_setup,
                                                                 UE_x,
                                                                 UE_y,
                                                                 x,
                                                                 y,
                                                                 wH,
                                                                 all_in=True)
# np.savetxt("8_500_400_0.1.csv", ampl, delimiter=',')