Exemple #1
0
def info(ses, fn, t, prt, kind):

    class Opt:
        def __init__(self, t):
            self.after = t
            self.before = float('inf')
            self.overview = 'none'

    # xxx assumes this exists
    time_metric = util.join('serverStatus', 'localTime')

    # find and print the first sample after t (only)
    for metrics in read(ses, fn, Opt(t), progress=False):
        if kind=='raw':
            for sample, sample_time in enumerate(metrics[time_metric]):
                sample_time = sample_time / 1000.0
                if sample_time >= t:
                    break
            prt('%s at t=%.3f (%s)' % (fn, t, util.f2s(t)))
            util.print_sample(metrics, sample, prt)
            break
        elif kind=='metadata':
            prt('metadata at t=%.3f (%s)' % (t, util.f2s(t)))
            if metrics.metadata:
                util.print_bson_doc(metrics.metadata, prt, '    ')
            else:
                prt('    NOT AVAILABLE')
            break
Exemple #2
0
def info(ses, fn, t, prt, kind):
    class Opt:
        def __init__(self, t):
            self.after = t
            self.before = float('inf')
            self.overview = 'none'

    # xxx assumes this exists
    time_metric = util.join('serverStatus', 'localTime')

    # find and print the first sample after t (only)
    for metrics in read(ses, fn, Opt(t), progress=False):
        if kind == 'raw':
            for sample, sample_time in enumerate(metrics[time_metric]):
                sample_time = sample_time / 1000.0
                if sample_time >= t:
                    break
            prt('%s at t=%.3f (%s)' % (fn, t, util.f2s(t)))
            util.print_sample(metrics, sample, prt)
            break
        elif kind == 'metadata':
            prt('metadata at t=%.3f (%s)' % (t, util.f2s(t)))
            if metrics.metadata:
                util.print_bson_doc(metrics.metadata, prt, '    ')
            else:
                prt('    NOT AVAILABLE')
            break
Exemple #3
0
def model(data,
          ix_to_char,
          char_to_ix,
          vocab_size,
          num_iterations=35000,
          n_a=50):
    """
    Trains the model and generates jokes.

    Arguments:
        data -- text corpus
        ix_to_char -- dictionary that maps the index to a character
        char_to_ix -- dictionary that maps a character to an index
        num_iterations -- number of iterations to train the model for
        n_a -- number of units of the RNN cell
        vocab_size -- number of unique characters in vocabulary

    Returns:
        parameters -- learned parameters
    """
    n_x, n_y = vocab_size, vocab_size
    parameters = initialize_parameters(n_a, n_x, n_y)
    loss = get_initial_loss(vocab_size, 10)

    # Shuffle list of all jokes
    np.random.seed(0)
    np.random.shuffle(data)

    # Initialize the hidden state
    a_prev = np.zeros((n_a, 1))

    # Optimization loop
    for j in range(num_iterations):
        index = j % len(data)
        X = [None] + [char_to_ix[ch] for ch in data[index]]
        Y = X[1:] + [char_to_ix["\n"]]

        curr_loss, gradients, a_prev = optimize(X,
                                                Y,
                                                a_prev,
                                                parameters,
                                                vocab_size,
                                                learning_rate=0.01)

        # Use a latency trick to keep the loss smooth.
        loss = smooth(loss, curr_loss)

        # Every 1000 iterations, generate n characters to check if the model is
        # learning properly
        if j % 1000 == 0:
            print('Iteration: %d, Loss: %f' % (j, loss) + '\n')
            for name in range(10):
                sampled_indices = sample(parameters, char_to_ix)
                print_sample(sampled_indices, ix_to_char)
            print('\n')

    return parameters
Exemple #4
0
 def info(cls, ses, fn, t, kind):
     if kind != 'raw':
         return
     def putln(*s):
         ses.put(' '.join(str(ss) for ss in s) + '\n')
     file = cls.get(fn)
     for chunk in file.chunks:
         if t >= chunk[file.time_key][0]:
             for sample, sample_time in enumerate(chunk[file.time_key]):
                 if sample_time >= t:
                     putln('%s at t=%.3f (%s)' % (fn, t, util.f2s(t)))
                     util.print_sample(chunk, sample, putln)
                     return
Exemple #5
0
    def info(cls, ses, fn, t, kind):
        if kind != 'raw':
            return

        def putln(*s):
            ses.put(' '.join(str(ss) for ss in s) + '\n')

        file = cls.get(fn)
        for chunk in file.chunks:
            if t >= chunk[file.time_key][0]:
                for sample, sample_time in enumerate(chunk[file.time_key]):
                    if sample_time >= t:
                        putln('%s at t=%.3f (%s)' % (fn, t, util.f2s(t)))
                        util.print_sample(chunk, sample, putln)
                        return