Keras和张量流连接和拟合错误
Posted
技术标签:
【中文标题】Keras和张量流连接和拟合错误【英文标题】:Keras and tensorflow concatenation and fitting error 【发布时间】:2018-12-03 19:02:51 【问题描述】:我在调整以下模型时遇到了一些问题。我正在尝试使用 keras 和 tensorflow 训练模型来对雷达发射器的时间序列进行分类。它给出的错误是:
ValueError: 检查目标时出错:预期 concatenate_1 的形状为 (2,),但得到的数组的形状为 (1,)
似乎我无法调整标签的正确尺寸(我尝试过使用分类标签(我必须分类但它输出为 3,为什么?)并在 fit 调用中使用 [y_train,y_train] ,但一点运气都没有。也许错误来自不同的部分,我看不到它。
def get_model_lstm_fcn(shape):
main_input = Input(shape=shape, dtype='float32', name='main_input')
lstm_out = LSTM(256, dtype=float)(main_input)
# lstm_out = (LSTM(128, dtype=float))(lstm_out)
lstm_out = (Dropout(0.25))(lstm_out)
auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)
auxiliary_input = Input(shape=shape, name='aux_input')
conv = Convolution1D(128, 10, input_shape=shape, activation='relu')(auxiliary_input)
conv = (Convolution1D(256, 10, activation='relu'))(conv)
conv = (Convolution1D(128, 10, activation='relu'))(conv)
conv = (GlobalAveragePooling1D())(conv)
main_output = Dense(1, activation='sigmoid', name='main_output')(conv)
concatenation = concatenate([auxiliary_output, main_output])
model = Model(inputs=[main_input, auxiliary_input], outputs=concatenation)
model.summary()
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
return model
if __name__ == "__main__":
x_train = load_data("D:\\Software_Herramienta\\Datasets\\sweep_switch_train.csv")
y_train = load_data("D:\\Software_Herramienta\\Datasets\\sweep_switch_labels_train.csv")
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
y_train.astype(int)
y_train = y_train.reshape(1000, 1)
# y_train = to_categorical(y_train)
batch = 50
model = get_model_lstm_fcn(x_train.shape[1:])
model.fit([x_train,x_train], y_train, epochs=5, batch_size=batch)
x_test = load_data("D:\\Software_Herramienta\\Datasets\\sweep_switch_test.csv")
y_test = load_data("D:\\Software_Herramienta\\Datasets\\sweep_switch_labels_test.csv")
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], 1)
y_test.astype(int)
y_test = y_test.reshape(1000, 1)
y_test = to_categorical(y_test)
loss_and_metrics = model.evaluate(x_test, y_test, batch_size=batch)
classes = model.predict(x_test, batch_size=batch)
print("Loss, accuracy: ")
print(loss_and_metrics)
print("Classes:")
print(classes.flatten())
型号汇总为:
Layer(type) ------------------- 输出形状 -------- Param #----连接到
aux_input (InputLayer) -------- (None, 1000, 1) ----- 0 ----------
conv1d_1 (Conv1D) -------------- (None, 991, 128) --- 1408 ----- aux_input[0][0]
main_input (InputLayer) --------- (None, 1000, 1) ---- 0
conv1d_2 (Conv1D) -------------- (None, 982, 256) ---- 327936 ----- conv1d_1[0][0]
lstm_1 (LSTM) ------- (无,256) ------- 264192 ---- main_input[0][0]
conv1d_3 (Conv1D) --------------- (None, 973, 128) --- 327808 ---- conv1d_2[0][0]
dropout_1(丢弃)-------------(无,256)-------- 0 --------- lstm_1[0][0]
global_average_pooling1d_1 ------ (None, 128) -------- 0 --------- conv1d_3[0][0]
aux_output (Dense) --------------- (None, 1) ------ 257 --------- dropout_1[0][0]
main_output (Dense) --------------- (None, 1) ---- 129 ---------global_average_pooling1d_1[0][0]
concatenate_1 (连接) ------- (None, 2) ------ 0 --------- aux_output[0][0] main_output[0][0]
总参数:921,730 可训练参数:921,730 不可训练参数:0
感谢您的帮助!
【问题讨论】:
我会通过打印auxiliary_output
和main_output
的形状来开始调试。另外,我想知道您的 concatenate
函数是做什么的。如果您提供完整的堆栈跟踪也会有所帮助。
【参考方案1】:
事实证明错误来自标签,正如我最初所想的那样。但是,我没有使用正确的形状,所以我猜问题出在其他地方。 由于有两个输入,因此标签也应该有两个输入,所以我最初使用的是:
model.fit([x_train, x_train], [y_train, y_train], epochs=5, batch_size=batch)
但这不正确,因为标签的形状在两个不同的数组中而不是在一个数组中。所以正确的做法是:
model.fit([x_train, x_train], np.reshape([y_train, y_train], (1000, 2)), epochs=5, batch_size=batch)
将 np 数组重塑为一个只有 2 列的数组,重复标签。
其实evaluate函数也出现了同样的问题,应该是这样的:
loss_and_metrics = model.evaluate([x_test, x_test], np.reshape([y_test, y_test]), batch_size=batch)
干杯
【讨论】:
以上是关于Keras和张量流连接和拟合错误的主要内容,如果未能解决你的问题,请参考以下文章