예제 #1
0
def extractive_decode(list_of_sentences, sentence_labels, features, num_clusters, config):
    """Implements the 'encode' and 'decode' methods of the pipeline. density_parameter
        and minimum_samples are DBSCAN hyperparameters."""
        
    #sample sentences from each cluster
    candidates = sample(list_of_sentences, sentence_labels, features, num_clusters, config)
    return candidates
예제 #2
0
async def test_bernoulli_parallel(api_url: str) -> None:
    """Test sampling in parallel from Bernoulli model with defaults."""

    payload = {
        "function": "stan::services::sample::hmc_nuts_diag_e_adapt",
        "data": data
    }

    # launch `num_chains` sample operations in parallel
    num_chains = 3
    tasks_coros = [
        asyncio.ensure_future(helpers.sample(api_url, program_code, payload))
        for _ in range(num_chains)
    ]
    operations = [await coro for coro in tasks_coros]
    for operation in operations:
        fit_name = operation["result"]["name"]
        fit_url = f"{api_url}/{fit_name}"
        async with aiohttp.ClientSession() as session:
            async with session.get(fit_url) as resp:
                assert resp.status == 200
                assert resp.headers[
                    "Content-Type"] == "text/plain; charset=utf-8"
                theta = helpers.extract("theta", await resp.read())
                assert len(theta) == 1_000
예제 #3
0
def main():
    parser = argparse.ArgumentParser("Char-RNN on the complete works of Shakespeare")
    parser.add_argument("--test", type=bool, default=False,
            help = "if true, keep only a thousand lines from the Shakespeare corpus")

    args = parser.parse_args()

    seed(1616)
    
    text = extract_shakespeare_data("data/t8.shakespeare.txt")
    char_encoder = CharEncoder(text)
    #get sequences of 100 characters
    sequences = make_sequences(text)
    #vectorize with numeric labeling
    #each character gets mapped to an integer & vice versa
    sequences = char_encoder.label_sequences(sequences)
    if args.test:
        print("Test: downsizing data to 1,000 sequences...")
        sequences = sequences[:1000]

    shuffle(sequences)
    n_training_sequences = int(.9 * len(sequences))
    #split the dataset into training and validation sets
    training = sequences[:n_training_sequences]
    validation = sequences[n_training_sequences:]

    hidden_size = 128 
    rnn = CharRNN(char_encoder.n_chars, hidden_size)
    train(rnn, training, validation, epochs = 4, lr = 0.01, evaluate_per = 2, batch_size = 20)
    
    print(sample(rnn, prime_str = "Macbeth", size = 100, encoder = char_encoder,
            temperature=.9))
예제 #4
0
def main():

    input_dir = "../data/test"
    training_val_split = 0.7
    #defines, in Hz, the smallest timestep preserved in quantizing MIDIs
    #determines number of timeshift events
    sampling_rate = 125
    #determines number of velocity events
    n_velocity_bins = 32

    #set up data pipeline
    seq_length = 128
    padded_length = 128
    pipeline = PreprocessingPipeline(input_dir=input_dir,
                                     stretch_factors=[0.975, 1, 1.025],
                                     split_size=30,
                                     sampling_rate=sampling_rate,
                                     n_velocity_bins=n_velocity_bins,
                                     transpositions=range(-2, 3),
                                     training_val_split=training_val_split,
                                     max_encoded_length=seq_length + 1,
                                     min_encoded_length=33)

    pipeline.run()
    training_sequences = pipeline.encoded_sequences['training'][:1000]
    validation_sequences = pipeline.encoded_sequences['validation'][:100]
    n_tokens = 256 + sampling_rate + n_velocity_bins
    batch_size = 10
    optim = "adam"
    transformer = MusicTransformer(n_tokens,
                                   seq_length=padded_length,
                                   d_model=4,
                                   d_feedforward=32,
                                   n_heads=4,
                                   positional_encoding=True,
                                   relative_pos=True)

    train(transformer,
          training_sequences,
          validation_sequences,
          epochs=2,
          evaluate_per=1,
          batch_size=batch_size,
          batches_per_print=20,
          padding_index=0,
          checkpoint_path="../saved_models/test_save",
          custom_schedule=True)

    print(sample(transformer, 10))
예제 #5
0
 def cluster(encodings, sentences, config):
     if False:
         sentence_labels, num_clusters = find_clusters(encodings, config)
         candidate_sentences = sample(sentences, sentence_labels, encodings, 
                                      num_clusters, config)
         return candidate_sentences
     else:
         sentence_labels, _ = find_clusters(encodings, config)
         means = []
         for cluster in set(sentence_labels):
             if cluster == -1:
                 continue
             cluster_indices = np.where(sentence_labels == cluster)
             cluster_core_samples = encodings[cluster_indices]
             average = np.mean(cluster_core_samples, axis = 0)
             means.append(average)
         return means
