我试图做类似的架构的一个在这个例子:
https://github.com/fchollet/keras/blob/master/examples/image_ocr.py#L480
但是我的数据我一直有尺寸的问题,我还没有找到一个很好的网站这解释了用您自己的数据而不是MNIST或默认数据来控制尺寸。Keras非顺序的,尺寸麻烦,重塑
上下文:即时尝试前文提到的图像与文字让我说我第一次尝试与2000.对于标签,我决定做one_hot编码,这是数据特征:
图像固定形状: (2000,208,352,1)#B & W
one_hot标签大小:(2000,346,1)#2000个样本和346个类,最后一个值是具有三维数组,因为它对于softmax显然是需要的
现在的代码:
nb_classes = 346
max_lin, max_col = (208, 352)
input_shape = (max_lin, max_col, 1)
conv_filters = 16
kernel_size = (3, 3)
pool_size = 2
time_dense_size = 32
rnn_size = 512
act = 'relu'
input_data = Input(name='the_input', shape=input_shape)
inner = Conv2D(conv_filters, kernel_size, padding='same',
activation=act, name='CONV2D_1')(input_data)
inner = MaxPooling2D(pool_size=(pool_size, pool_size),
name='MXPOOL2D_1')(inner)
inner = Conv2D(conv_filters, kernel_size, padding='same',
activation=act, name='CONV2D_1')(input_data)
inner = MaxPooling2D(pool_size=(pool_size, pool_size),
name='MXPOOL2D_1')(inner)
#This is my problem, I dont really know how to reshape it with my data,
#I chose (104,2816) because other stuff didnt worked and I found it was
#the Layer Before (104,176,16) = (104, 176*16) = (104,2816); others values
#gives me ValueError: total size of new array must be unchanged
conv_to_rnn_dims = (104,2816)
inner = Reshape(target_shape=conv_to_rnn_dims, name='reshape')(inner)
inner = Dense(time_dense_size, activation=act, name='dense1')(inner)
gru_1 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru1')(inner)
gru_1b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru1_b')(inner)
gru1_merged = add([gru_1, gru_1b])
gru_2 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru2')(gru1_merged)
gru_2b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru2_b')(gru1_merged)
gru_conc = concatenate([gru_2, gru_2b])
print("GruCOnc: ",gru_conc.shape)
inner = Dense(nb_classes, kernel_initializer='he_normal',
name='DENSE_2')(gru_conc)
print("2ndDense: ",inner.shape)
y_pred = Activation('softmax',name='softmax')(inner)
print(y_pred.shape)
model = Model(inputs=input_data, outputs=y_pred)
print(model.summary())
sgd = SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)
model.compile(loss='categorical_crossentropy',optimizer=sgd)
model.fit(train_data, train_label, batch_size=10, epochs=2, verbose=1)
score = model.evaluate(x_test, y_test, verbose=1)
print(score)
和运行代码后,我得到:
ValueError: Error when checking target: expected softmax to have shape (None, 104, 346) but got array with shape (2000, 346, 1)
所以这里的大问题是,什么是104?因为346显然是班级的数量,但另一个值让我完全失去了。
感谢大家阅读我的问题。
好的我明白了,我需要在我的Dense之前展开展平,然后我可以在GRU图层中进行重塑,感谢您的帮助! – alohapinilla