Beispiel #1
0
def indices_to_data(indices, seqlength, num_chars):
    num_examples = len(indices) - seqlength
    x = np.zeros([num_examples, seqlength], dtype='float32')
    y = np.zeros([num_examples], dtype='int32')
    for example_num in range(0, len(indices) - seqlength):
        start = example_num
        end = start + seqlength
        x[example_num, :] = indices[start:end]
        y[example_num] = indices[end]
    return to_one_hot(x, num_chars), to_one_hot(y, num_chars)
Beispiel #2
0
def gen_sequence(model,
                 seqlength,
                 char_to_index,
                 index_to_char,
                 seed='The quick brown fox jumps',
                 num_chars=100):
    assert len(seed) >= seqlength
    samples = []
    indices = [char_to_index[c] for c in seed]
    data = np.zeros((1, seqlength))
    for i, index in enumerate(indices[:seqlength]):
        data[0, i] = index
    data = to_one_hot(data, len(char_to_index))

    for i in range(num_chars):
        # Pick the character that got assigned the highest probability
        preds = model(data)
        # ix = np.argmax(preds.ravel())
        # Alternatively, to sample from the distribution instead:
        ix = np.random.choice(np.arange(len(char_to_index)), p=preds.ravel())
        samples.append(ix)
        data[0, 0:seqlength - 1] = data[0, 1:]  # bump down
        data[0, seqlength - 1] = ix  # insert latest
    random_snippet = seed + ''.join(index_to_char[index] for index in samples)
    return random_snippet
Beispiel #3
0
def load_labels(data_dir, filename):
    path = os.path.join(data_dir, filename)
    if not os.path.exists(path):
        download(data_dir, filename)
    with gzip.open(path, 'rb') as f:
        data = np.frombuffer(f.read(), np.uint8, offset=8)
    return to_one_hot(data, 10)
Beispiel #4
0
def load_labels(data_dir, filename):
    path = os.path.join(data_dir, 'cifar-10-batches-py', filename)
    with open(path, 'rb') as f:
        labels = np.asarray(pk.load(f, encoding='latin1')['labels'])
        return to_one_hot(labels, 10)