예제 #6
0
 def update_function_points(self, xylimits):
     x_min, x_max, y_min, y_max = xylimits
     x_initial = np.linspace(x_min, x_max, self.resolution)
     try:
         x, y_arr = sample(self.np_expr, x_initial)
         y = y_arr[0]
     except IndexError:
         # if f(x)=a, make sure that y is an array with the
         # same size as x and with a constant value.
         debug('This looks like a constant function: ' +
               self.np_expr)
         self.constant = True
         # 2 graph points are enough for a constant function
         x = np.array([x_min, x_max])
         this_y = eval(self.np_expr)
         y = np.array([this_y, this_y])
     except Exception, e:
         debug('Exception caught.' +
               'This should not have happened here: ' + e)
         return False
예제 #7
0
def main():
    parser = argparse.ArgumentParser(
        "Script to generate MIDI tracks by sampling from a trained model.")

    parser.add_argument("--model_name",
                        type=str,
                        help="name of the model in saved_models/ folder.")
    parser.add_argument("--sample_length",
                        type=int,
                        default=512,
                        help="number of events to generate")
    parser.add_argument(
        "--temps",
        nargs="+",
        type=float,
        default=[1.0],
        help="space-separated list of temperatures to use when sampling")
    parser.add_argument(
        "--n_trials",
        type=int,
        default=3,
        help="number of MIDI samples to generate per experiment")
    parser.add_argument(
        "--live_input",
        action='store_true',
        default=False,
        help="if true, take in a seed from a MIDI input controller")

    parser.add_argument("--play_live",
                        action='store_true',
                        default=False,
                        help="play sample(s) at end of script if true")
    parser.add_argument("--keep_ghosts", action='store_true', default=False)
    parser.add_argument("--stuck_note_duration", type=int, default=0)

    args = parser.parse_args()

    model_name = args.model_name

    # try:
    #     model_dict = yaml.safe_load(open('saved_models/model.yaml'))[model_key]
    # except:
    #     raise GeneratorError(f"could not find yaml information for key {model_key}")
    #
    # model_path = model_dict["path"]
    # model_args = model_dict["args"]
    model_path = 'saved_models/' + model_name
    try:
        model = torch.load(model_path)
    except RuntimeError:
        model = torch.load(model_path, map_location="cpu")

    n_velocity_events = 32
    n_time_shift_events = 125

    decoder = SequenceEncoder(n_time_shift_events,
                              n_velocity_events,
                              min_events=0)

    if args.live_input:
        print("Expecting a midi input...")
        note_sequence = midi_input.read(n_velocity_events, n_time_shift_events)
        prime_sequence = decoder.encode_sequences([note_sequence])[0]

    else:
        prime_sequence = []

    # model = MusicTransformer(**model_args)
    # model.load_state_dict(state, strict=False)

    temps = args.temps

    trial_key = str(uuid.uuid4())[:6]
    n_trials = args.n_trials

    keep_ghosts = args.keep_ghosts
    stuck_note_duration = None if args.stuck_note_duration == 0 else args.stuck_note_duration

    for temp in temps:
        print(f"sampling temp={temp}")
        note_sequence = []
        for i in range(n_trials):
            print("generating sequence")
            output_sequence = sample(model,
                                     prime_sequence=prime_sequence,
                                     sample_length=args.sample_length,
                                     temperature=temp)
            note_sequence = decoder.decode_sequence(output_sequence,
                                                    verbose=True,
                                                    stuck_note_duration=None)

            output_dir = f"output/{model_name}/{trial_key}/"
            file_name = f"sample{i+1}_{temp}"
            write_midi(note_sequence, output_dir, file_name)

    for temp in temps:
        try:
            subprocess.run([
                'timidity',
                f"output/{model_name}/{trial_key}/sample{i+1}_{temp}.midi"
            ])
        except KeyboardInterrupt:
            continue
