Ejemplo n.º 1
0
def Predict(max_step, prefix):

    edf.components = []

    T = max_step
    h = edf.Value(np.zeros((1, hidden_dim)))
    c = edf.Value(np.zeros((1, hidden_dim)))

    prediction = []

    for t in range(T):

        if t < len(prefix):
            pred = edf.Value(prefix[t])
            prediction.append(pred)
        else:
            prediction.append(pred)

        wordvec = edf.Embed(pred, C2V)
        xt = edf.Reshape(wordvec, [-1, hidden_dim])
        h_next, c_next = LSTMCell(xt, h, c)
        p = edf.SoftMax(edf.VDot(h_next, V))
        pred = edf.ArgMax(p)
        h = h_next
        c = c_next

    edf.Forward()

    idx = [pred.value for pred in prediction]
    stop_idx = utils.to_index('}')

    if stop_idx in idx:
        return idx[0:idx.index(stop_idx) + 1]
    else:
        return idx
Ejemplo n.º 2
0
def Predict(max_step, prefix, model):
    T = max_step
    prediction = []

    for t in range(T):

        if t < len(prefix):
            pred = prefix[t]
            prediction.append(pred)
        else:
            prediction.append(pred)

        hidden = model.init_hidden(1)
        inp = Variable(torch.LongTensor([[pred]]))
        if use_cuda:
            inp = inp.cuda()
        #print(inp)
        output, hidden = model(inp, hidden)
        output = output.view(n_vocab)
        #print(output)
        pred = output.data.max(0)[1][0]
        #print(pred)

    idx = [pred for pred in prediction]
    stop_idx = utils.to_index('}')

    if stop_idx in idx:
        return idx[0:idx.index(stop_idx) + 1]
    else:
        return idx
Ejemplo n.º 3
0
    def wrapper(*args, **kwargs):
        r"""
        Run view function if session contains username, otherwise redirect to
        index page.

        :param \*args: Positional arguments to pass to view function.
        :param \*\*kwargs: Keyword arguments to pass to view function.
        """
        if not g.username:
            return to_index()
        return func(*args, **kwargs)
Ejemplo n.º 4
0
    def wrapper(*args, **kwargs):
        r"""
        Run view function if session contains username, otherwise redirect to
        index page.

        :param \*args: Positional arguments to pass to view function.
        :param \*\*kwargs: Keyword arguments to pass to view function.
        """
        if not g.username:
            return to_index()
        return func(*args, **kwargs)
Ejemplo n.º 5
0
def Predict(max_step, prefix):

    edf.components = []

    T = max_step
    h = [[None] * layer] * (T + 1)
    c = [[None] * layer] * (T + 1)
    for i in range(layer):
        h[0][i] = edf.Value(np.zeros((1, hidden_dim)))
        c[0][i] = edf.Value(np.zeros((1, hidden_dim)))

    prediction = []

    for t in range(T):

        if t < len(prefix):
            pred = edf.Value(prefix[t])
            prediction.append(pred)
        else:
            prediction.append(pred)

        wordvec = edf.Embed(pred, C2V)
        xt = edf.Reshape(wordvec, [-1, hidden_dim])
        for i in range(layer):
            h[t + 1][i], c[t + 1][i] = LSTMCell(xt, h[t][i], c[t][i], i)
            xt = h[t + 1][i]
        p = edf.SoftMax(edf.VDot(xt, V))
        pred = edf.ArgMax(p)

    edf.Forward()

    idx = [pred.value for pred in prediction]
    stop_idx = utils.to_index('}')

    if stop_idx in idx:
        return idx[0:idx.index(stop_idx) + 1]
    else:
        return idx
Ejemplo n.º 6
0
def quit():
    """Clear username from session and quit from Comments app."""
    username_key = USERNAME_KEY.format(current_app.config['KEY_PREFIX'])
    session.pop(username_key, None)
    return to_index()