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 3DTensor
. Must be of typefloat16
orfloat32
.filters
: A 3DTensor
. Must have the same type asvalue
.stride
: Aninteger
. 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)- 注意:filter shape的第二个参数必须等于value shape的第三个参数
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,第一个表示卷积的感受视野。
关于二维,三维卷积。后面需要的话会持续更新