2017-09-02 3 views
0

Ich versuche, Faltungsschichten für die Textklassifizierung von diesem blog post mit einigen Modifikationen zu implementieren, um meine Bedürfnisse zu erfüllen.Implementierung von Faltungsschichten mit Tensorflow

Im Blog gibt es nur eine Faltungsschicht, während ich meins zwei Faltungsschichten haben sollte, gefolgt von ReLU und Max-Pooling.

Der Code so weit ist:

vocab_size = 2000 
embedding_size = 100 
filter_height = 5 
filter_width = embedding_size 
no_of_channels = 1 
no_of_filters = 256 
sequence_length = 50 
filter_size = 3 
no_of_classes = 26 


input_x = tf.placeholder(tf.int32, [None, sequence_length], name="input_x") 
input_y = tf.placeholder(tf.float32, [None, no_of_classes], name="input_y") 



# Defining the embedding layer: 

with tf.device('/cpu:0'), tf.name_scope("embedding"): 
    W = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0), name="W") 
    embedded_chars = tf.nn.embedding_lookup(W, input_x) 
    embedded_chars_expanded = tf.expand_dims(embedded_chars, -1) 


# Convolution block: 

with tf.name_scope("convolution-block"): 
    filter_shape = [filter_height, embedding_size, no_of_channels, no_of_filters] 
    W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W") 
    b = tf.Variable(tf.constant(0.1, shape=[no_of_filters]), name="b") 

    conv1 = tf.nn.conv2d(embedded_chars_expanded, 
        W, 
        strides = [1,1,1,1], 
        padding = "VALID", 
        name = "conv1") 

    conv2 = tf.nn.conv2d(conv1, 
        W, 
        strides = [1,1,1,1], 
        padding = "VALID", 
        name = "conv2") 

Hier ist W die Filtermatrix.

jedoch gibt dies den Fehler:

ValueError: Dimensions must be equal, but are 256 and 1 for 'convolution-block_16/conv2' (op: 'Conv2D') with input shapes: [?,46,1,256], [5,100,1,256].

Ich weiß, ich habe in den Dimensionen der Schicht geirrt, aber ich bin nicht in der Lage, es zu beheben oder in den richtigen Dimensionen setzen.

Wenn jemand irgendeine Anleitung/Hilfe zur Verfügung stellen könnte, wäre es sehr hilfreich.

Vielen Dank.

Antwort

1

Kann nicht ganz verstehen, was Sie tun, aber ändern Sie wie folgt Ihr Problem beheben wird.

with tf.name_scope("convolution-block"): 
    filter_shape = [filter_height, embedding_size, no_of_channels, no_of_channels #change the output channel as input#] 
    W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W") 
    b = tf.Variable(tf.constant(0.1, shape=[no_of_filters]), name="b") 

    conv1 = tf.nn.conv2d(embedded_chars_expanded, 
        W, 
        strides = [1,1,1,1], 
        padding = "SAME", ##Change the padding scheme 
        name = "conv1") 

    conv2 = tf.nn.conv2d(conv1, 
        W, 
        strides = [1,1,1,1], 
        padding = "VALID", 
        name = "conv2") 
Verwandte Themen