O'Reilly logo

Hands-On Natural Language Processing with Python by Rajalingappaa Shanmugamani, Rajesh Arumugam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Modeling with CNN

Next, we will build a character based CNN model. We will start by creating embedding lookup with dimensions a number of characters and 50. First, the character set length is obtained from the character map:

def character_CNN(tf_char_map, char_map, char_embed_dim=50):    char_set_len = len(char_map.keys())

Each convolution layer is initiated with a random variable for weights and biases. The convolution layer function will be called when the model is constructed:

def conv2d(x, W, b, strides=1):        x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1],              padding="SAME")        x = tf.nn.bias_add(x, b)        return tf.nn.relu(x)

Each layer is also followed by a max pooling layer, defined as follows:

def maxpool2d(x, k=2):        return tf.nn.max_pool(x ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required