Basic 1d convolution in tensorflow

后端 未结 3 1030
甜味超标
甜味超标 2020-12-13 06:59

OK, I\'d like to do a 1-dimensional convolution of time series data in Tensorflow. This is apparently supported using tf.nn.conv2d, according to these tickets,

3条回答
  •  隐瞒了意图╮
    2020-12-13 07:21

    I am sorry to say that, but your first code was almost right. You just inverted x and phi in tf.nn.conv2d:

    g = tf.Graph()
    with g.as_default():
        # data shape is "[batch, in_height, in_width, in_channels]",
        x = tf.Variable(np.array([0.0, 0.0, 0.0, 0.0, 1.0]).reshape(1, 1, 5, 1), name="x")
        # filter shape is "[filter_height, filter_width, in_channels, out_channels]"
        phi = tf.Variable(np.array([0.0, 0.5, 1.0]).reshape(1, 3, 1, 1), name="phi")
        conv = tf.nn.conv2d(
            x,
            phi,
            strides=[1, 1, 1, 1],
            padding="SAME",
            name="conv")
    

    Update: TensorFlow now supports 1D convolution since version r0.11, using tf.nn.conv1d. I previously made a guide to use them in the stackoverflow documentation (now extinct) that I'm pasting here:


    Guide to 1D convolution

    Consider a basic example with an input of length 10, and dimension 16. The batch size is 32. We therefore have a placeholder with input shape [batch_size, 10, 16].

    batch_size = 32
    x = tf.placeholder(tf.float32, [batch_size, 10, 16])
    

    We then create a filter with width 3, and we take 16 channels as input, and output also 16 channels.

    filter = tf.zeros([3, 16, 16])  # these should be real values, not 0
    

    Finally we apply tf.nn.conv1d with a stride and a padding: - stride: integer s - padding: this works like in 2D, you can choose between SAME and VALID. SAME will output the same input length, while VALID will not add zero padding.

    For our example we take a stride of 2, and a valid padding.

    output = tf.nn.conv1d(x, filter, stride=2, padding="VALID")
    

    The output shape should be [batch_size, 4, 16].
    With padding="SAME", we would have had an output shape of [batch_size, 5, 16].

提交回复
热议问题