如何将输入/输出数据解析为keras model.fit?

时间:2017-04-03 01:16:15

标签: python arrays list numpy keras

鉴于CNN如何格式化我的输入和输出数据。

输入目前存储为带有形状的numpy.ndarray列表(72,78,3)

CNN有72个输入通道=>每行是特定输入的输入向量。

并且输出是类的向量,因此每(72x78)输入应该是输出是类的向量,数组的大小= 24。它目前存储为形状为24的numpy.arrays列表。

我现在的问题是将输出数据解析为我的model.fit。

我的网络是这样的:

print "After test_output/input"

print "Length:"
print len(data_train_input)
print len(data_train_output)


print len(data_test_input)
print len(data_test_output)


print "Type;"
print type(data_train_input[0])
print type(data_train_output[0])


print "Size [0]"
print data_train_input[0].shape
print data_train_output[0].shape


print "type: "
print type(np.asarray(data_train_output))
print "len: "
print len(np.asarray(data_train_output))


raw_input("Ok?...")


list_of_input = [Input(shape = (78,3)) for i in range(72)]
list_of_conv_output = []
list_of_max_out = []
#list_of_flatten_out = []
for i in range(72):
    list_of_conv_output.append(Conv1D(filters = 32 , kernel_size = 6 , padding = "same", activation = 'relu')(list_of_input[i]))
    list_of_max_out.append(MaxPooling1D(pool_size=3)(list_of_conv_output[i]))
    #list_of_flatten_out.append(Flatten()(list_of_max_out[i]))


merge = keras.layers.concatenate(list_of_max_out)


reshape = Reshape((-1,))(merge)


dense1 = Dense(units = 500, activation = 'relu',    name = "dense_1")(reshape)
dense2 = Dense(units = 250, activation = 'relu',    name = "dense_2")(dense1)
dense3 = Dense(units = 24 , activation = 'softmax', name = "dense_3")(dense2)


model = Model(inputs = list_of_input , outputs = dense3)
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam" , metrics = [metrics.sparse_categorical_accuracy])


reduce_lr=ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, mode='auto', epsilon=0.01, cooldown=0, min_lr=0.000000000000000000001)
stop  = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')


print "Train!"
list_train_input = []
list_test_input = []


for i in range(len(data_train_input)):
    list_train_input.append(data_train_input[i])


for i in range(len(data_test_input)):
    list_test_input.append(data_test_input[i])


hist_current = model.fit(x = [np.array(list_train_input[i]) for i in range(72)],
                    y = np.array(data_train_output),
                    shuffle=False,
                    validation_data=([np.array(list_test_input[i]) for i in range(72)],np.array(data_test_output)),
                    validation_split=0.1,
                    epochs=150000,
                    verbose=1,
                    callbacks=[reduce_lr,stop])

这个输出是:

https://pastebin.com/R3XwSnK6

错误信息是:

Traceback (most recent call last):
  File "keras_cnn_phoneme_classification.py", line 407, in <module>
    model(train_input_data_interweawed_normalized, output_train, test_input_data_interweawed_normalized, output_test, test_name_interweawed_normalized)
  File "keras_cnn_phoneme_classification.py", line 386, in model
    callbacks=[reduce_lr,stop])
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1405, in fit
    batch_size=batch_size)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1299, in _standardize_user_data
    exception_prefix='model target')
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 133, in _standardize_input_data
    str(array.shape))
ValueError: Error when checking model target: expected dense_3 to have shape (None, 1) but got array with shape (10410, 24)

由于某种原因,它预期形状(无,1),但在dense_3中定义的输出维数是24?那为什么这不正确?

我将输出数据作为numpy数组列表传递。

0 个答案:

没有答案