cross_entropy not working correctly when label is not in expected range
Created by: walloollaw
when the label for fluid.layers.cross_entropy
is not in the expected range,
for example when my real class num is 100, but i give it a 1000, this layer can be executed and output a large value
my test code:
import sys
import numpy as np
def get_data(shape=[1, 100]):
sz = reduce(lambda x, y: x*y, shape)
d = [1] * sz
data = np.array(d, dtype='float32').reshape(shape)
return data / sz
def test():
import paddle.fluid as fluid
data = get_data()
x = fluid.layers.data(name='x', shape=data.shape[1:], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
out = fluid.layers.cross_entropy(input=x, label=label)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
fetch_list = [out]
exe.run(fluid.default_startup_program())
program = fluid.default_main_program()
y = exe.run(
program=program,
feed={'x': data, 'label': np.array(1000).reshape([1,-1])},
fetch_list=fetch_list)
print('results:')
print y
test()
results:
results:
[array([[1.e+20]], dtype=float32)]