卷积神经网络最简单的例子莫过于对MNIST做分类了。MNIST 每个图片有28乘以28个像素,我们可以用 4D placeholder 作为输入。
x = tf.placeholder(tf.float32, shape=[None, 28, 28, 1]) y_ = tf.placeholder(tf.int64, shape=[None,])
https://github.com/zsdonghao/tunelayer
然后如下使用2D CNN filter 和 pooling 即可。
network = tl.layers.InputLayer(x, name='input_layer') network = tl.layers.Conv2dLayer(network, act = tf.nn.relu, shape = [5, 5, 1, 32], # 32 features for each 5x5 patch strides=[1, 1, 1, 1], padding='SAME', name ='cnn_layer1') # output: (?, 28, 28, 32) network = tl.layers.Pool2dLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool = tf.nn.max_pool, name ='pool_layer1',) # output: (?, 14, 14, 32) network = tl.layers.Conv2dLayer(network, act = tf.nn.relu, shape = [5, 5, 32, 64], # 64 features for each 5x5 patch strides=[1, 1, 1, 1], padding='SAME', name ='cnn_layer2') # output: (?, 14, 14, 64) network = tl.layers.Pool2dLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool = tf.nn.max_pool, name ='pool_layer2',) # output: (?, 7, 7, 64) network = tl.layers.FlattenLayer(network, name='flatten_layer') # output: (?, 3136) network = tl.layers.DropoutLayer(network, keep=0.5, name='drop1') # output: (?, 3136) network = tl.layers.DenseLayer(network, n_units=256, act = tf.nn.relu, name='relu1') # output: (?, 256) network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2') # output: (?, 256) network = tl.layers.DenseLayer(network, n_units=10, act = tl.identity, name='output_layer') # output: (?, 10)