Пример #1
0
    def BuildModel():

        edf.components = []

        B = inp.value.shape[0]
        T = inp.value.shape[1]
        h = edf.Value(np.zeros((B, hidden_dim)))
        c = edf.Value(np.zeros((B, hidden_dim)))

        score = []

        for t in range(T - 1):

            wordvec = edf.Embed(edf.Value(inp.value[:, t]), C2V)
            xt = edf.Reshape(wordvec, [-1, hidden_dim])
            h_next, c_next = LSTMCell(xt, h, c)
            p = edf.SoftMax(edf.VDot(h_next, V))
            logloss = edf.Reshape(
                edf.LogLoss(edf.Aref(p, edf.Value(inp.value[:, t + 1]))),
                (B, 1))

            if t == 0:
                loss = logloss
            else:
                loss = edf.ConCat(loss, logloss)

            score.append(p)
            h = h_next
            c = c_next

        masks = np.zeros((B, T - 1), dtype=np.int32)
        masks[inp.value[:, 1:] != 0] = 1
        loss = edf.MeanwithMask(loss, edf.Value(masks))

        return loss, score
Пример #2
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
Пример #3
0
def BuildModel():

    edf.components = []

    B = inp.value.shape[0]
    T = inp.value.shape[1]
    h = [[None] * layer] * T
    c = [[None] * layer] * T

    for i in range(layer):
        h[0][i] = edf.Value(np.zeros((B, hidden_dim)))
        c[0][i] = edf.Value(np.zeros((B, hidden_dim)))
    score = []

    for t in range(T - 1):

        wordvec = edf.Embed(edf.Value(inp.value[:, t]), 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))
        logloss = edf.Reshape(edf.LogLoss(edf.Aref(p, edf.Value(inp.value[:, t + 1]))), (B, 1))

        if t == 0:
            loss = logloss
        else:
            loss = edf.ConCat(loss, logloss)

        score.append(p)

    masks = np.zeros((B, T - 1), dtype=np.int32)
    masks[inp.value[:, 1:] != 0] = 1
    loss = edf.MeanwithMask(loss, edf.Value(masks))

    return loss, score
Пример #4
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
Пример #5
0
f1 = edf.Param(edf.xavier((3, 3, prev_channel, 32)))
b1 = edf.Param(np.zeros(32))

f3 = edf.Param(edf.xavier((3, 3, 32, 64)))
b3 = edf.Param(np.zeros(64))

f5 = edf.Param(edf.xavier((1, 1, 64, 10)))
b5 = edf.Param(np.zeros(10))

layer1 = edf.RELU(edf.Add(Conv(f1, inp, 1, 1), b1))
layer2 = MaxPool(layer1, 4)
layer3 = edf.RELU(edf.Add(Conv(f3, layer2), b3))
layer4 = AvePool(layer3, 6)
layer5 = edf.RELU(edf.Add(Conv(f5, layer4), b5))
pred = edf.Reshape(layer5, (bucket, 10))

# the standard classification layer, which you don't need to modify
pred = edf.SoftMax(pred)
loss = edf.Mean(edf.LogLoss(edf.Aref(pred, lab)))
acc = edf.Accuracy(pred, lab)

##################################################################################################################


def eval_train():

    # we only choose 1/5 of the train images for evaluation since evaluation the whole images is time consuming
    eval_imgs = t_imgs[::5]
    eval_labels = t_labels[::5]
    avg_acc = 0