tf.layers.conv1d函数解析(一维卷积)

2019-04-03  本文已影响0人  233彭于晏

1 功能

一维卷积一般用于处理文本数据,常用语自然语言处理中,输入一般是文本经过embedding的二维数据。

2 定义

tf.layers.conv1d(
inputs,
filters,
kernel_size,
strides=1,
padding='valid',
data_format='channels_last',
dilation_rate=1,
activation=None,
use_bias=True,
kernel_initializer=None,
bias_initializer=tf.zeros_initializer(),
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
trainable=True,
name=None,
reuse=None
)

3 参数

重要参数介绍:

4 举例

# coding: utf-8
import tensorflow as tf

num_filters = 2
kernel_size = 2
batch_size = 1
seq_length = 4
embedding_dim = 5

embedding_inputs = tf.constant(-1.0, shape=[batch_size, seq_length, embedding_dim], dtype=tf.float32)

with tf.name_scope("cnn"):
    conv = tf.layers.conv1d(embedding_inputs, num_filters, kernel_size, name='conv')

session = tf.Session()
session.run(tf.global_variables_initializer())

print (session.run(conv).shape)

输出为(1, 3, 2)

上一篇下一篇

猜你喜欢

热点阅读