Exemplo 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
Exemplo n.º 2
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