简体   繁体   English

Keras模型网络3D输入到2D输出

[英]Keras Model Network 3D Input to 2D Output

I have a 3D Input (Samples, Steps, Features). 我有一个3D输入(示例,步骤,功能)。 So each sample has a chain of steps that have different features. 因此,每个样本都有一系列具有不同功能的步骤。 Now, I want a 2D Output (Samples, Steps) where I have samples and at each step of the sample a 0 or a 1 calculated from the model. 现在,我想要一个2D输出(样本,步骤),其中有样本,并且在样本的每个步骤中都有从模型计算出的0或1。

So I think it is a sequential binary classification problem. 因此,我认为这是一个顺序二进制分类问题。 I have some difficult to define the model, especially the output layer. 我很难定义模型,尤其是输出层。

Here are the shapes of numpy arrays: 以下是numpy数组的形状:

x_train.shape 
# (200, 1657, 669)

x_test.shape
# (41, 1657, 669)

y_train.shape
# (200, 1657)

y_test.shape
# (41, 1657)

I tried this model but the output was not the one I was expecting 我尝试了这种模型,但是输出不是我期望的那种

n_residues, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_residues,n_features)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.1))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
m_classes = model.predict_classes(x_test, verbose=0)
print(m_classes)
[  36   36   59   32   16   32   36  804 1047   16   16   36   32   36
   36   36   16   16   16   16   16   16   36   16   36   36   36   16
   59   36   36   36   16   16   16  804   16   16   16   36   36]

The output is a 41 long vector for the 41 samples in the test set with classes 0 -1657 I assume. 对于假定为0 -1657类的测试集中的41个样本,输出是41个长向量。

My desired output for the test set would be 41 binary vectors that are 1657 long. 我期望的测试集输出将是41个二进制矢量,长度为1657。

Thanks! 谢谢!

When you are dealing with Conv1D, RNN or sequence models the output can be one to one, many to one, (or) many to many. 当您处理Conv1D,RNN或序列模型时,输出可以是一对一,多对一,(或)多对多。 In this case, the model is acting many to one. 在这种情况下,模型是多对一的行为。 Generally, In Keras, there is a return_sequence or stateful parameter. 通常,在Keras中,存在return_sequencestateful参数。 If these parameters are False then your model behaves like many to one . 如果这些参数为False,则您的模型的行为就像一对一 (ie.., the output shape is (batch_size, unit_length). In this case, unit length is an output). (即,输出形状为(batch_size,unit_length)。在这种情况下,单位长度为输出)。 To make a model many to many then the output should be like (batch_size, time_step, unit_length) . 要使模型many to many则输出应类似于(batch_size,time_step,unit_length) Just initialize the stateful==True will help you to solve this problem. 只需初始化stateful==True将帮助您解决此问题。

some of the helpful links to understand about sequence output data 一些有用的链接,以了解有关序列输出数据的信息
Understanding LSTM 了解LSTM
Feeding one prediction after another 一个又一个地提供预测

check the conv1D initialization official documentation and set the parameter stateful=True . 查看conv1D初始化官方文档并设置参数stateful = True This is the Theoretical Idea behind LSTM or Conv1D. 这是LSTM或Conv1D背后的理论思想。 This links will help you to get architecture Idea behind LSTM's. 此链接将帮助您使LSTM背后的体系结构想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM