Keras non Sequential,尺寸问题和重塑问题

时间:2017-06-22 10:16:06

标签: python machine-learning deep-learning keras

我正在尝试对这个例子中的那个进行类似的架构:
https://github.com/fchollet/keras/blob/master/examples/image_ocr.py#L480
然而,根据我的数据,我一直存在维度问题,而且我没有找到一个很好的网站来解释使用您自己的数据而不是MNIST或默认数据来控制维度。

上下文:我尝试使用前面提到的架构和文本图像让我说第一次尝试用2000.对于标签,我决定做one_hot编码,这是数据特征:
图像固定形状:(2000,208,352,1)#B& W
one_hot标签大小:(2000,346,1)#2000样本和346个类,最后一个值是有一个3dimensional数组,因为它需要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显然是类的数量,但另一个值让我完全迷失了。

感谢大家阅读我的问题。

1 个答案:

答案 0 :(得分:1)

  1. conv_to_rnn_dims = (104,2816)这是虚构的。据我所知,您正在尝试将CNN输出提供给Dense图层。但最后一层CNN是MaxPooling,它产生2D输出。您应该使用Flatten来建立此连接。我们来看看这个例子。

    model=Sequential()               
    model.add(Conv2D(16,3,3,border_mode="same",input_shape=(208,352,1))
    #Produces 2000 x 208 x 352 x 16
    model.add(Conv2D(32,3,3,activation="tanh",border_mode="valid"))
    #Produces 2000 x 208 x 352 x 32
    model.add(Flatten())
    #Produces 2000 x 2342912
    model.add(Dense(100,activation="sigmoid"))
    #Produces 2000 x 100
    
  2. 这意味着您不需要重塑图层。

    1. 在此Dense之后,您应使用ReshapeGRU准备好输出。现在您有100个时间步长来阅读。所以你应该重塑为model.add(Reshape((100,1))所以网络的结果现在是 2000 x 100 x 1 。您可以安全地将其安装到GRU图层
    2. 最后,对于 One Hot 向量和输出Dense图层的分类问题,您的目标形状应为 2000 x 346 所以最终{{ 1}}层应该有346个节点。