Skip to content
Snippets Groups Projects
Commit db9cbc19 authored by Jayesh's avatar Jayesh
Browse files

add hypermodel

parent 701d61a0
No related branches found
No related tags found
No related merge requests found
import keras_tuner as kt
import numpy as np
import tensorflow as tf
from constants import *
class StackOverflowCNN:
"""
A class representing a CNN model for Stack Overflow classification.
"""
def __init__(self, vocab_length, embedding_matrix, filters=64, hidden_layer_size=0,
num_convolutions=3, kernel_size=3):
"""
Initializes the StackOverflowCNN model.
Args:
vocab_length (int): The length of the vocabulary.
embedding_matrix (numpy.ndarray): The embedding matrix.
filters (int, optional): The number of filters in the convolutional layers. Defaults to 64.
hidden_layer_size (int, optional): The size of the hidden layer. Defaults to 0.
num_convolutions (int, optional): The number of convolutional layers. Defaults to 3.
kernel_size (int, optional): The size of the convolutional kernel. Defaults to 3.
"""
input_layer = tf.keras.layers.Input(
shape=(MAX_SEQUENCE_LENGTH,),
batch_size=32
)
embedding = tf.keras.layers.Embedding(vocab_length,
EMBEDDING_DIM,
input_length=MAX_SEQUENCE_LENGTH,
embeddings_initializer=tf.keras.initializers.Constant(
embedding_matrix),
trainable=True, mask_zero=True)(input_layer)
conv_layers = [
tf.keras.layers.Conv1D(filters=filters,
kernel_size=kernel_size)(embedding)
for _ in range(num_convolutions)
]
pooling_layers = [
tf.keras.layers.GlobalMaxPooling1D()(conv)
for conv in conv_layers
]
if len(pooling_layers) == 1:
hidden = pooling_layers[0]
else:
hidden = tf.keras.layers.concatenate(pooling_layers, axis=1)
if hidden_layer_size > 0:
hidden = tf.keras.layers.Dense(
hidden_layer_size, activation='relu')(hidden)
outputs = tf.keras.layers.Dense(NUM_CLASSES)(hidden)
self.model = tf.keras.Model(inputs=[input_layer], outputs=outputs)
def get_model(self):
"""
Returns the CNN model.
Returns:
tf.keras.Model: The CNN model.
"""
return self.model
class StackOverflowCNNHyperModel(kt.HyperModel):
def __init__(self, vocab_length, embedding_matrix):
super().__init__()
self.vocab_length = vocab_length
self.embedding_matrix = embedding_matrix
def build(self, hp):
hp_num_filters = hp.Choice('num_filters', values=[32, 64])
hp_num_conv_layers = hp.Choice('num_layers', values=[1, 2, 3])
hp_kernel_size = hp.Choice('kernel_size', values=[3, 5, 7])
hp_hidden_size = hp.Choice('hidden_size', values=[0, 64, 128])
hp_learning_rate = hp.Choice('learning_rate', values=[
0.001, 0.0001, 0.00001])
input_layer = tf.keras.layers.Input(shape=(MAX_SEQUENCE_LENGTH,))
embedding = tf.keras.layers.Embedding(self.vocab_length, EMBEDDING_DIM,
input_length=MAX_SEQUENCE_LENGTH,
embeddings_initializer=tf.keras.initializers.Constant(
self.embedding_matrix),
trainable=True, mask_zero=True)(input_layer)
conv_layers = [
tf.keras.layers.Conv1D(filters=hp_num_filters,
kernel_size=hp_kernel_size)(embedding)
for _ in range(hp_num_conv_layers)
]
pooling_layers = [
tf.keras.layers.GlobalMaxPooling1D()(conv)
for conv in conv_layers
]
if len(pooling_layers) == 1:
hidden = pooling_layers[0]
else:
hidden = tf.keras.layers.concatenate(pooling_layers, axis=1)
if hp_hidden_size > 0:
hidden = tf.keras.layers.Dense(
hp_hidden_size, activation='relu')(hidden)
outputs = tf.keras.layers.Dense(NUM_CLASSES)(hidden)
optimizer = tf.keras.optimizers.AdamW(learning_rate=hp_learning_rate)
loss = tf.keras.losses.BinaryCrossentropy(from_logits=True)
model = tf.keras.Model(inputs=[input_layer], outputs=outputs)
model.compile(loss=loss, optimizer=optimizer)
return model
def fit(self, hp, model, *args, **kwargs):
return model.fit(
*args,
batch_size=hp.Int('batch_size', min_value=32,
max_value=64, step=32),
**kwargs,
)
%% Cell type:code id: tags:
``` python
import keras_tuner as kt
import numpy as np
import tensorflow as tf
```
%% Cell type:code id: tags:
``` python
from constants import *
```
%% Cell type:code id: tags:
``` python
class StackOverflowCNN:
"""
A class representing a CNN model for Stack Overflow classification.
"""
def __init__(self, vocab_length, embedding_matrix, filters=64, hidden_layer_size=0,
num_convolutions=3, kernel_size=3):
"""
Initializes the StackOverflowCNN model.
Args:
vocab_length (int): The length of the vocabulary.
embedding_matrix (numpy.ndarray): The embedding matrix.
filters (int, optional): The number of filters in the convolutional layers. Defaults to 64.
hidden_layer_size (int, optional): The size of the hidden layer. Defaults to 0.
num_convolutions (int, optional): The number of convolutional layers. Defaults to 3.
kernel_size (int, optional): The size of the convolutional kernel. Defaults to 3.
"""
input_layer = tf.keras.layers.Input(
shape=(MAX_SEQUENCE_LENGTH,),
batch_size=32
)
embedding = tf.keras.layers.Embedding(vocab_length,
EMBEDDING_DIM,
input_length=MAX_SEQUENCE_LENGTH,
embeddings_initializer=tf.keras.initializers.Constant(embedding_matrix),
trainable=True, mask_zero=True)(input_layer)
conv_layers = [
tf.keras.layers.Conv1D(filters=filters,
kernel_size=kernel_size)(embedding)
for _ in range(num_convolutions)
]
pooling_layers = [
tf.keras.layers.GlobalMaxPooling1D()(conv)
for conv in conv_layers
]
if len(pooling_layers) == 1:
hidden = pooling_layers[0]
else:
hidden = tf.keras.layers.concatenate(pooling_layers, axis=1)
if hidden_layer_size > 0:
hidden = tf.keras.layers.Dense(hidden_layer_size, activation='relu')(hidden)
outputs = tf.keras.layers.Dense(NUM_CLASSES)(hidden)
self.model = tf.keras.Model(inputs=[embedding], outputs=outputs)
def get_model(self):
"""
Returns the CNN model.
Returns:
tf.keras.Model: The CNN model.
"""
return self.model
```
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment