embedding
tf.nn.embedding_lookup(params,ids,...)
通过id找对应的tensor.
在 Text-CNN中, 输入的word对应着一个tensor, 这个参数是可学习的, 就会用到这个函数.
示例:
embedding_shape = [vocab_size, embedding_size]
embedding_weight = tf.Variable(
tf.random_uniform(embedding_shape, -1.0, 1.0),
name="W",
dtype=tf.float32)
embedded_chars = tf.nn.embedding_lookup(self.embedding_weight, input_data)
one-hot
tf.one-hot(indices, depth, on_value=None, off_value=None,axis=None, dtype=None, name=None)
即 tensorflow.python.ops.array_ops.one_hot()
.
Returns a one-hot tensor.
conv1d
tf.keras.layers.Conv1D
# inputs [N,15,4]
Conv1D(filters=7, kernel_size=4, activation=tf.nn.leaky_relu)(inputs)
'''
对于以上调用, 构建的 variable 是这样的:
conv1d/kernel:0===(4=1d_kernel_size, 4=input_channel, 7=filters=output_channel)
conv1d/bias:0===(7,)
'''
conv2d
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None,data_format=None, name=None)
即tensorflow.python.ops.gen_nn_ops.conv2d()
,
Computes a 2-D convolution given 4-D input
and filter
tensors. 参数见下:
- input
a tensor of shape[batch, in_height, in_width, in_channels]
- filter
a tensor of shape[filter_height, filter_width, in_channels, out_channels]
- strides
A list ofints
. 1-D tensor of length 4. The stride of the sliding window for each dimension ofinput
. 一般情况下, strides=[1,1,1,1]. - padding
Astring
from:"SAME", "VALID"
.
SAME 对应的就是 zero-padding, 卷积后尺寸不变. VALID 对应的就是不填充, 卷积后尺寸为input_size-filter_size+1
.
Why call it a 2-D convolution, because it flattens the filter to a 2-D matrix with shape [filter_height * filter_width * in_channels, output_channels]
.
max_pool
tf.keras.layers.MaxPooling1D
__init__(self, pool_size=2, strides=None, padding='valid', **kwargs)
构造函数.
tf.nn.max_pool(value, ksize, strides, padding, data_format="NHWC", name=None)
Performs the max pooling on the input. 参数见下:- value
A 4-DTensor
with shape[batch, height, width, channels]
and typetf.float32
. - ksize
A list of ints that has length >= 4. The size of the window for each dimension of the input tensor. - strides
A list of ints that has length >= 4. The stride of the sliding window for each dimension of the input tensor.
一般情况下, strides=[1,ksize[1],ksize[2],1].
- value
fully-connected
使用 tf.matmul()
即可.
argmax
tf.argmax(input,axis=None,name=None,dimension=None,output_type=dtypes.int64)
即tensorflow.python.ops.math_ops.argmax()
.
类似于 numpy.argmax(), it returns the index with the largest value across dimensions of a tensor.