how to generate two samples each line using pyDataProvider
Each line encodes two samples in input files. I want to extract them when parsing each line in pyDataProvider. These two samples must be next to each other in a mini-batch. Anyone have a good solution? Thx.
@pkuyym
我理解,主要解决:
-
一个是shuffle问题,避免连续两个进行shuffle。 这个不知道是否dataprovider能提供更加灵活的shuffle方法,比如能否hook 用户的shuffle函数。 ( @pkuyym 你也可以考虑hack下dataprovider源码,控制下shuffle逻辑应该能解决问题, 具体思路可以请教下 @reyoung )
-
另外是否能用现有的dataprovider来抽象一下,构建一个新模型,伪装出类似的效果。 比如,配置一个模型,包含两个相同的模型,分别用两个input做输入,然后共享一组参数(可能要特殊处理一下cost layer)。这样起到等价的效果,但是不需要改dataprovovider的shuffle实现。仔细的思路可以推演一下(可能因为cost函数特性,无法构造出等价的模型)。
@pkuyym
If you are using PyDataProvider2, the shuffle job is handled inside the DataProvider. But you can still set should_shuffle=False, then handle the shuffle by yourself. See here. In this situation, you should ensure the training process use yield shuffled data.
But it seems that you want to train a pairwise network or a pair data when training. Maybe you could use two inputs for this training job, then the training job contains two neural networks shared the same parameter. Or you could use a time series(sequence) as input.
@reyoung @backyes In fact, I want to use paddle to predict ad's CTR. Traditionally, we trade CTR prediction as a classification problem, clicked instance labeled as 1 otherwise 0. But the format of original data is as following: pv click fea1 fea2 ... pv represents the number of total instances, and click means the number of clicked instance, fea1...feaN are the sharing features. The loss function are Cost = (click) * log Prob + (pv - click) * log (1 - Prob) for cross entropy loss. So we can split the instance to two instances with different weight but sharing the same feature. As following: click[weight] 1 fea1 fea2 ... pv-click[weight] 0 fea1 fea2 ... But I want these two instances to be near to each other in a mini-batch, so that we can approximate the original behavior as possible as we can. PS. I use the mpi paddle, so writing my own layer or overload the dataprovider layer maybe not easy.
@pkuyym Maybe you can config the neural network in one file, like follows.
... # ignore previous neural network.
prediction = fc_layer(input=previous_layers, size=2, act=SoftmaxActivation())
# so prediction is the Prob now.
with mixed_layer(size=2, act=LogActivation()) as logProb:
logProb += identity_projection(input=prediction)
# here, logProb and logProb[1] is the logProb.
with mixed_layer(size=2, act=LinearActivation()) as crossEntropy:
# set data layer as a dense vector, with value (click, 0)
# dotmul means out[i] +=(a[i] .* b[i])
crossEntropy += dotmul_operator(a = logProb, b = data_layer(name="click", size=2))
# set data layer as a dense vector, with value (0, pv-click)
crossEntropy += dotmul_operator(a = logProb, b = data_layer(name="pv_click", size=2))
# crossEntropy will be a 2 dimension vector now, with [ click * logProb, (pv-click) * log(1-prob)]
# then, sum two dimension of crossEntropy together.
crossEntropy = sum_cost(input=scaling_layer(input=crossEntropy,
weight=data_layer(name="weight", size=1)
))
outputs(crossEntropy)
- Use softmax to calculate prob and (1-prob)
- Use an identity projection to calculate logProb, log(1-prob)
- Use dotmul to multiply click and pv-click
- Use sum_cost sum the whole XE cost.
NOTE: You should use the latest released Paddle in master branch for this feature.
@pengli09 If you are convenient, please help me for reviewing this configuration whether it has some major mistakes or not.
@reyoung @pengli09 Thanks for your nice solution. The idea seems to make sense. We can use cross entropy loss to train the model, however, we also use AUC to evaluate model's performance, so it is necessary to add AUC calculation logic. As I know, it's not easy to accumulate two weighted AUC layers to get the final AUC. If I have any misunderstanding, please let me know.
If you use Yang's solution, I think you no longer need to split one sample into two. You can simply yield (features, click, pv-click, golden label) in your data provider, then use features to compute probabilities (prediction in Yang's config), and finally feed prediction and golden label to AUC evaluator as usual to compute AUC.
@pengli09 @reyoung The original line contains two golden labels, 0 for (pv-click) instances and 1 for click instances. Can we use AUC layer (with weight) twice and combine the two outputs to get the final AUC?