将conv层转换为fc层,反之亦然

时间:2018-10-11 16:06:45

标签: tensorflow machine-learning keras computer-vision classification

我只是想知道是否可以将转换层转换为完全连接的层,然后返回到转换层?

1 个答案:

答案 0 :(得分:0)

仅需确保输入的形状正确即可。我认为您正在使用keras。

from tensorflow.keras.layers import Dense, Flatten, Conv2D, Reshape

# Add a convolution to the network (previous layer called some_input)

c1 = Conv2D(32, (3, 3), activation='relu', name='first_conv')(some_input)

# Now reshape using 'Flatten'
f1 = Flatten(name='flat_c1')(c1)

# Now add a dense layer with 10 nodes
dense1 = Dense(10, activation='relu', name='dense1')(f1)

# Now add a dense layer, making sure it has the right number of nodes for my next conreshape8v layer.
dense2 = Dense(784, activation='relu', name='dense2')(dense1)
reshape2 = Reshape((7, 7, 16), name='reshape2')(dense2)

#Now back to convolutions (up or down)
c2 = Conv2D(16, kernel_size=(3, 3), activation='relu', 
                      name='conv2')(reshape2)