如何添加更多图层到卷积神经网络文本分类TensorFlow示例?

时间:2017-03-01 15:33:27

标签: tensorflow

根据文档,this example中提供的模型类似于以下文章: “Character-level Convolutional Networks for Text Classification

我发现原始模型(在论文中提出)包含9层深,有6个卷积层和3个完全连接层,但实现的例子只包含两个卷积层:

with tf.variable_scope('CNN_Layer1'):
    # Apply Convolution filtering on input sequence.
    conv1 = tf.contrib.layers.convolution2d(
                 byte_list, N_FILTERS, FILTER_SHAPE1, padding='VALID')
    # Add a RELU for non linearity.
    conv1 = tf.nn.relu(conv1)
    # Max pooling across output of Convolution+Relu.
    pool1 = tf.nn.max_pool(
            conv1,
            ksize=[1, POOLING_WINDOW, 1, 1],
            strides=[1, POOLING_STRIDE, 1, 1],
            padding='SAME')
    # Transpose matrix so that n_filters from convolution becomes width.
    pool1 = tf.transpose(pool1, [0, 1, 3, 2])
with tf.variable_scope('CNN_Layer2'):
    # Second level of convolution filtering.
    conv2 = tf.contrib.layers.convolution2d(
                 pool1, N_FILTERS, FILTER_SHAPE2, padding='VALID')
    # Max across each filter to get useful features for classification.
    pool2 = tf.squeeze(tf.reduce_max(conv2, 1), squeeze_dims=[1])

如果有人可以帮我扩展这个模型以获得更多图层?

1 个答案:

答案 0 :(得分:0)

  1. 与BVLC Caffenet相似:

    def bvlc_caffenet(imgs,weights,biases):    #mean减法    mean = tf.constant([123.68,116.779,103.939],dtype = tf.float32,shape = [1,1,1,3],name =' img_mean')    images = imgs-mean

    #CONV1    conv1 = tf.nn.conv2d(图像,权重[' c1'],[1,3,3,1],填充='有效')    out1 = tf.nn.relu(tf.nn.bias_add(conv1,biases [' b1']))    pool1 = tf.nn.max_pool(out1,ksize = [1,3,3,1],strides = [1,2,2,1],padding =' VALID')

    #CONV2    conv2 = tf.nn.conv2d(pool1,weight [' c2'],[1,1,1,1],padding =' VALID')    out2 = tf.nn.relu(tf.nn.bias_add(conv2,偏见[' b2']))    pool2 = tf.nn.max_pool(out2,ksize = [1,3,3,1],strides = [1,2,2,1],padding =' VALID')

    #conv3    conv3 = tf.nn.conv2d(pool2,权重[' c3'],[1,1,1,1],padding =' VALID')    out3 = tf.nn.relu(tf.nn.bias_add(conv3,偏见[' b3']))

    #CONV4    conv4 = tf.nn.conv2d(out3,权重[' c4'],[1,1,1,1],填充='有效')    out4 = tf.nn.relu(tf.nn.bias_add(conv4,偏见[' b4']))

    #conv5    conv5 = tf.nn.conv2d(out4,权重[' c5'],[1,1,1,1],填充='有效')    out5 = tf.nn.relu(tf.nn.bias_add(conv5,偏见[' b5']))    pool5 = tf.nn.max_pool(out5,ksize = [1,3,3,1],strides = [1,2,2,1],padding =' VALID')

    #flattening    shape = int(np.prod(pool5.get_shape()[1:]))    pool5_flat = tf.reshape(pool5,[ - 1,shape])

    #FC6    fc6 = tf.matmul(pool5_flat,权重[' f6'])    out6 = tf.nn.relu(tf.nn.bias_add(fc6,偏见[' b6']))    out6 = tf.nn.dropout(out6,0.5)

    #FC7    fc7 = tf.matmul(out6,权重[' f7'])    out7 = tf.nn.relu(tf.nn.bias_add(fc7,偏见[' b7']))    out7 = tf.nn.dropout(out7,0.5)

    #FC8    fc8 = tf.matmul(out7,权重[' f8'])    out8 = tf.nn.relu(tf.nn.bias_add(fc8,偏见[' b8']))    out8 = tf.nn.dropout(out8,0.5)

    probs = tf.nn.softmax(out8)    返回probs

    网络的初始化权重和偏差

    weights = { ' c1':tf.Variable(tf.truncated_normal([7,7,3,96],stddev = 0.1)), ' c2':tf.Variable(tf.truncated_normal([5,5,96,256],stddev = 0.1)), ' c3':tf.Variable(tf.truncated_normal([3,3,256,384],stddev = 0.1)), ' c4':tf.Variable(tf.truncated_normal([3,3,384,384],stddev = 0.1)), ' c5':tf.Variable(tf.truncated_normal([3,3,384,256],stddev = 0.1)), ' f6':tf.Variable(tf.truncated_normal([4096,2048],stddev = 0.1)), ' f7':tf.Variable(tf.truncated_normal([2048,2048],stddev = 0.1)), ' f8':tf.Variable(tf.truncated_normal([2048,1000],stddev = 0.1)) } 偏见= { ' B1' :tf.Variable(tf.constant(0.1,shape = [96])), ' B2' :tf.Variable(tf.constant(0.1,shape = [256])), ' B3' :tf.Variable(tf.constant(0.1,shape = [384])), ' B4' :tf.Variable(tf.constant(0.1,shape = [384])), ' B5' :tf.Variable(tf.constant(0.1,shape = [256])), ' B6' :tf.Variable(tf.constant(0.1,shape = [2048])), ' B7' :tf.Variable(tf.constant(0.1,shape = [2048])), ' B8' :tf.Variable(tf.constant(0.1,shape = [1000])) }

    1. 遵循此(另一种格式):https://www.cs.toronto.edu/~frossard/vgg16/vgg16.py
    2. 这些有用吗?