我目前正在尝试训练一个cnn网络,该网络有72个输入,一个输出是长度为24的向量,表示每个第三个输入的a类为72/24 = 3.有145个类。
这是一个随机生成数据集的类似示例 - 在模型函数内部执行预处理步骤,这是必要的,因为列车输入形状不相同,但为了这个例子的容易性,形状保持一致。
# Creating train input:
for i in range(974):
nano_train_input.append(np.random.random((78,684,4)))
nano_train_output.append(np.random.randint(145,size=(228)).tolist())
# Creating test input:
for i in range(104):
nano_test_input.append(np.random.random((78,684,4)))
nano_test_output.append(np.random.randint(145,size=(228)).tolist())
def model(train_input, train_output, test_input, test_output, names=0):
print "Type:"
print type(train_input)
print type(train_output)
print type(test_input)
print type(test_output)
print
print "len:"
print len(train_input)
print len(train_output)
print len(test_input)
print len(test_output)
print
print "Type[0]:"
print type(train_input[0])
print type(train_output[0])
print type(test_input[0])
print type(test_output[0])
print
print "shape of [0]:"
print train_input[0].shape
print len(train_output[0])
print test_input[0].shape
print len(test_output[0])
raw_input("sad...")
#data_train_input = np.empty((0,72,78,3))
data_train_input = []
#data_test_input = np.empty((0,72,78,3))
data_test_input = []
# Paper uses dimension (40 x 45 =(15 * 3))
# Filter size 5
# Pooling size
# I use dimension (78 x 72 = (24 * 3)
# Filter size 9
print "In model"
i = 0
print_once = True
data_train_output = []
for matrix in train_input:
row,col,color = matrix.shape
#out = np.empty((0,78,3))
out = []
remove = (col/3)%24
remove_col = col%72
#print "Col: " + str(col)
#print "Removed output: " + str(remove)
#print "Removed input: " + str(remove_col)
#print "Before:"
#print len(train_output[i])
if remove > 0:
train_output[i] = train_output[i][:-(remove)]
# print "After:"
# print len(train_output[i])
#else:
#print "After:"
#print len(train_output[i])
#raw_input("ASD")
for splits in chunks(train_output[i],24):
#print len(splits)
#print np.array(splits).shape
#data_train_output = np.append(data_train_output,[np.array(splits)],axis=0)
if len(splits) == 24:
data_train_output.append(np.array(splits))
i=i+1
for feature_map in xrange((col-remove_col)):
#out = np.append(out,[matrix[:,feature_map,:-1]],axis=0)
out.append(matrix[:,feature_map,:-1])
#print out.shape
if feature_map%72 == 0:
#data_train_input = np.append(data_train_input,[out],axis=0)
data_train_input.append(np.array(out))
del out
out = []
#print "Length of train-input: " + str(len(data_train_input))
#print "Length of train-output: " + str(len(data_train_output))
#raw_input("ASdsad")
print "done train-pit"
print "After train_output/input"
i = 0
data_test_output = []
for matrix in test_input:
row,col,color = matrix.shape
out = []
remove = (col/3)%24
remove_col = col%72
#print "Col: " + str(col)
#print "Removed output: " + str(remove)
#print "Removed input: " + str(remove_col)
#print "Before:"
#print len(test_output[i])
if remove > 0:
test_output[i] = test_output[i][:-(remove)]
# print "After:"
# print len(test_output[i])
#else:
# print "After - Not remove:"
# print len(test_output[i])
#raw_input("ASD")
for splits in chunks(test_output[i],24):
if len(splits) == 24:
data_test_output.append(np.array(splits))
i=i+1
for feature_map in xrange((col-remove_col)): #out = np.append(out,[matrix[:,feature_map,:-1]],axis=0)
out.append(matrix[:,feature_map,:-1])
if feature_map%72 == 0:
data_test_input.append(np.array(out)) #Maybe a reshape
del out
out = []
#print "Length of train-input: " + str(len(data_test_input))
#print "Length of train-output: " + str(len(data_test_output))
#raw_input("ASdsad")
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 "asarray - type: "
print type(np.asarray(data_train_input))
print type(np.asarray(data_train_output))
print "asarray - shape: "
print np.asarray(data_train_input).shape
print np.asarray(data_train_output).shape
print "asarray [0]- shape: "
print np.asarray(data_train_input)[0].shape
print np.asarray(data_train_output)[0].shape
#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 = []
print model.summary()
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.asarray(data_train_output),
shuffle=False,
validation_data=([np.array(list_test_input[i]) for i in range(72)],np.asarray(data_test_output)),
validation_split=0.1,
epochs=150000,
verbose=1,
callbacks=[reduce_lr,stop])
del test_input_data
del test_name
del test_input_data_normalized
del test_name_normalized
del test_input_data_interweawed
del test_name_interweawed
#del test_input_data_interweawed_normalized
#del test_name_interweawed_normalized
del train_input_data
del train_name
del train_input_data_normalized
del train_name_normalized
del train_input_data_interweawed
del train_name_interweawed
#del train_input_data_interweawed_normalized
#del train_name_interweawed_normalized
model(nano_train_input,nano_train_output,nano_test_input, nano_test_output)
错误讯息:
Traceback (most recent call last):
File "keras_cnn_phoneme_classification.py", line 456, in <module>
model(nano_train_input,nano_train_output,nano_test_input, nano_test_output)
File "keras_cnn_phoneme_classification.py", line 435, 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 (8766, 24)
为什么我收到此错误?
答案 0 :(得分:1)
一些评论:
len(data_train_input) = 9436
和len(data_train_output) = 9417
?输入多于输出...... dense3 = Dense(1 ,activation = 'softmax')(dense2)
:这有两个原因没有意义,首先是激活:softmax将规范化输出,使它们总和为1,如果你只有一个输出,它将始终由于激活而变为1。第二个原因,输出的数量:我看到你的目标的形状是(24,)...为什么你只输出你最后一个密集的1个值?如果您尝试在24个班级之间进行分类,请将最后一个图层更改为:
dense3 =密集(24,激活='softmax')(密集2)
修复后可能会出现更多错误,但只是通过查看代码我无法一次调试。
我希望这有点帮助。