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?
2条答案
按热度按时间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.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:
Then I do this:
prediction, [avg_loss, acc] = train()
And get this:
What I need to fix it?