[英]Using tensorflow or keras to build a NN model by feeding 'pairwise' samples
I'm trying to implement a NN model with pairwise samples. 我正在尝试使用成对样本实现NN模型。 Details are shown in follows: 详细信息如下所示:
Original data: 原始数据:
Processing these original data for real training: 处理这些原始数据以进行实际训练:
Select 2 samples from X_org
randomly (so we have 100*99/2 such combinations) to form a new 'pairwise' sample, and the prediction target, namely the new y label is the subtraction of the two corresponding y_org
labels (Y_org_sample1 - Y_org_sample2). 从X_org
随机选择2个样本(因此我们有100 * X_org
这样的组合)以形成新的“成对”样本,并且预测目标(即新y标签是两个对应的y_org
标签(Y_org_sample1-Y_org_sample2)的减法) )。 Now we have new X_train
and Y_train
. 现在我们有了新的X_train
和Y_train
。
I need a more a NN model (DNN, CNN, LSTM, whatever ...), with which I can pass the first sub_sample of one pairwise sample from X_train
into the model and will get one result, same step for the second sub_sample. 我需要一个更多的NN模型(DNN,CNN,LSTM,等等),通过该模型,我可以将一个成对样本的第一个sub_sample从X_train
到模型中,并得到一个结果,第二个sub_sample的步骤与此相同。 By calculating the subtraction of the two results, I can get the prediction of this pairwise sample. 通过计算两个结果的减法,可以得到此成对样本的预测。 This prediction will be the one compared with the corresponding Y label from Y_train
. 与来自Y_train
的相应Y标签相比,该预测将是一个预测。
Overall, I need to train a model (update the weights) after feeding it a 'pairwise' sample (two successive sub samples). 总体而言,在向模型提供“成对”样本(两个连续的子样本)后,我需要训练模型(更新权重)。 The reason why I don't choose a 'two-arm' model (eg merge two arms by xxx.sub()
) is that I will only feed one sub sample during test process. 我之所以不选择“两臂”模型(例如,通过xxx.sub()
合并两臂),是因为在测试过程中我只会提供一个子样本。 I will just use the model to predict one sub-sample finally. 我将仅使用模型来最终预测一个子样本。
So I will use the data from X_train
during train step, while use X_org-like data format during test step. 因此,我将在训练步骤中使用来自X_train
的数据,而在测试步骤中使用类似X_org的数据格式。 It looks a bit complex. 看起来有点复杂。
Looks like Tensorflow would be more feasible for this task, if keras also works, please kindly share your idea. 看起来Tensorflow对此任务更可行,如果keras也可行,请分享您的想法。
You can first create a model that will take only one X_org-like element: 您可以首先创建一个仅包含一个类似于X_org的元素的模型:
#create a model the way you like it, it can be Functional API or Sequential, no problem
xOrgModel = createAModelForXOrgData(...)
Now, lets create a second model, this time necessarily functional API that works with both inputs: 现在,让我们创建第二个模型,这次必须是可用于两个输入的功能性API:
from keras.models import Model
from keras.layers import Input, Subtract
input1 = Input(shapeOfInput)
input2 = Input(shapeOfInput)
output1 = xOrgModel(input1)
output2 = xOrgModel(input2)
output = Subtract()([output1,output2])
pairWiseModel = Model([input1,input2],output)
Now you have two models: xOrgModel
and pairWiseModel
. 现在,您有两个模型: xOrgModel
和pairWiseModel
。 You can use any of them depending on the task you are doing at the moment. 您可以根据当前正在执行的任务使用它们中的任何一个。
Both models are sharing their weights. 两种模型都在分担自己的重量。 This means that you can train any of them and the other will be updated as well. 这意味着您可以训练其中任何一个,另一个也会被更新。
Using the pairwise model 使用成对模型
First, organize your data in two separate arrays. 首先,将数据组织在两个单独的阵列中。 (Because our model uses two inputs) (因为我们的模型使用两个输入)
L = len(X_org)
x1 = []
x2 = []
y = []
for i in range(L):
for j in range(i+1,L):
x1.append(X_org[i])
x2.append(X_org[j])
y.append(Y_org[i] - Y_org[j])
x1 = np.array(x1)
x2 = np.array(x2)
y = np.array(y)
Train and predict with a list of inputs: 用输入列表进行训练和预测:
pairWiseModel.fit([x1,x2],y,...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.