How to use accuracy metric in model
Created by: SaviorD7
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?