sentiment_model.py 1.74 KB
Newer Older
1
2
3
4
5
6
"""Model for sentiment analysis.

The model makes use of concatenation of two CNN layers with
different kernel sizes.
"""

7
8
9
10
11
12
13
14
15
16
17
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf


class CNN(tf.keras.models.Model):
  """CNN for sentimental analysis."""

  def __init__(self, emb_dim, num_words, sentence_length, hid_dim,
18
               class_dim, dropout_rate):
19
20
21
22
23
24
25
26
27
28
29
30
    """Initialize CNN model.

    Args:
      emb_dim: The dimension of the Embedding layer.
      num_words: The number of the most frequent tokens
        to be used from the corpus.
      sentence_length: The number of words in each sentence.
        Longer sentences get cut, shorter ones padded.
      hid_dim: The dimension of the Embedding layer.
      class_dim: The number of the CNN layer filters.
      dropout_rate: The portion of kept value in the Dropout layer.
    Returns:
31
      tf.keras.models.Model: A Keras model.
32
33
    """

34
    input_layer = tf.keras.layers.Input(shape=(sentence_length,), dtype=tf.int32)
35

36
    layer = tf.keras.layers.Embedding(num_words, output_dim=emb_dim)(input_layer)
37
38

    layer_conv3 = tf.keras.layers.Conv1D(hid_dim, 3, activation="relu")(layer)
39
    layer_conv3 = tf.keras.layers.GlobalMaxPooling1D()(layer_conv3)
40

41
42
    layer_conv4 = tf.keras.layers.Conv1D(hid_dim, 2, activation="relu")(layer)
    layer_conv4 = tf.keras.layers.GlobalMaxPooling1D()(layer_conv4)
43

44
    layer = tf.keras.layers.concatenate([layer_conv4, layer_conv3], axis=1)
45
    layer = tf.keras.layers.BatchNormalization()(layer)
46
    layer = tf.keras.layers.Dropout(dropout_rate)(layer)
47
48
49

    output = tf.keras.layers.Dense(class_dim, activation="softmax")(layer)

50
    super(CNN, self).__init__(inputs=[input_layer], outputs=output)