请教一些使用问题?
Created by: yeyupiaoling
环境
- PaddlePaddle dev 版本
问题
话不多少,上一个demo,
import paddle.fluid as fluid
import numpy as np
BATCH_SIZE = 384
data1 = fluid.layers.data(name="input1", shape=[2], dtype="float32")
data2 = fluid.layers.data(name="input2", shape=[1], dtype="float32")
def cls_ohem(cls_prob, label):
zeros = fluid.layers.zeros_like(label)
ones = fluid.layers.zeros_like(label) + 1
# 把小于等于0的都设置为0
cond = fluid.layers.cast(fluid.layers.greater_than(label, zeros), dtype='int64')
ie = fluid.layers.IfElse(cond)
with ie.true_block():
ie.output(ones)
with ie.false_block():
ie.output(zeros)
label_filter_invalid = ie()[0]
# 类别size[2*batch]
num_cls_prob = fluid.layers.size(cls_prob)
cls_prob_reshpae = fluid.layers.reshape(cls_prob, [num_cls_prob, -1])
label_int = fluid.layers.cast(label_filter_invalid, np.int32)
label_int = fluid.layers.squeeze(label_int, axes=[])
# 获取batch数
num_row = BATCH_SIZE
# 对应某一batch而言,batch*2为非人类别概率,batch*2+1为人概率类别,indices为对应 cls_prob_reshpae
# 应该的真实值,后续用交叉熵计算损失
row = fluid.layers.range(0, num_row * 2, 2, np.int32)
indices_ = row + label_int
# 真实标签对应的概率
label_prob = fluid.layers.squeeze(fluid.layers.gather(cls_prob_reshpae, indices_), axes=[])
loss = 0 - fluid.layers.log(label_prob + 1e-10)
zeros = fluid.layers.zeros_like(label_prob)
# 统计 0 和 1 的数量
valid_inds = fluid.layers.where(fluid.layers.greater_equal(label, zeros))
num_valid = fluid.layers.reduce_sum(valid_inds)
# 选取70%的数据
keep_num = fluid.layers.cast(num_valid * 0.7, dtype=np.int32)
# 只选取 0 和 1的70%损失
loss1 = loss * valid_inds
loss, _ = fluid.layers.topk(loss1, k=keep_num)
return fluid.layers.reduce_mean(loss)
loss = cls_ohem(data1, data2)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
d1 = []
for i in range(BATCH_SIZE):
r = np.random.rand()
d1.append([r, 1 - r])
d1 = np.random.rand(BATCH_SIZE, 2).astype(np.float32)
d2 = np.array(np.random.randint(-2, 2, [BATCH_SIZE, 1])).astype('float32')
r = exe.run(program=fluid.default_main_program(),
feed={"input1": d1, "input2": d2},
fetch_list=[loss])
print(r)
错误信息为
C++ Callstacks:
Tensor holds the wrong type, it holds int64_t, but desires to be bool at [/paddle/paddle/fluid/framework/tensor_impl.h:45]
PaddlePaddle Call Stacks: