Ejemplo n.º 1
0
def init_forward(x_list, volatile=False):
    init_h1 = np.zeros((batch_size, hidden_size), dtype=dt)
    init_h2 = np.zeros((batch_size, hidden_size), dtype=dt)
    if args.gpu >= 0:
        init_h1 = cuda.to_gpu_async(init_h1)
        init_h2 = cuda.to_gpu_async(init_h2)
        x_list = cuda.to_gpu_async(x_list)
    prev_h1 = V(init_h1, volatile=volatile)
    prev_h2 = V(init_h2, volatile=volatile)
    return prev_h1, prev_h2, x_list
Ejemplo n.º 2
0
def init_forward(x_list, volatile=False):
    init_h1 = np.zeros((batch_size, hidden_size), dtype=dt)
    init_h2 = np.zeros((batch_size, hidden_size), dtype=dt)
    if args.gpu >= 0:
        init_h1 = cuda.to_gpu_async(init_h1)
        init_h2 = cuda.to_gpu_async(init_h2)
        x_list = cuda.to_gpu_async(x_list)
    prev_h1 = V(init_h1, volatile=volatile)
    prev_h2 = V(init_h2, volatile=volatile)
    return prev_h1, prev_h2, x_list
Ejemplo n.º 3
0
def forward(x_list, result, use_gpu=False):
    init_h = np.zeros((batch_size, hidden_size), dtype=dt)
    if use_gpu:
        init_h = cuda.to_gpu(init_h)
    prev_h = V(init_h)

    for x in x_list:
        if use_gpu:
            x = cuda.to_gpu_async(x)
        prev_h = forward_one_step(prev_h, x)

    if use_gpu:
        result = cuda.to_gpu_async(result)

    t = V(result)
    y = model.Wy(prev_h)
    loss = F.mean_squared_error(y, t)
    return loss, y
Ejemplo n.º 4
0
def forward(model, x_list, result, use_gpu=False):
    init_h = np.zeros((batch_size,hidden_size), dtype=dt)

    if use_gpu:
        x_list = cuda.to_gpu_async(x_list)
        result = cuda.to_gpu_async(result)
        init_h = cuda.to_gpu(init_h)

    prev_h = V(init_h)
    for i in xrange(0, 784):
        x = x_list[:,i]
        prev_h = forward_one_step(model, prev_h, x)

    t        = V(result)
    y        = model.Wy(prev_h)
    loss     = F.softmax_cross_entropy(y, t)
    accuracy = F.accuracy(y, t)
    return loss, accuracy
Ejemplo n.º 5
0
def forward(x_list, result, use_gpu=False):
    init_h = np.zeros((batch_size,hidden_size), dtype=dt)
    if use_gpu:
        init_h = cuda.to_gpu(init_h)
    prev_h = V(init_h)

    for x in x_list:
        if use_gpu:
            x = cuda.to_gpu_async(x)
        prev_h = forward_one_step(prev_h, x)

    if use_gpu:
        result = cuda.to_gpu_async(result)

    t = V(result)
    y = model.Wy(prev_h)
    loss = F.mean_squared_error(y, t)
    return loss, y
Ejemplo n.º 6
0
def predict(x_list, pred_num=10, volatile=False):
    prev_h1, prev_h2, x_list = init_forward(x_list)
    preds = list()
    pred = None
    for cur_word in x_list:
        prev_h1, prev_h2, new_loss, pred = forward_one_step(prev_h1, prev_h2, cur_word, cur_word, volatile=volatile)
    for i in xrange(0, pred_num):
        cur_word = np.array([pred])
        if args.gpu >= 0:
            cur_word = cuda.to_gpu_async(cur_word)
        prev_h1, prev_h2, new_loss, pred = forward_one_step(prev_h1, prev_h2, cur_word, cur_word, volatile=volatile)
        preds.append(pred)
    return preds
Ejemplo n.º 7
0
def predict(x_list, pred_num=10, volatile=False):
    prev_h1, prev_h2, x_list = init_forward(x_list)
    preds = list()
    pred = None
    for cur_word in x_list:
        prev_h1, prev_h2, new_loss, pred = forward_one_step(prev_h1,
                                                            prev_h2,
                                                            cur_word,
                                                            cur_word,
                                                            volatile=volatile)
    for i in xrange(0, pred_num):
        cur_word = np.array([pred])
        if args.gpu >= 0:
            cur_word = cuda.to_gpu_async(cur_word)
        prev_h1, prev_h2, new_loss, pred = forward_one_step(prev_h1,
                                                            prev_h2,
                                                            cur_word,
                                                            cur_word,
                                                            volatile=volatile)
        preds.append(pred)
    return preds
Ejemplo n.º 8
0
 def test_to_gpu_async_unavailable(self):
     x = numpy.array([1])
     if not cuda.available:
         with self.assertRaises(RuntimeError):
             cuda.to_gpu_async(x)
Ejemplo n.º 9
0
 def test_to_gpu_async_unavailable(self):
     x = numpy.array([1])
     if not cuda.available:
         with self.assertRaises(RuntimeError):
             cuda.to_gpu_async(x)