【问题标题】:Implementing convolutional layers using Tensorflow使用 Tensorflow 实现卷积层
【发布时间】:2018-02-11 05:31:22
【问题描述】:

我正在尝试通过此blog post 实现用于文本分类的卷积层,并进行一些修改以满足我的需要。

在博客中,只有一个卷积层,而我希望我的有两个卷积层,然后是 ReLU 和 max-pooling。

目前的代码是:

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")

这里,W是过滤矩阵。

但是,这会产生错误:

ValueError:维度必须相等,但对于输入形状为 [?,46,1,256]、[5,100,1,256] 的“convolution-block_16/conv2”(操作:“Conv2D”),维度必须是 256 和 1。

我意识到我在图层的尺寸上有错误,但我无法修复它或放入正确的尺寸。

如果有人可以提供任何指导/帮助,那将非常有帮助。

谢谢。

【问题讨论】:

    标签: tensorflow conv-neural-network


    【解决方案1】:

    不太明白你的代码是做什么的,但如下更改将解决你的问题。

    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") 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 2017-10-21
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      相关资源
      最近更新 更多