def train(place):
    num_layers = 1
    batch_size = 4
    hidden_size = 10
    num_steps = 3
    init_scale = 0.1
    max_epoch = 1
    dropout = 0.0
    vocab_size = 1000
    batch_num = 200
    with fluid.dygraph.guard(place):
        fluid.default_startup_program().random_seed = SEED
        fluid.default_main_program().random_seed = SEED
        ptb_model = PtbModel(hidden_size=hidden_size, vocab_size=vocab_size, num_layers=num_layers, num_steps=num_steps, init_scale=init_scale, dropout=dropout)
        sgd = SGDOptimizer(learning_rate=0.001, parameter_list=ptb_model.parameters())
        for epoch_id in range(max_epoch):
            total_loss = 0.0
            iters = 0.0
            total_sample = 0
            init_hidden_data = np.zeros((num_layers, batch_size, hidden_size), dtype='float32')
            init_cell_data = np.zeros((num_layers, batch_size, hidden_size), dtype='float32')
            init_hidden = to_variable(init_hidden_data)
            init_cell = to_variable(init_cell_data)
            for step_id in range(batch_num):
                x_data = np.arange(12).reshape(4, 3).astype('int64')
                y_data = np.arange(1, 13).reshape(4, 3).astype('int64')
                y_data = y_data.reshape(((- 1), 1))
                x_data = x_data.reshape(((- 1), num_steps, 1))
                y_data = y_data.reshape(((- 1), num_steps, 1))
                x = to_variable(x_data)
                y = to_variable(y_data)
                (dy_loss, last_hidden, last_cell) = ptb_model(x, y, init_hidden, init_cell)
                out_loss = dy_loss.numpy()
                dy_loss.backward()
                sgd.minimize(dy_loss)
                ptb_model.clear_gradients()
                total_loss += out_loss
                iters += num_steps
                total_sample += 1
                if ((step_id % PRINT_STEP) == 0):
                    if (step_id == 0):
                        logging.info(('epoch %d | step %d, loss %0.3f' % (epoch_id, step_id, (total_loss / total_sample))))
                        avg_batch_time = time.time()
                    else:
                        speed = (PRINT_STEP / (time.time() - avg_batch_time))
                        logging.info(('epoch %d | step %d, loss %0.3f, speed %.3f steps/s' % (epoch_id, step_id, (total_loss / total_sample), speed)))
                        avg_batch_time = time.time()
            return (out_loss, last_hidden.numpy(), last_cell.numpy())
def train(place):
    num_layers = 1
    batch_size = 4
    hidden_size = 10
    num_steps = 3
    init_scale = 0.1
    max_epoch = 1
    dropout = 0.0
    vocab_size = 1000
    batch_num = 200
    with fluid.dygraph.guard(place):
        fluid.default_startup_program().random_seed = SEED
        fluid.default_main_program().random_seed = SEED
        ptb_model = PtbModel(hidden_size=hidden_size, vocab_size=vocab_size, num_layers=num_layers, num_steps=num_steps, init_scale=init_scale, dropout=dropout)
        sgd = SGDOptimizer(learning_rate=0.001, parameter_list=ptb_model.parameters())
        for epoch_id in range(max_epoch):
            total_loss = 0.0
            iters = 0.0
            total_sample = 0
            init_hidden_data = np.zeros((num_layers, batch_size, hidden_size), dtype='float32')
            init_cell_data = np.zeros((num_layers, batch_size, hidden_size), dtype='float32')
            init_hidden = to_variable(init_hidden_data)
            init_cell = to_variable(init_cell_data)
            for step_id in range(batch_num):
                x_data = np.arange(12).reshape(4, 3).astype('int64')
                y_data = np.arange(1, 13).reshape(4, 3).astype('int64')
                y_data = y_data.reshape(((- 1), 1))
                x_data = x_data.reshape(((- 1), num_steps, 1))
                y_data = y_data.reshape(((- 1), num_steps, 1))
                x = to_variable(x_data)
                y = to_variable(y_data)
                (dy_loss, last_hidden, last_cell) = ptb_model(x, y, init_hidden, init_cell)
                out_loss = dy_loss.numpy()
                dy_loss.backward()
                sgd.minimize(dy_loss)
                ptb_model.clear_gradients()
                total_loss += out_loss
                iters += num_steps
                total_sample += 1
                if ((step_id % PRINT_STEP) == 0):
                    if (step_id == 0):
                        logging.info(('epoch %d | step %d, loss %0.3f' % (epoch_id, step_id, (total_loss / total_sample))))
                        avg_batch_time = time.time()
                    else:
                        speed = (PRINT_STEP / (time.time() - avg_batch_time))
                        logging.info(('epoch %d | step %d, loss %0.3f, speed %.3f steps/s' % (epoch_id, step_id, (total_loss / total_sample), speed)))
                        avg_batch_time = time.time()
            return (out_loss, last_hidden.numpy(), last_cell.numpy())