Open QGB opened 5 years ago
你可以使用最新的API:https://keras.io/layers/wrappers/#bidirectional
比如:Bidirectional(GRU(128), merge_mode='sum')
2019 更新的代码已经使用了新版 API:
input_tensor = Input((height, width, 3))
x = input_tensor
for i, n_cnn in enumerate([2, 2, 2, 2, 2]):
for j in range(n_cnn):
x = Conv2D(32*2**min(i, 3), kernel_size=3, padding='same', kernel_initializer='he_uniform')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D(2 if i < 3 else (2, 1))(x)
x = Permute((2, 1, 3))(x)
x = TimeDistributed(Flatten())(x)
rnn_size = 128
x = Bidirectional(CuDNNGRU(rnn_size, return_sequences=True))(x)
x = Bidirectional(CuDNNGRU(rnn_size, return_sequences=True))(x)
x = Dense(n_class, activation='softmax')(x)
base_model = Model(inputs=input_tensor, outputs=x)
首先模型输入一个 (height, width, 3)
维度的图片,然后经过一系列的层降维到了 (2, 16, 256)
,之后我们使用 Permute 把 width 轴调整到第一个维度以适配 RNN 的输入格式。调整以后的维度是 (16, 2, 256)
,然后使用 TimeDistributed(Flatten())
把后两个维度压成一维,也就是 (16, 512)
,之后经过 2 层双向的 GRU 对序列横向建模,最后经过 Dense 分类器输出水平方向上每个字符的概率分布 (16, 37)
。
WARNING:tensorflow:From /home/qgb/.local/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. /root/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: UserWarning: Update your
Conv2D
call to the Keras 2 API:Conv2D(32, (3, 3), activation="relu")
/root/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:9: UserWarning: Update your
Conv2D
call to the Keras 2 API:Conv2D(32, (3, 3), activation="relu")
if name == 'main': /root/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:17: UserWarning: Update yourGRU
call to the Keras 2 API:GRU(128, return_sequences=True, name="gru1", kernel_initializer="he_normal")
/root/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:18: UserWarning: Update yourGRU
call to the Keras 2 API:GRU(128, return_sequences=True, go_backwards=True, name="gru1_b", kernel_initializer="he_normal")
TypeError Traceback (most recent call last)