用于3D张量时间序列输入的二进制分类的Keras神经网络模型

时间:2018-08-29 09:22:48

标签: python tensorflow machine-learning neural-network keras

我正在使用keras和tensorflow建立神经网络模型以进行时间序列二进制分类。这是我的输入的样子,形状为(124,4,591):

      |      Col 1      |    Col 2        |    Col 3        |    Col 4        |
Row 1 | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] |
Row 2 | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] |
Row 3 | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] |

我将数据分为X_trainX_testy_trainy_test。我还使用['True', 'False'][0, 1]将标签从LabelEncoder()编码为OneHotEncoder()

x = np.stack((np.vstack(x[:,0]),np.vstack(x[:,1]),np.vstack(x[:,2]),np.vstack(x[:,3])))
x = x.reshape((124,4,591))
y = table_raw_ptpt['Binding Known']
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 0)

X_train.shape返回(86,4,591)。 标签编码:

label_encoder = LabelEncoder()
integer_encoded_train = label_encoder.fit_transform(array(y_train))
integer_encoded_test = label_encoder.fit_transform(array(y_test))

onehot_encoded_y_train = OneHotEncoder(sparse=False)
integer_encoded_train = integer_encoded_train.reshape(len(integer_encoded_train), 1)
onehot_encoded_y_train = onehot_encoded_y_train.fit_transform(integer_encoded_train)

onehot_encoded_y_test = OneHotEncoder(sparse=False)
integer_encoded_test = integer_encoded_test.reshape(len(integer_encoded_test), 1)
onehot_encoded_y_test = onehot_encoded_y_test.fit_transform(integer_encoded_test)

onehot_encoded_y_train.shape返回(86,2)

这是我的NN:

model = Sequential()
model.add(Dense(86, activation='relu', input_shape=(4,591)))
model.add(Dense(43, activation='relu'))
model.add(Dense(20, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss = 'binary_crossentropy',
              optimizer = 'adam',
              metrics = ['accuracy'])
model.summary()

有效。但是当我尝试安装X_train时,我得到一个错误:

  

检查目标时出错:预期density_227具有形状(1,),但   得到形状为(2,)的阵列

提到的层是输出层。据我了解,输出形状不正确。我尝试在各层之间使用Flattern(),甚至尝试使用Reshape(1,)。但是由于我是初学者,所以我不完全了解为获得所需的输出而必须添加哪些内容以控制NN中的数据形状。

我设法使其与softmax一起使用,但是我也需要sigmoid才能起作用,以便以后可以进行最终预测(真或假/ 1或0)。

谢谢。

1 个答案:

答案 0 :(得分:0)

问题在于您如何处理地面真实数据y。以下模型正在处理具有真实数据的随机数据,您必须将y编码为具有4个值的 one ,并为该一次编码分配地面真值向量{{1} }的形状必须为(124, 4 ,1),请注意 4 (我在这里使用了124个样本)

因此y必须具有4个维度y。下面显示了如何将成对的地面真理逻辑值y1, y2, y3, y4编码并分配给4维目标向量00, 01, 10,11

y

这种逻辑值的处理可以用 Karnaugh-Veitch映射https://en.wikipedia.org/wiki/Karnaugh_map)来说明,映射中的每个平方等效于一个单编码的逻辑值。

您必须编写一个函数,将逻辑值0 0 -> 0001 -> y1=0, y2=0, y3=0, y4=1 0 1 -> 0010 -> y1=0, y2=0, y3=1, y4=0 1 0 -> 0100 -> y1=0, y2=1, y3=0, y4=0 1 1 -> 1000 -> y1=1, y2=0, y3=0, y4=0 的组合转换为像上面一样第4维的一键编码特征 vector

00,01,10,11像这样编码,可以像这样构建模型

y

关键是from random import randint from random import seed import math import numpy as np from keras.models import Sequential from keras.layers import Dense # generating random data, replace this with your data X = np.random.rand(124,4,591) y = np.random.randint(2,size=(124,4)) # replace this with your ground truth feature vector y=y.reshape(124,4,1) # https://stackoverflow.com/questions/44704435/error-when-checking-model-input-expected-lstm-1-input-to-have-3-dimensions-but model = Sequential() model.add(Dense(86, activation='relu', input_shape=(4,591))) model.add(Dense(43, activation='relu')) model.add(Dense(20, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() model.fit(X, y, epochs=1, batch_size=10) 具有与y相同的第二维,此处为4,这可以通过像上面那样编码基本事实的逻辑值来实现

在上面的代码中没有这样做,因为我不知道您的数据,在代码中使用了正确维数的随机数据