Esempio n. 1
0
import os
import sys
import tensorflow as tf
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
import config_service_client

learning_param = config_service_client.getMachineLearningParamForUsecase('topic_modeling')
number_filters = int(learning_param['number_filters'])
embedding_size = int(learning_param['embedding_size'])
window_size = int(learning_param['window_size'])
filter_shape1 = [window_size, embedding_size]
filter_shape2 = [window_size, number_filters]
pooling_window = int(learning_param['pooling_window'])
pooling_stride = int(learning_param['pooling_window'])

def generate_cnn_model(n_classes, n_words, learning_rate):
    """2 layer ConvNet to predict from sequence of words to a class."""
    def cnn_model(features, target):
        # Convert indexes of words into embeddings.
        # This creates embeddings matrix of [n_words, embedding_size] and then
        # maps word indexes of the sequence into [batch_size, sequence_length,
        # embedding_size].

        target = tf.one_hot(target, n_classes, 1, 0)
        word_vectors = tf.contrib.layers.embed_sequence(
            features, vocab_size=n_words, embed_dim=embedding_size, scope='words')
        word_vectors = tf.expand_dims(word_vectors, 3)
        with tf.variable_scope('CNN_layer1'):
            # Apply Convolution filtering on input sequence.
            conv1 = tf.contrib.layers.convolution2d(
                word_vectors, number_filters, filter_shape1, padding='VALID')
Esempio n. 2
0
import os
import sys
import tensorflow as tf
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
import config_service_client

learning_param = config_service_client.getMachineLearningParamForUsecase(
    'topic_modeling')
number_filters = int(learning_param['number_filters'])
embedding_size = int(learning_param['embedding_size'])
window_size = int(learning_param['window_size'])
filter_shape1 = [window_size, embedding_size]
filter_shape2 = [window_size, number_filters]
pooling_window = int(learning_param['pooling_window'])
pooling_stride = int(learning_param['pooling_window'])


def generate_cnn_model(n_classes, n_words, learning_rate):
    """2 layer ConvNet to predict from sequence of words to a class."""
    def cnn_model(features, target):
        # Convert indexes of words into embeddings.
        # This creates embeddings matrix of [n_words, embedding_size] and then
        # maps word indexes of the sequence into [batch_size, sequence_length,
        # embedding_size].

        target = tf.one_hot(target, n_classes, 1, 0)
        word_vectors = tf.contrib.layers.embed_sequence(
            features,
            vocab_size=n_words,
            embed_dim=embedding_size,
            scope='words')