user9799714
user9799714

Reputation: 75

Using conv2d in tensorflow

I want to use conv2d in tensorflow, The code as follows:

import tensorflow as tf
filter_num = 3
filter_size = 5
char_embed_size = 300
max_word_length = 1
max_seq_length = 15
embeddings = tf.Variable(tf.random_normal([512,14,300]))
#cnn_inputs = tf.reshape(word_embeddings, (-1, max_word_length, char_embed_size, 1))
cnn_inputs = tf.reshape(embeddings, (-1, max_word_length, char_embed_size, 1))
filter_shape = [filter_size, char_embed_size, 1, filter_num]
w = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="cnn_w")
b = tf.Variable(tf.constant(0.1, shape=[filter_num]), name="cnn_b")
conv = tf.nn.conv2d(
    cnn_inputs,
    w,
    strides=[1, 1, 1, 1],
    padding="VALID")

when I run it,it occurs the errors as follows:

ValueError: Negative dimension size caused by subtracting 5 from 1 for 'Conv2D' (op: 'Conv2D') with input shapes: [7168,1,300,1], [5,300,1,3]

It seems the input shape does not match, how do I solve the problem?

Upvotes: 0

Views: 357

Answers (1)

NShiny
NShiny

Reputation: 1156

TL;DR answer.

Use padding="SAME":

conv = tf.nn.conv2d(
    cnn_inputs,
    w,
    strides=[1, 1, 1, 1],
    padding="SAME") # old value is padding="VALID"

Detailed answer.

According to TF documentation input tensor (cnn_inputs) should be of shape [batch, in_height, in_width, in_channels] and kernel tensor (w in your example) should be of shape [filter_height, filter_width, in_channels, out_channels]

In your sample:

  • cnn_input.shape is [7168, 1, 300, 1] therefore in_height == 1 and in_width = 300
  • w.shape is [5, 300, 1, 3], therefore filter_height == 5 and filter_width == 300

If padding="VALID" and stride=[1, 1, 1, 1] then conv2d operation will "shrink" input tensor in spatial dimension by substracting filter_size in spatial dimension. For example if in_height == 20, and filter_height == 4 then output tensor height would probably be 20 - 4 = 16. In your sample with in_height == 1 and filter_height == 5 the shape of output tensor along the height dimension is approximately in_height - filter_height = 1 - 5 = -4, i.e. you recieve tensor with negative height which is not possible and that causes the error.

With padding="SAME" the conv2d operation tries to preserve spatial dimensions by adding zero values (the process is called "zero padding"). Therefore the height of output tensor stays the same with in_height

You can find more detailed explanation for padding here: What is the difference between 'SAME' and 'VALID' padding in tf.nn.max_pool of tensorflow?

Upvotes: 1

Related Questions