一、背景
label,score= model.predict(text) # 例如label =1,score =0.9
jsonify({"label":label,"score":score})
1、报错
TypeError: Object of type int32 is not JSON serializable
2、原因:
score的数据类型是numpy.float32,所以应将numpy.float32转换成python内置的数据类型float
以下是实验代码
import numpy as np
# examples using a.item()
type(np.float32(0).item()) # <type 'float'>
type(np.float64(0).item()) # <type 'float'>
type(np.uint32(0).item()) # <type 'long'>
3 、解决方法
score = score.item()
附上两个彩蛋:
云孚科技的可以测试他们NLP效果的地址,还蛮全的。https://www.yunfutech.com/demo?tab=2
很好总结NLP所需要的技术和应用方面https://zhuanlan.zhihu.com/p/101109775