Skip to content

  • 体验新版
    • 正在加载...
  • 登录
  • PaddlePaddle
  • Paddle
  • Issue
  • #26433

P
Paddle
  • 项目概览

PaddlePaddle / Paddle
大约 2 年 前同步成功

通知 2325
Star 20933
Fork 5424
  • 代码
    • 文件
    • 提交
    • 分支
    • Tags
    • 贡献者
    • 分支图
    • Diff
  • Issue 1423
    • 列表
    • 看板
    • 标记
    • 里程碑
  • 合并请求 543
  • Wiki 0
    • Wiki
  • 分析
    • 仓库
    • DevOps
  • 项目成员
  • Pages
P
Paddle
  • 项目概览
    • 项目概览
    • 详情
    • 发布
  • 仓库
    • 仓库
    • 文件
    • 提交
    • 分支
    • 标签
    • 贡献者
    • 分支图
    • 比较
  • Issue 1,423
    • Issue 1,423
    • 列表
    • 看板
    • 标记
    • 里程碑
  • 合并请求 543
    • 合并请求 543
  • Pages
  • 分析
    • 分析
    • 仓库分析
    • DevOps
  • Wiki 0
    • Wiki
  • 成员
    • 成员
  • 收起侧边栏
  • 动态
  • 分支图
  • 创建新Issue
  • 提交
  • Issue看板
已关闭
开放中
Opened 8月 19, 2020 by saxon_zh@saxon_zhGuest

acc不收敛多个epoch依然很低

Created by: luolimingfighting

import paddle.fluid as fluid import paddle import shutil import six import os import time import numpy as np from paddle.fluid import compiler

define reader

def reader_creator(label_dic, col_dic, max_value, file_list, is_train): def reader(): for file in file_list: files = os.listdir(file) for data in files: with open(file+data, "r") as f: for line in f: fea_index = [] fea_value = [] features = line.rstrip("\n").split(",") features.pop(0) if is_train: label = int(label_dic[features.pop(0)]) else: label = 0 for ele in features: col_name, value = ele.split(" ")[0], ele.split(" ")[1] if col_name in col_dic: index = col_dic[col_name] fea_index.append(index) fea_value.append(float(value))

                    yield fea_index, fea_value, [label]
return reader

def run_iterable(program, exe, loss, py_reader): for data in py_reader: loss_value = exe.run(program=program, feed=data, fetch_list=[loss]) print("loss is {}".format(loss_value))

train_file = "./train_data/" file_list = [train_file] is_train=True

#col_dic, max_value, label_dic, label_index = init_dic(train_file) col_dic = np.load("./thirdparty/col-file.npy", allow_pickle=True).item() max_value = len(col_dic) label_dic = np.load("./thirdparty/label-file.npy", allow_pickle=True).item() label_index = len(label_dic) print("feature_num_is:{}".format(max_value)) print("label_num_is:{}".format(label_index)) sparse_feature_dim = max_value

ITERABLE = True USE_CUDA = True USE_DATA_PARALLEL = False

if ITERABLE: if USE_DATA_PARALLEL: places = fluid.cuda_places() if USE_CUDA else fluid.cpu_places(8) else: places = fluid.cuda_places() if USE_CUDA else fluid.cpu_places(1) else: places = None

#define data input type sparse_input_feature_idx = fluid.layers.data(name="feature_idx", shape=[1], lod_level=1, dtype="int64") sparse_input_feature_value_float = fluid.layers.data(name="feature_value", shape=[1], lod_level=1, dtype="float32") sparse_input_emb_weight = fluid.layers.embedding( input=sparse_input_feature_idx, is_sparse=True, # you need to patch https://github.com/PaddlePaddle/Paddle/pull/14190 # if you want to set is_distributed to True is_distributed=False, size=[sparse_feature_dim, 1],)

#x = fluid.layers.data(name="x", shape=[max_value+1], dtype="float32") y = fluid.layers.data(name="y", shape=[1], dtype="int64") w_mat_x_with_lod = sparse_input_feature_value_float * sparse_input_emb_weight w_mat_x = fluid.layers.sequence_pool(input=w_mat_x_with_lod, pool_type='sum') mean_w_x = fluid.layers.mean(w_mat_x)

##define network def network(input): fc = fluid.layers.fc(input=input, size=label_index+1, act="softmax") return fc

model = network(w_mat_x) cost = fluid.layers.cross_entropy(input=model, label=y) avg_cost = fluid.layers.mean(cost) acc = fluid.layers.accuracy(input=model, label=y)

obtain main program

main_program = fluid.default_main_program()

start_program = fluid.default_startup_program()

define optimizer

optimizer = fluid.optimizer.Adam() opts = optimizer.minimize(avg_cost)

read train data

train_reader = paddle.batch(reader_creator(label_dic, col_dic, max_value, file_list, is_train), batch_size=128)

run

place = fluid.CUDAPlace(0) parallel_places = [fluid.CUDAPlace(0), fluid.CUDAPlace(1), fluid.CUDAPlace(2), fluid.CUDAPlace(3)] if USE_CUDA else [fluid.CPUPlace()] * 4 compiled_program = compiler.CompiledProgram(main_program) compiled_program.with_data_parallel(loss_name=avg_cost.name, places=parallel_places)

exe = fluid.Executor(place) exe.run(start_program)

param_path = "./my_paddle_model" param_path1 = "./params_model"

save model

feeder = fluid.DataFeeder(place=place, feed_list=[sparse_input_feature_idx, sparse_input_feature_value_float, y])

for pass_id in range(20): # for batch_id, data in enumerate(train_reader()): train_cost, train_acc = exe.run(program=compiled_program, feed=feeder.feed(data), fetch_list=[avg_cost, acc]) print_data = [pass_id, batch_id, train_cost[0], train_acc[0]] print('Pass:{}, Batch:{}, Cost:{}, Accuracy:{}'.format(*print_data)) #time.sleep(2) # #print("first epoch complete and start to save model") #fluid.io.save_persistables(executor=exe, dirname=param_path, main_program=compiled_program) #fluid.io.save_params(executor=exe, dirname=param_path, main_program=compiled_program) #print("model save success and start to next epoch") # # save_path = './output/' #print(save_path) #if not os.path.exists(save_path): #os.makedirs(save_path) # #shutil.rmtree(save_path, ignore_errors=True) # #os.makedirs(save_path) # fluid.io.save_inference_model(dirname=save_path, feeded_var_names=[w_mat_x.name], target_vars=[model], executor=exe)

这是我训练的base,我的数据形式是特征索引加特征权重,模型只需一层FC实现LR,不知是不是组网的逻辑问题还是其它原因,acc多个epoch依然很低很低。

指派人
分配到
无
里程碑
无
分配里程碑
工时统计
无
截止日期
无
标识: paddlepaddle/Paddle#26433
渝ICP备2023009037号

京公网安备11010502055752号

网络110报警服务 Powered by GitLab CE v13.7
开源知识
Git 入门 Pro Git 电子书 在线学 Git
Markdown 基础入门 IT 技术知识开源图谱
帮助
使用手册 反馈建议 博客
《GitCode 隐私声明》 《GitCode 服务条款》 关于GitCode
Powered by GitLab CE v13.7