def define_stackrnn(LEN, DIM_IN, DIM_OUT):
    model = StackedRNN(
        timespan=LEN,
        input_dim=DIM_IN,
        output_dim=DIM_OUT,
        cells=[512, 512, 512],
    )
    model.build_generator('temp/e-comp_simple_rnn.h5')
    # model.build_generator('temp/temp.h5')
    return model
def define_stackrnn_hist(LEN, DIM_IN, DIM_OUT):
    model = StackedRNN(
        timespan=LEN,
        input_dim=DIM_IN,
        output_dim=DIM_OUT,
        cells=[512, 512, 512],
    )
    #   model.build_generator('temp/stackedrnn_hist.h5')
    model.build_generator('temp/stackedrnn_hist_patch_T=0.05.h5')
    return model
Exemple #3
0
def test_stackrnn(data, weight='temp/simple_rnn.h5'):
    model = StackedRNN(timespan=LEN,
                       input_dim=DIM,
                       output_dim=DIM,
                       cells=[512, 512, 512])
    model.build_generator(weight)

    moving_mean = []
    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])
        x = to_categorical(x.flatten(), num_classes=DIM).\
            reshape(x.shape[0], DIM)

        score = bleu(model=model,
                     prior=x,
                     prefix_length=PREFIX,
                     tokenize=tokenize)
        moving_mean.append(score)
        print np.mean(moving_mean)
Exemple #4
0
def test_emb_stackrnn(data, weight='temp/emb_stackrnn.h5'):
    emb_w = load_model('temp/emb.h5').layers[2].layers[1].get_weights()
    model = StackedRNN(timespan=LEN,
                       input_dim=DIM,
                       output_dim=DIM,
                       cells=[512, 512, 512],
                       embedding=Embedding(DIM, 512, trainable=False),
                       embedding_w=emb_w)
    model.build_generator(weight)

    moving_mean = []
    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])

        score = bleu(model=model,
                     prior=x,
                     prefix_length=PREFIX,
                     tokenize=tokenize)
        moving_mean.append(score)
        print np.mean(moving_mean)
                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])
            x = np.array(x)
            y = np.array(y)

            # sparse to complex
            x = to_categorical(x.flatten(), num_classes=DIM).\
                reshape(x.shape[0], x.shape[1], DIM)
            y = y.reshape(x.shape[0], x.shape[1], 1)
            yield (x, y)

    # model
    model = StackedRNN(
        timespan=LEN,
        input_dim=DIM,
        output_dim=DIM,
        cells=[512, 512, 512],
    )
    #                  block_kwargs={'kernel_regularizer': l2(1e-5)})
    model.build()
    #   model.model.load_weights('temp/stackedrnn_kernel_l2.h5')
    #   model.model.load_weights('temp/simple_rnn.h5')
    #   model.train(data_generator(),
    #               opt=1e-5,
    #               steps_per_epoch=30,
    #               epochs=100,
    #               save_path='temp/simple_rnn.h5')

    model.build_generator('temp/e-comp_simple_rnn.h5')
    res = model.generate(seed=32, length=2000)
                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])
            x = np.array(x)
            y = np.array(y)

            # sparse to complex
            y = y.reshape(x.shape[0], x.shape[1], 1)
            yield (x, y)

    # model
    emb_w = load_model('temp/emb.h5').layers[2].layers[1].get_weights()
    model = StackedRNN(timespan=LEN,
                       input_dim=DIM,
                       output_dim=DIM,
                       cells=[512, 512, 512],
                       embedding=Embedding(DIM, 512, trainable=False),
                       embedding_w=emb_w)
    model.build()
    model.model.load_weights('temp/emb_stackrnn.h5')

    model.train(data_generator(),
                opt=1e-5,
                steps_per_epoch=20,
                epochs=200,
                save_path='temp/emb_stackrnn.h5')

    res = model.generate('temp/emb_stackrnn.h5', seed=64, length=5000)
    mid = Song()
    track = mid.add_track()
    for msgi in AllInOneCoder().decode(res):
Exemple #7
0
            print '| Layer[{}]:'.format(ind), cell.max(), cell.min(),
        print

        # self.keyboard = self.keyboard*0.9
        note = note.argmax()
        if note <= 127:
            self.keyboard[0, note] = 1
        elif note <= 128 + 127:
            self.keyboard[0, note - 128] = 0
        self.imshows[-1].set_data(self.keyboard)
        self.canvas[-1].draw()


if __name__ == '__main__':
    model = StackedRNN(timespan=LEN,
                       input_dim=DIM,
                       output_dim=DIM,
                       cells=[512, 512, 512])
    model.build()
    # generator = model.build_generator('temp/simple_rnn.h5')
    generator = model.build_generator('temp/stackedrnn_act_l1.h5')
    lstm_layers = []
    for layer in generator.layers:
        if isinstance(layer, LSTM):
            lstm_layers.append(layer)
    print lstm_layers

    window = VisualizeWindow(model=model, lstm_layers=lstm_layers)
    window.connect("delete-event", Gtk.main_quit)
    window.show_all()
    Gtk.main()
Exemple #8
0
                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])
            x = np.array(x)
            y = np.array(y)

            # sparse to complex
            x = to_categorical(x.flatten(), num_classes=DIM).\
                reshape(x.shape[0], x.shape[1], DIM)
            y = y.reshape(x.shape[0], x.shape[1], 1)
            yield (x, y)

    # model
    model = StackedRNN(timespan=LEN,
                       input_dim=DIM,
                       output_dim=DIM,
                       cells=[128, 128, 128],)
    model.build()
    model.model.load_weights('temp/start_with_me.h5')
#   model.train(data_generator(),
#               opt=optimizers.Adam(1e-3),
#               steps_per_epoch=30,
#               epochs=300,
#               save_path='temp/start_with_me.h5')

    model.build_generator('temp/start_with_me.h5')
    res = model.generate(seed=32, length=2000, verbose=0)

    # store
    mid = Song()
    track = mid.add_track()
Exemple #9
0
                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
    model = StackedRNN(
        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 = [
                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
    model = StackedRNN(
        timespan=LEN,
        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)