def prepare_feed_data(data, place): tensor_words = to_lodtensor(map(lambda x: x[0], data), place) label = np.array(map(lambda x: x[1], data)).astype("int64") label = label.reshape([len(label), 1]) tensor_label = fluid.LoDTensor() tensor_label.set(label, place) return tensor_words, tensor_label
def run_benchmark(model, args): if args.use_cprof: pr = cProfile.Profile() pr.enable() start_time = time.time() word_dict = paddle.dataset.imdb.word_dict() print("load word dict successfully") dict_dim = len(word_dict) data = fluid.layers.data(name="words", shape=[1], dtype="int64", lod_level=1) label = fluid.layers.data(name="label", shape=[1], dtype="int64") prediction = model(data, dict_dim) cost = fluid.layers.cross_entropy(input=prediction, label=label) avg_cost = fluid.layers.mean(x=cost) adam_optimizer = fluid.optimizer.Adam(learning_rate=0.002) adam_optimizer.minimize(avg_cost) accuracy = fluid.evaluator.Accuracy(input=prediction, label=label) train_reader = paddle.batch(paddle.reader.shuffle( paddle.dataset.imdb.train(word_dict), buf_size=25000), batch_size=args.batch_size) place = core.CPUPlace() if args.device == 'CPU' else core.CUDAPlace(0) exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) for it, pass_id in enumerate(xrange(args.pass_num)): accuracy.reset(exe) if iter == args.iterations: break for data in train_reader(): tensor_words = to_lodtensor(map(lambda x: x[0], data), place) label = np.array(map(lambda x: x[1], data)).astype("int64") label = label.reshape([args.batch_size, 1]) tensor_label = fluid.LoDTensor() tensor_label.set(label, place) loss, acc = exe.run(fluid.default_main_program(), feed={ "words": tensor_words, "label": tensor_label }, fetch_list=[avg_cost] + accuracy.metrics) pass_acc = accuracy.eval(exe) print("Iter: %d, loss: %s, acc: %s, pass_acc: %s" % (it, str(loss), str(acc), str(pass_acc)))
def to_lodtensor(data, place): seq_lens = [len(seq) for seq in data] cur_len = 0 lod = [cur_len] for l in seq_lens: cur_len += l lod.append(cur_len) flattened_data = np.concatenate(data, axis=0).astype("int64") flattened_data = flattened_data.reshape([len(flattened_data), 1]) res = fluid.LoDTensor() res.set(flattened_data, place) res.set_lod([lod]) return res