Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

softmax算法模型加载数据的问题

Open calm736 opened this issue 8 years ago • 7 comments

在仿造数字识别的例子,写一个对帖子进行优质贴和非优质贴的模型训练,整个代码如下: `#coding=utf-8 from future import print_function import os from PIL import Image import numpy as np import paddle import paddle.fluid as fluid import pandas as pd

def softmax_regression(): tid = fluid.layers.data(name='tid', shape=[11], dtype='float32') predict = fluid.layers.fc(input=tid, size=2, act='softmax') return predict

def train_program(): label = fluid.layers.data(name='label', shape=[1], dtype='int64')

# Here we can build the prediction network in different ways. Please
predict = softmax_regression() # uncomment for Softmax
# predict = multilayer_perceptron() # uncomment for MLP
# predict = convolutional_neural_network()  # uncomment for LeNet5

# Calculate the cost from the prediction and label.
cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = fluid.layers.mean(cost)
acc = fluid.layers.accuracy(input=predict, label=label)
return [avg_cost, acc]

def optimizer_program(): return fluid.optimizer.Adam(learning_rate=0.001)

生成train_reader

def trainReader(trainFilePath): def reader(): with open(trainFilePath,'r') as f: lines = [line.strip() for line in f] for line in lines: item = line.strip().split('\t')#一条数据 yield [np.array(item[1:12]).astype('float32'),np.array(item[-1:]).astype('float32')] return reader

生成test_reader

def testReader(testFilePath): def reader(): with open(testFilePath,'r') as f: lines = [line.strip() for line in f] for line in lines: item = line.strip().split('\t')#一条数据 yield [np.array(item[1:12]).astype('float32'),np.array(item[-1:]).astype('float32')] return reader

def main(): #需要从文件读取训练数据 train_reader = trainReader(trainFilePath = "/root/lilinke/code/python/demo/data/cutepet_train_minmax.txt") # train_reader = paddle.batch( # paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=500), # batch_size=64) #需要从文件中读取测试数据 test_reader = testReader(testFilePath = "/root/lilinke/code/python/demo/data/cutepet_train_minmax.txt") # test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=64)

use_cuda = False  # set to True if training with GPU
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()

trainer = fluid.Trainer(
    train_func=train_program, place=place, optimizer_func=optimizer_program)

# Save the parameter into a directory. The Inferencer can load the parameters from it to do infer
params_dirname = "cutepet_premium_thread_network.inference.model"

lists = []

def event_handler(event):
    if isinstance(event, fluid.EndStepEvent):
        if event.step % 100 == 0:
            # event.metrics maps with train program return arguments.
            # event.metrics[0] will yeild avg_cost and event.metrics[1] will yeild acc in this example.
            print("Pass %d, Batch %d, Cost %f" % (event.step, event.epoch,
                                                  event.metrics[0]))

    if isinstance(event, fluid.EndEpochEvent):
        avg_cost, acc = trainer.test(
            reader=test_reader, feed_order=['tid', 'label'])

        print("Test with Epoch %d, avg_cost: %s, acc: %s" %
              (event.epoch, avg_cost, acc))

        # save parameters
        trainer.save_params(params_dirname)
        lists.append((event.epoch, avg_cost, acc))

# Train the model now
trainer.train(
    num_epochs=5,
    event_handler=event_handler,
    reader=train_reader,
    feed_order=['tid', 'label'])

# find the best pass
best = sorted(lists, key=lambda list: float(list[1]))[0]
print('Best pass is %s, testing Avgcost is %s' % (best[0], best[1]))
print('The classification accuracy is %.2f%%' % (float(best[2]) * 100))

# def load_image(file):
#     im = Image.open(file).convert('L')
#     im = im.resize((28, 28), Image.ANTIALIAS)
#     im = np.array(im).reshape(1, 1, 28, 28).astype(np.float32)
#     im = im / 255.0 * 2.0 - 1.0
#     return im


#加载需要预测的帖子特征数据进行预测
cur_dir = os.path.dirname(os.path.realpath(__file__))
#img = load_image(cur_dir + '/image/infer_3.png')
inferencer = fluid.Inferencer(
    infer_func=softmax_regression, # uncomment for softmax regression
    # infer_func=multilayer_perceptron, # uncomment for MLP
    # infer_func=convolutional_neural_network,  # uncomment for LeNet5
    param_path=params_dirname,
    place=place)

# results = inferencer.infer({'img': img})
# lab = np.argsort(results)  # probs and lab are the results of one batch data
# print("Inference result of image/infer_3.png is: %d" % lab[0][0][-1])

if name == 'main': main() `

使用数据加载后老报字段和feed_list不匹配的问题,望大神看一下。

calm736 avatar Aug 27 '18 03:08 calm736

image 报错提示

calm736 avatar Aug 27 '18 03:08 calm736

贴一下 train_program, 以及 数据样例。

seiriosPlus avatar Aug 27 '18 03:08 seiriosPlus

image 第一列是序号,从第二列开始,到最后一列,最后一列是标签 训练程序: ` trainer = fluid.Trainer( train_func=train_program, place=place, optimizer_func=optimizer_program) trainer.train( num_epochs=5, event_handler=event_handler, reader=train_reader, feed_order=['tid', 'label'])

def train_program(): label = fluid.layers.data(name='label', shape=[1], dtype='int64')

# Here we can build the prediction network in different ways. Please
predict = softmax_regression() # uncomment for Softmax

# Calculate the cost from the prediction and label.
cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = fluid.layers.mean(cost)
acc = fluid.layers.accuracy(input=predict, label=label)
return [avg_cost, acc]

def softmax_regression(): tid = fluid.layers.data(name='tid', shape=[11], dtype='float32') predict = fluid.layers.fc(input=tid, size=2, act='softmax') return predict `

calm736 avatar Aug 27 '18 03:08 calm736

修改train_reader: yield [item[1:12], item[-1:]]

seiriosPlus avatar Aug 27 '18 04:08 seiriosPlus

image image 修改train_reader以后提示依旧是这个问题

calm736 avatar Aug 27 '18 04:08 calm736

reader=train_reader, feed_order=['tid', 'label']) 这个地方有问题 reader返回的数据和feed_order 必须一一对应

xuezhong avatar Aug 30 '18 08:08 xuezhong

您好,此issue在近一个月内暂无更新,我们将于今天内关闭。若在关闭后您仍需跟进提问,可重新开启此问题,
我们将在24小时内回复您。因关闭带来的不便我们深表歉意,请您谅解~感谢您对PaddlePaddle的支持!

lucywsq avatar Sep 20 '18 17:09 lucywsq