tensorflow中的1维卷积-备忘

tensorflow中1维卷积的官方文档:https://tensorflow.google.cn/api_docs/python/tf/nn/conv1d

tf.nn.conv1d(
    value,
    filters,
    stride,
    padding,
    use_cudnn_on_gpu=None,
    data_format=None,
    name=None
)

其中:

  • value: A 3D Tensor. Must be of type float16 or float32.
  • filters: A 3D Tensor. Must have the same type as value.
  • stride: An integer. The number of entries by which the filter is moved right at each step.
  • padding: 'SAME' or 'VALID'
  • name: A name for the operation (optional)
  • value: A 3D Tensor. Must be of type float16 or float32.
  • filters: A 3D Tensor. Must have the same type as value.
  • stride: An integer. The number of entries by which the filter is moved right at each step.
  • padding: 'SAME' or 'VALID' ; SAME表示加入padding尺寸不变。VALID表示不加padding尺寸可能变小.

tensorflow  1D 卷积实例:

import tensorflow as tf
import numpy as np

import os

word_mat = tf.constant([
                        [[1,2,2],[4,1,6],[7,3,9]],
                        [[1,2,3],[4,5,6],[7,8,9]],
                        [[1,3,3],[7,5,6],[2,8,9]]
                       ],dtype=tf.float32)

filter_shape = [2,3,4]
kernel_ = tf.get_variable("kernel_",
                          filter_shape,
                          dtype=tf.float32,
                          initializer=tf.ones_initializer())
o = tf.nn.conv1d(word_mat,kernel_,1,"VALID")

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(o))

输入:

[[[16. 16. 16. 16.]
  [30. 30. 30. 30.]]

 [[21. 21. 21. 21.]
  [39. 39. 39. 39.]]

 [[25. 25. 25. 25.]
  [37. 37. 37. 37.]]]

注意:

1. tensorflow的大部分卷积都是从【axis0  × axis1】这个面开始卷的

2. 由于是一维卷积,batch中每一个样本的维度是2,卷积核的参数是3个,最后一个表示out_channel,倒数第二个表示in_channel,第一个表示卷积的感受视野。

关于二维,三维卷积。后面需要的话会持续更新

猜你喜欢

转载自blog.csdn.net/biubiubiu888/article/details/82017430