Beispiel #1
0
    def data_generator():
        batch_size = 32
        while True:
            x = []
            y = []
            h = []
            for _ in range(batch_size):
                ind = np.random.randint(len(data))
                start = np.random.randint(data[ind].shape[0] - LEN - 1)
                x.append(data[ind][start:start + LEN])
                y.append(data[ind][start + 1:start + LEN + 1])
                # global hist
                # h.append(hist[ind])
                # patch hist
                h_ = histogram_onehot(x[-1], coder.code_to_name, NOTE_NUMBER)
                h.append(norm(h_, H_TEMP))
            x = np.array(x)
            y = np.array(y)
            h = np.array(h)

            # sparse to complex
            x = to_categorical(x.flatten(), num_classes=DIM_IN).\
                reshape(x.shape[0], x.shape[1], DIM_IN)
            x[:, :, -len(NOTE_NUMBER):] = h[:, None, :]
            y = y.reshape(x.shape[0], x.shape[1], 1)
            yield (x, y)
        model = define_stackrnn(LEN, DIM_IN, DIM_OUT)

    D = []
    for _ in range(TEST):
        ind = RNG.randint(len(data))
        start = RNG.randint(data[ind].shape[0] - LEN - 1)
        x = np.array(data[ind][start:start + LEN])
        h_pre = histogram_onehot(x[:PREFIX], coder.code_to_name, NOTE_NUMBER)
        h_ans = histogram_onehot(x[-ANSWER:], coder.code_to_name, NOTE_NUMBER)
        x = to_categorical(x[:PREFIX], num_classes=DIM_IN).\
            reshape(PREFIX, DIM_IN)

        model.reset_generator()
        song = model.generate(
            seed=RNG.randint(1e+9),
            length=ANSWER,
            prefix=x[:PREFIX],
            verbose=0,
            addition=norm(h_ans, TEMPERATURE) if HIST else None)

        hist_res = histogram_onehot(song, coder.code_to_name, NOTE_NUMBER)
        d = JS_divergence(h_ans, hist_res)

        print h_ans
        print hist_res
        print d

        if d != np.inf:
            D.append(d)
        print np.mean(D)
Beispiel #3
0
        timespan=LEN,
        input_dim=DIM_IN,
        output_dim=DIM_OUT,
        cells=[512, 512, 512],
    )
    model.build()
    #   model.model.load_weights('temp/stackedrnn_hist.h5')
    #   model.train(data_generator(),
    #               opt=8e-4,
    #               steps_per_epoch=30,
    #               epochs=300,
    #               save_path='temp/stackedrnn_hist.h5')

    C_scale = np.array([2, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1])
    model.build_generator('temp/stackedrnn_hist.h5')
    res = model.generate(seed=64, length=3000, addition=norm(C_scale, ))

    events = [
        coder.code_to_name(note)
        for note in res[:, :DIM_IN - len(NOTE_NUMBER)].argmax(1)
    ]
    np.set_printoptions(precision=2)
    res_hist = histogram(events, NOTE_NUMBER)
    res_hist /= res_hist.sum()
    print res_hist
    print hist[0]

    mid = Song()
    track = mid.add_track()
    # the ``useful'' notes are more than usual
    # therefore use a smaller _MIDO_TIME_SCALE
        input_dim=DIM_IN,
        output_dim=DIM_OUT,
        cells=[512, 512, 512],
    )
    model.build()
    #   model.model.load_weights('temp/stackedrnn_hist_easy.h5')
    #   model.train(data_generator(),
    #               opt=1e-3,
    #               steps_per_epoch=30,
    #               epochs=300,
    #               save_path='temp/stackedrnn_hist_easy.h5')

    model.build_generator('temp/stackedrnn_hist_easy.h5')
    res = model.generate(seed=64,
                         length=3000,
                         addition=norm(hist[1], H_TEMP),
                         verbose=0)

    events = [
        coder.code_to_name(note)
        for note in res[:, :DIM_IN - len(NOTE_NUMBER)].argmax(1)
    ]
    np.set_printoptions(precision=2)
    res_hist = histogram(events, NOTE_NUMBER)
    res_hist /= res_hist.sum()
    print res_hist
    print hist[0]

    mid = Song()
    track = mid.add_track()
    # the ``useful'' notes are more than usual