Paddle How to use accuracy metric in model

s71maibg  于 2022-04-21  发布在  Java
关注(0)|答案(2)|浏览(185)

Hi!

I took beginners code from the guide:
link|code is down below

I try to add accuracy metric like here

Have this:


# load library

import paddle.fluid as fluid
import numpy

# define data

train_data=numpy.array([[1.0],[2.0],[3.0],[4.0]]).astype('float32')
y_true = numpy.array([[2.0],[4.0],[6.0],[8.0]]).astype('float32')

# define network

x = fluid.layers.data(name="x",shape=[1],dtype='float32')
y = fluid.layers.data(name="y",shape=[1],dtype='float32')
y_predict = fluid.layers.fc(input=x,size=1,act=None)

# define loss function

cost = fluid.layers.square_error_cost(input=y_predict,label=y)
result = fluid.layers.accuracy(input=y_predict, label=y, k=1)
avg_cost = fluid.layers.mean(cost)

# define optimization algorithm

sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.01)
sgd_optimizer.minimize(avg_cost)

# initialize parameters

cpu = fluid.core.CPUPlace()
exe = fluid.Executor(cpu)
exe.run(fluid.default_startup_program())

## start training and iterate for 100 times

for i in range(100):
    outs = exe.run(
        feed={'x':train_data,'y':y_true},
        fetch_list=[y_predict.name,avg_cost.name,result[0]])

# observe result

print outs

Ofc I have Error of input type, but if I change input in int also get an Error.
How should I combine loss and accuracy metrics in code to work it normally?

wb1gzix0

wb1gzix01#

Thanks for your feedback. I found that accuracy needs labels in int64 while squeare_error_cost needs labels in float32 or float64. So you need to add y = fluid.layers.cast(y, 'int64') before accuracy in the code above and we will optimizer these two apis soon.

nwnhqdif

nwnhqdif2#

Thanks for your feedback. I found that accuracy needs labels in int64 while squeare_error_cost needs labels in float32 or float64. So you need to add y = fluid.layers.cast(y, 'int64') before accuracy in the code above and we will optimizer these two apis soon.

Lets see the code:

def NN():
      x = fluid.data(name='x', shape=[1, 14], dtype='float64')
     hidden = fluid.layers.fc(input=x, size=200, act='relu')
    hidden = fluid.layers.fc(input=hidden, size=200, act='relu')
    prediction = fluid.layers.fc(input=hidden, size=10, act='softmax')
    return prediction

def train():
label = fluid.data(name='y', shape=[1, 1], dtype='int64')
    predict = NN() 
    cost = fluid.layers.cross_entropy(input=predict, label=label)
     avg_cost = fluid.layers.mean(cost)
    acc = fluid.layers.accuracy(input=predict, label=label)
    return predict, [avg_cost, acc]

Then I do this:
prediction, [avg_loss, acc] = train()

And get this:

----------------------
Error Message Summary:
----------------------
InvalidArgumentError: ShapeError: label's dimensions of AccuracyOp must be 2. But received label's dimensions = 1, label's shape = [1]
  [Hint: Expected label_dim.size() == 2, but received label_dim.size():1 != 2:2.] at (/paddle/paddle/fluid/operators/metrics/accuracy_op.cc:62)
  [operator < accuracy > error]

What I need to fix it?

相关问题