首页 炼丹房 帖子详情
请教有关“ accuracy”的问题,求救
收藏
快速回复
炼丹房 问答新手上路 1387 2
请教有关“ accuracy”的问题,求救
收藏
快速回复
炼丹房 问答新手上路 1387 2

先上代码。

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      fjnuzs
#
# Created:     23/12/2018
# Copyright:   (c) fjnuzs 2018
# Licence:     
#-------------------------------------------------------------------------------

# Include libraries.
import paddle
import paddle.fluid as fluid
import numpy

training_data = [(1, [1, 1, 1, 0, 0]), (1, [1, 0, 1, 0, 0]), (1, [0, 1, 0, 0, 0]), (0, [0, 0, 0, 1, 1]),
                 (0, [1, 0, 0, 0, 1]),
                 (0, [0, 0, 0, 1, 1]), (1, [1, 1, 1, 0, 0]), (1, [1, 0, 1, 0, 0]), (1, [0, 1, 0, 0, 0]),
                 (0, [0, 0, 0, 1, 1]), (0, [1, 0, 0, 0, 1]),
                 (0, [0, 0, 0, 1, 1]), (1, [1, 1, 1, 0, 0]), (1, [1, 0, 1, 0, 0]), (1, [0, 1, 0, 0, 0]),
                 (0, [0, 0, 0, 1, 1]), (0, [1, 0, 0, 0, 1]),
                 (0, [0, 0, 0, 1, 1]), (1, [1, 1, 1, 0, 0]), (1, [1, 0, 1, 0, 0]), (1, [0, 1, 0, 0, 0]),
                 (0, [0, 0, 0, 1, 1]), (0, [1, 0, 0, 0, 1]),
                 (0, [0, 0, 0, 1, 1]), (1, [1, 1, 1, 0, 0]), (1, [1, 0, 1, 0, 0]), (1, [0, 1, 0, 0, 0]),
                 (0, [0, 0, 0, 1, 1]), (0, [1, 0, 0, 0, 1]),
                 (0, [0, 0, 0, 1, 1]), (1, [1, 1, 1, 0, 0]), (1, [1, 0, 1, 0, 0]), (1, [0, 1, 0, 0, 0]),
                 (0, [0, 0, 0, 1, 1]), (0, [1, 0, 0, 0, 1]),
                 (0, [0, 0, 0, 1, 1])]

def reader():
    def reader_creator():
        for (y,x) in training_data:
            yield numpy.array(x).astype('float32'),numpy.float32(y)
    return reader_creator

def main():
    train_reader = paddle.batch(reader(),batch_size=10)

    data   = fluid.layers.data(name='data',shape=[1,5],dtype='float32')
    hid    = fluid.layers.fc(input=data, size=20, act='relu')
    output = fluid.layers.fc(input=hid, size=1, act='softmax')
    label  = fluid.layers.data(name='label',shape=[1],dtype='float32')

    cost   = fluid.layers.square_error_cost(input=output,label=label)
    avg_cost = fluid.layers.mean(cost)

    #如果把下面这句放开,就不可以了
    #accu    = fluid.layers.accuracy(input=output, label=label)

    optimizer = fluid.optimizer.AdamOptimizer(learning_rate=0.001)
    opts      = optimizer.minimize(avg_cost)
    place = fluid.CPUPlace()
    feeder = fluid.DataFeeder(place=place, feed_list=['data', 'label'])
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    for pass_id in range(100):
        for batch_id,data in enumerate(train_reader()):
            print (data)
            fl = exe.run(fluid.default_main_program(),
                                 feed=feeder.feed(data),
                                 fetch_list=[avg_cost])
            print("Pass {0},Loss {1}".format(pass_id,fl[0]) )
    pass;

if __name__ == '__main__':
    main()

我的环境是python3.7,win7。上述程序可以正确运行。

之后,我增加了一个accuracy,也就是上述程序被我注释掉的哪一行,运行错误,提示信息如下:

Message File Name Line Position
Traceback
。。。
run D:\Programs\Python\Python37\lib\site-packages\paddle\fluid\executor.py 472
EnforceNotMet: Tensor holds the wrong type, it holds float at [E:\dist\Paddle\paddle/fluid/framework/tensor_impl.h:29]
PaddlePaddle Call Stacks:
Windows not support stack backtrace yet.

花了很多时间,搞不清愿意,请大牛指教。

开始怀疑在我的环境下不能用accuracy,可是下载的0-9程序是没有问题。

 

0
收藏
回复
全部评论(2)
时间顺序
busyboxs
#2 回复于2018-12

accuracy应该接受int型的输入,square_error_cost接受float的输入,感觉应该是这样。所以回归里面应该没有accuracy这种说法吧,分类里面才会有。

0
回复
s
stonehege
#3 回复于2019-09

fetch_list中要加一下acc

0
回复
在@后输入用户全名并按空格结束,可艾特全站任一用户