예제 #8
0
def main():
    parser = argparse.ArgumentParser("Script to generate MIDI tracks by sampling from a trained model.")

    parser.add_argument("--model", type=str, 
            help="Key in saved_models/model.yaml, helps look up model arguments and path to saved checkpoint.")
    parser.add_argument("--sample_length", type=int, default=512,
            help="number of events to generate")
    parser.add_argument("--temps", nargs="+", type=float, 
            default=[1.0],
            help="space-separated list of temperatures to use when sampling")
    parser.add_argument("--n_trials", type=int, default=3,
            help="number of MIDI samples to generate per experiment")
    parser.add_argument("--live_input", action='store_true', default = False,
            help="if true, take in a seed from a MIDI input controller")

    parser.add_argument("--play_live", action='store_true', default=False,
            help="play sample(s) at end of script if true")
    parser.add_argument("--keep_ghosts", action='store_true', default=True)
    parser.add_argument("--stuck_note_duration", type=int, default=1)

    args=parser.parse_args()

    model = args.model
    '''
    try:
        model_dict = yaml.safe_load(open('saved_models/model.yaml'))[model_key]
    except:
        raise GeneratorError(f"could not find yaml information for key {model_key}")
    '''
    #model_path = model_dict["path"]
    #model_args = model_dict["args"]

    #Change the value here to the model you want to run
    model_path = 'saved_models/'+model

    try:
        state = torch.load(model_path)
    except RuntimeError:
        state = torch.load(model_path, map_location="cpu")
    
    n_velocity_events = 32
    n_time_shift_events = 125

    decoder = SequenceEncoder(n_time_shift_events, n_velocity_events,
           min_events=0)

    if args.live_input:
        pretty_midis = []
        m = 'twinkle.midi'
        with open(m, "rb") as f:
                try:
                    midi_str = six.BytesIO(f.read())
                    pretty_midis.append(pretty_midi.PrettyMIDI(midi_str))
                    #print("Successfully parsed {}".format(m))
                except:
                    print("Could not parse {}".format(m))
        pipeline = PreprocessingPipeline(input_dir="data")
        note_sequence = pipeline.get_note_sequences(pretty_midis)
        note_sequence = [vectorize(ns) for ns in note_sequence]
        prime_sequence = decoder.encode_sequences(note_sequence)
        prime_sequence = prime_sequence[1:6]


    else:
        prime_sequence = []

   #model = MusicTransformer(**model_args)
    model = MusicTransformer(256+125+32, 1024, 
            d_model = 64, n_heads = 8, d_feedforward=256, 
            depth = 4, positional_encoding=True, relative_pos=True)

    model.load_state_dict(state, strict=False)

    temps = args.temps

    trial_key = str(uuid.uuid4())[:6]
    n_trials = args.n_trials

    keep_ghosts = args.keep_ghosts
    stuck_note_duration = None if args.stuck_note_duration == 0 else args.stuck_note_duration

    for temp in temps:
        print(f"sampling temp={temp}")
        note_sequence = []
        for i in range(n_trials):
            print("generating sequence")
            output_sequence = sample(model, prime_sequence = prime_sequence, sample_length=args.sample_length, temperature=temp)
            note_sequence = decoder.decode_sequence(output_sequence, 
                verbose=True, stuck_note_duration=0.5, keep_ghosts=True)

            output_dir = f"output/midis/{trial_key}/"
            file_name = f"sample{i+1}_{temp}"
            write_midi(note_sequence, output_dir, file_name)
    '''
예제 #9
0
def main():
    parser = argparse.ArgumentParser(
        "Script to generate MIDI tracks by sampling from a trained model.")

    parser.add_argument(
        "--model_key",
        type=str,
        help=
        "Key in saved_models/model.yaml, helps look up model arguments and path to saved checkpoint."
    )
    parser.add_argument("--sample_length",
                        type=int,
                        default=512,
                        help="number of events to generate")
    parser.add_argument(
        "--temps",
        nargs="+",
        type=float,
        default=[1.0],
        help="space-separated list of temperatures to use when sampling")
    parser.add_argument(
        "--n_trials",
        type=int,
        default=3,
        help="number of MIDI samples to generate per experiment")
    parser.add_argument("--primer",
                        type=str,
                        default=None,
                        help="Path to the primer")

    parser.add_argument("--play_live",
                        action='store_true',
                        default=False,
                        help="play sample(s) at end of script if true")
    parser.add_argument("--keep_ghosts", action='store_true', default=False)
    parser.add_argument("--stuck_note_duration", type=int, default=0)

    args = parser.parse_args()

    model_key = args.model_key

    try:
        model_dict = yaml.safe_load(open('saved_models/model.yaml'))[model_key]
    except:
        raise GeneratorError(
            f"could not find yaml information for key {model_key}")

    model_path = model_dict["path"]
    model_args = model_dict["args"]
    try:
        state = torch.load(model_path)
    except RuntimeError:
        state = torch.load(model_path, map_location="cpu")

    n_velocity_events = 32
    n_time_shift_events = 125

    decoder = SequenceEncoder(n_time_shift_events,
                              n_velocity_events,
                              min_events=0)

    if args.primer:
        # Read midi primer
        midi_str = six.BytesIO(open(args.primer, 'rb').read())
        p = pretty_midi.PrettyMIDI(midi_str)
        piano_data = p.instruments[0]

        notes = apply_sustain(piano_data)
        note_sequence = sorted(notes, key=lambda x: (x.start, x.pitch))
        ns = vectorize(note_sequence)

        prime_sequence = decoder.encode_sequences([ns])[0]
    else:
        prime_sequence = []

    model = MusicTransformer(**model_args)
    model.load_state_dict(state, strict=False)

    temps = args.temps

    trial_key = str(uuid.uuid4())[:6]
    n_trials = args.n_trials

    keep_ghosts = args.keep_ghosts
    stuck_note_duration = None if args.stuck_note_duration == 0 else args.stuck_note_duration

    for temp in temps:
        print(f"sampling temp={temp}")
        note_sequence = []
        for i in range(n_trials):
            print("generating sequence")
            output_sequence = sample(model,
                                     prime_sequence=prime_sequence,
                                     sample_length=args.sample_length,
                                     temperature=temp)
            note_sequence = decoder.decode_sequence(output_sequence,
                                                    verbose=True,
                                                    stuck_note_duration=None)

            output_dir = f"output/{model_key}/{trial_key}/"
            file_name = f"sample{i+1}_{temp}"
            write_midi(note_sequence, output_dir, file_name)