TensorFlow CNN 相关类与方法

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 of ints. 1-D tensor of length 4. The stride of the sliding window for each dimension of input. 一般情况下, strides=[1,1,1,1].
  • padding
    A string 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-D Tensor with shape [batch, height, width, channels] and type tf.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].

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值