Skip to content

Instantly share code, notes, and snippets.

@Rexhaif
Created December 8, 2019 19:29
Show Gist options
  • Save Rexhaif/47603d6ed82e5e98681b5e9a277c99c0 to your computer and use it in GitHub Desktop.
Save Rexhaif/47603d6ed82e5e98681b5e9a277c99c0 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import json"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import os"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import pickle"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras import models\n",
"from tensorflow.keras import initializers\n",
"from tensorflow.keras import regularizers\n",
"\n",
"from tensorflow.keras import backend as K\n",
"\n",
"import tensorflow as tf\n",
"\n",
"from tensorflow.keras.layers import Dense\n",
"from tensorflow.keras.layers import Dropout\n",
"from tensorflow.keras.layers import Embedding\n",
"from tensorflow.keras.layers import SeparableConv1D\n",
"from tensorflow.keras.layers import MaxPooling1D\n",
"from tensorflow.keras.layers import GlobalAveragePooling1D"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split, KFold\n",
"from sklearn.preprocessing import LabelEncoder\n",
"from sklearn.metrics import f1_score"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from tqdm.auto import tqdm"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.preprocessing import text, sequence"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"from hyperopt import hp, fmin, space_eval, STATUS_OK, STATUS_FAIL, tpe"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"VOCAB_SIZE = 20_000\n",
"SEQ_LEN = 50\n",
"RANDOM_SEED = 0\n",
"MODEL_NAME = 'hp_11ktweets_200d_1000_evals'\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(RANDOM_SEED)\n",
"tf.random.set_random_seed(RANDOM_SEED)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def sequence_vectorize(train_texts, val_texts):\n",
" \"\"\"Vectorizes texts as sequence vectors.\n",
"\n",
" 1 text = 1 sequence vector with fixed length.\n",
"\n",
" # Arguments\n",
" train_texts: list, training text strings.\n",
" val_texts: list, validation text strings.\n",
"\n",
" # Returns\n",
" x_train, x_val, word_index: vectorized training and validation\n",
" texts and word index dictionary.\n",
" \"\"\"\n",
" # Create vocabulary with training texts.\n",
" tokenizer = text.Tokenizer(num_words=VOCAB_SIZE)\n",
" tokenizer.fit_on_texts(train_texts)\n",
"\n",
" # Vectorize training and validation texts.\n",
" x_train = tokenizer.texts_to_sequences(train_texts)\n",
" x_val = tokenizer.texts_to_sequences(val_texts)\n",
"\n",
" # Get max sequence length.\n",
" max_length = SEQ_LEN\n",
" # Fix sequence length to max value. Sequences shorter than the length are\n",
" # padded in the beginning and sequences longer are truncated\n",
" # at the beginning.\n",
" x_train = sequence.pad_sequences(x_train, maxlen=max_length)\n",
" x_val = sequence.pad_sequences(x_val, maxlen=max_length)\n",
" return x_train, x_val, tokenizer.word_index, tokenizer"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"encoder = LabelEncoder()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"data = pd.read_csv(\"../data/processed/tweets-11000.csv\").sample(frac=1.0).reset_index(drop=True)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"data.label = encoder.fit_transform(data.label)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"x_train, x_test, y_train, y_test = train_test_split(data.text, data.label, test_size=0.2)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"def load_glove_txt_model(glove_file):\n",
" print(\"Loading Glove Model\")\n",
" f = open(glove_file,'r')\n",
" model = {}\n",
" for line in tqdm(f):\n",
" split_line = line.split()\n",
" word = split_line[0]\n",
" embedding = np.array([float(val) for val in split_line[1:]])\n",
" model[word] = embedding\n",
" print(\"Done.\",len(model),\" words loaded!\")\n",
" return model"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"def build_embedding_matrix(glove_data, w2i, vocab_size=VOCAB_SIZE):\n",
" glove_vocab = set(glove_data.keys())\n",
" embeddings_dim = len(glove_data[list(glove_vocab)[0]])\n",
" vocab_size = len(w2i.keys()) + 1\n",
" \n",
" embeddings = np.zeros((vocab_size, embeddings_dim), dtype=np.float32)\n",
" \n",
" for word in w2i.keys():\n",
" embeddings[w2i[word]] = glove_data[word] if word in glove_vocab else np.zeros(embeddings_dim)\n",
" \n",
" return embeddings[:VOCAB_SIZE, :]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading Glove Model\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a400c08b645c4437bc7a4fa2fc410dce",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=1, bar_style='info', max=1), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Done. 1193514 words loaded!\n"
]
}
],
"source": [
"glove = load_glove_txt_model(\"../data/embeddings/glove.twitter.27B.200d.txt\")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"def _get_last_layer_units_and_activation(num_classes):\n",
" \"\"\"Gets the # units and activation function for the last network layer.\n",
"\n",
" # Arguments\n",
" num_classes: int, number of classes.\n",
"\n",
" # Returns\n",
" units, activation values.\n",
" \"\"\"\n",
" if num_classes == 2:\n",
" activation = 'sigmoid'\n",
" units = 1\n",
" else:\n",
" activation = 'softmax'\n",
" units = num_classes\n",
" return units, activation\n",
"\n",
"def sepcnn_model(blocks,\n",
" filters,\n",
" kernel_size,\n",
" embedding_dim,\n",
" dropout_rate,\n",
" pool_size,\n",
" input_shape,\n",
" num_classes,\n",
" num_features,\n",
" use_pretrained_embedding=False,\n",
" is_embedding_trainable=False,\n",
" embedding_matrix=None):\n",
" \"\"\"Creates an instance of a separable CNN model.\n",
"\n",
" # Arguments\n",
" blocks: int, number of pairs of sepCNN and pooling blocks in the model.\n",
" filters: int, output dimension of the layers.\n",
" kernel_size: int, length of the convolution window.\n",
" embedding_dim: int, dimension of the embedding vectors.\n",
" dropout_rate: float, percentage of input to drop at Dropout layers.\n",
" pool_size: int, factor by which to downscale input at MaxPooling layer.\n",
" input_shape: tuple, shape of input to the model.\n",
" num_classes: int, number of output classes.\n",
" num_features: int, number of words (embedding input dimension).\n",
" use_pretrained_embedding: bool, true if pre-trained embedding is on.\n",
" is_embedding_trainable: bool, true if embedding layer is trainable.\n",
" embedding_matrix: dict, dictionary with embedding coefficients.\n",
"\n",
" # Returns\n",
" A sepCNN model instance.\n",
" \"\"\"\n",
" op_units, op_activation = _get_last_layer_units_and_activation(num_classes)\n",
" model = models.Sequential()\n",
"\n",
" # Add embedding layer. If pre-trained embedding is used add weights to the\n",
" # embeddings layer and set trainable to input is_embedding_trainable flag.\n",
" if use_pretrained_embedding:\n",
" model.add(Embedding(input_dim=num_features,\n",
" output_dim=embedding_dim,\n",
" input_length=input_shape[0],\n",
" weights=[embedding_matrix],\n",
" trainable=is_embedding_trainable))\n",
" else:\n",
" model.add(Embedding(input_dim=num_features,\n",
" output_dim=embedding_dim,\n",
" input_length=input_shape[0]))\n",
"\n",
" for _ in range(blocks-1):\n",
" model.add(Dropout(rate=dropout_rate))\n",
" model.add(SeparableConv1D(filters=filters,\n",
" kernel_size=kernel_size,\n",
" activation='relu',\n",
" bias_initializer='random_uniform',\n",
" depthwise_initializer='he_uniform',\n",
" padding='same'))\n",
" model.add(SeparableConv1D(filters=filters,\n",
" kernel_size=kernel_size,\n",
" activation='relu',\n",
" bias_initializer='random_uniform',\n",
" depthwise_initializer='he_uniform',\n",
" padding='same'))\n",
" model.add(MaxPooling1D(pool_size=pool_size))\n",
"\n",
" model.add(SeparableConv1D(filters=filters * 2,\n",
" kernel_size=kernel_size,\n",
" activation='relu',\n",
" bias_initializer='random_uniform',\n",
" depthwise_initializer='he_uniform',\n",
" padding='same'))\n",
" model.add(SeparableConv1D(filters=filters * 2,\n",
" kernel_size=kernel_size,\n",
" activation='relu',\n",
" bias_initializer='random_uniform',\n",
" depthwise_initializer='he_uniform',\n",
" padding='same'))\n",
" model.add(GlobalAveragePooling1D())\n",
" model.add(Dropout(rate=dropout_rate))\n",
" model.add(Dense(op_units, activation=op_activation))\n",
" return model\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"def train_sequence_model(data,\n",
" learning_rate=1e-4,\n",
" epochs=1000,\n",
" batch_size=1024,\n",
" blocks=2,\n",
" filters=64,\n",
" dropout_rate=0.0,\n",
" embedding_dim=200,\n",
" kernel_size=3,\n",
" pool_size=3,\n",
" use_pretrained_embeddings=True,\n",
" are_they_trainable=False):\n",
" \"\"\"Trains sequence model on the given dataset.\n",
" # Arguments\n",
" data: tuples of training and test texts and labels.\n",
" learning_rate: float, learning rate for training model.\n",
" epochs: int, number of epochs.\n",
" batch_size: int, number of samples per batch.\n",
" blocks: int, number of pairs of sepCNN and pooling blocks in the model.\n",
" filters: int, output dimension of sepCNN layers in the model.\n",
" dropout_rate: float: percentage of input to drop at Dropout layers.\n",
" embedding_dim: int, dimension of the embedding vectors.\n",
" kernel_size: int, length of the convolution window.\n",
" pool_size: int, factor by which to downscale input at MaxPooling layer.\n",
" # Raises\n",
" ValueError: If validation data has label values which were not seen\n",
" in the training data.\n",
" \"\"\"\n",
" # Get the data.\n",
" (train_texts, train_labels), (val_texts, val_labels) = data\n",
"\n",
" # Verify that validation labels are in the same range as training labels.\n",
" num_classes = len(encoder.classes_)\n",
" unexpected_labels = [v for v in val_labels if v not in range(num_classes)]\n",
" if len(unexpected_labels):\n",
" raise ValueError('Unexpected label values found in the validation set:'\n",
" ' {unexpected_labels}. Please make sure that the '\n",
" 'labels in the validation set are in the same range '\n",
" 'as training labels.'.format(\n",
" unexpected_labels=unexpected_labels))\n",
"\n",
" # Vectorize texts.\n",
" x_train, x_val, word_index, tokenizer = sequence_vectorize(\n",
" train_texts, val_texts)\n",
" embedding_matrix = None\n",
" if use_pretrained_embeddings:\n",
" embedding_matrix = build_embedding_matrix(glove, word_index, vocab_size=VOCAB_SIZE)\n",
" embedding_dim = embedding_matrix.shape[1]\n",
"\n",
" # Number of features will be the embedding input dimension. Add 1 for the\n",
" # reserved index 0.\n",
" num_features = min(len(word_index) + 1, VOCAB_SIZE)\n",
"\n",
" # Create model instance.\n",
" model = sepcnn_model(blocks=blocks,\n",
" filters=filters,\n",
" kernel_size=kernel_size,\n",
" embedding_dim=embedding_dim,\n",
" dropout_rate=dropout_rate,\n",
" pool_size=pool_size,\n",
" input_shape=x_train.shape[1:],\n",
" num_classes=num_classes,\n",
" num_features=num_features,\n",
" embedding_matrix=embedding_matrix,\n",
" use_pretrained_embedding=use_pretrained_embeddings,\n",
" is_embedding_trainable=are_they_trainable)\n",
"\n",
" # Compile model with learning parameters.\n",
" if num_classes == 2:\n",
" loss = 'binary_crossentropy'\n",
" else:\n",
" loss = 'sparse_categorical_crossentropy'\n",
" optimizer = tf.keras.optimizers.Adam(lr=learning_rate)\n",
" model.compile(optimizer=optimizer, loss=loss, metrics=['acc'])\n",
"\n",
" # Create callback for early stopping on validation loss. If the loss does\n",
" # not decrease in two consecutive tries, stop training.\n",
" callbacks = [tf.keras.callbacks.EarlyStopping(\n",
" monitor='val_loss', patience=30)]\n",
"\n",
" # Train and validate model.\n",
" history = model.fit(\n",
" x_train,\n",
" train_labels,\n",
" epochs=epochs,\n",
" callbacks=callbacks,\n",
" validation_data=(x_val, val_labels),\n",
" verbose=0, # Logs once per epoch.\n",
" batch_size=batch_size)\n",
"\n",
" # Print results.\n",
" history = history.history\n",
" #print('Validation accuracy: {acc}, loss: {loss}'.format(\n",
" # acc=history['val_acc'][-1], loss=history['val_loss'][-1]))\n",
"\n",
" # Save model.\n",
" return model, word_index, tokenizer, history['val_acc'][-1], history['val_loss'][-1]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def config_evaluation(params):\n",
" cross_val = KFold(n_splits=2)\n",
" accs, losses, f_scores = [], [], []\n",
" \n",
" embeddings = params.pop('embeddings')\n",
" #print(embeddings, type(embeddings))\n",
" if embeddings == 'pretrained_frozen':\n",
" params['use_pretrained_embeddings'] = True\n",
" params['are_they_trainable'] = False\n",
" elif embeddings == 'pretrained_unfrozen':\n",
" params['use_pretrained_embeddings'] = True\n",
" params['are_they_trainable'] = True\n",
" elif type(embeddings) == int or type(embeddings) == np.int64:\n",
" params['use_pretrained_embeddings'] = False\n",
" params['embedding_dim'] = embeddings\n",
" else:\n",
" raise KeyError(\"incorrect embeddings parameter\")\n",
"\n",
"\n",
" for i, (train_idx, test_idx) in enumerate(cross_val.split(data)):\n",
" #print(f\"Running fold #{i}\")\n",
" train_part, test_part = data.iloc[train_idx, :], data.iloc[test_idx, :]\n",
" x_train, y_train, x_test, y_test = train_part.text, train_part.label, test_part.text, test_part.label\n",
" try:\n",
" model, w2i, tokenizer, acc, loss = train_sequence_model(\n",
" ((x_train, y_train), (x_test, y_test)), **params\n",
" )\n",
" except Exception as e:\n",
" return {'loss': 100, 'status': STATUS_FAIL}\n",
"\n",
" x_test = sequence.pad_sequences(tokenizer.texts_to_sequences(x_test), maxlen=SEQ_LEN)\n",
" y_pred = np.argmax(model.predict(x_test), -1)\n",
"\n",
" f_score = f1_score(y_test, y_pred, average='macro')\n",
"\n",
" accs.append(acc)\n",
" losses.append(loss)\n",
" f_scores.append(f_score)\n",
"\n",
" K.clear_session()\n",
" \n",
" return {'loss': 1 - np.mean(f_scores), 'status': STATUS_OK}"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"#params = {\n",
"# 'blocks': 1,\n",
"# 'filters': 48,\n",
"# 'learning_rate': 5e-2,\n",
"# 'embeddings': 'pretrained_frozen'\n",
"#}\n",
"\n",
"#f_s = config_evaluation(params)http://web.telegram.com/"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"param_space = {\n",
" 'embeddings': hp.choice('embeddings', ['pretrained_frozen', 'pretrained_unfrozen'] + list(np.arange(15, 512, dtype=np.int64))),\n",
" 'blocks': hp.choice('blocks', [1, 2, 3, 4]),\n",
" 'filters': hp.choice('filters', np.arange(1, 64, dtype=int)),\n",
" 'learning_rate': hp.loguniform('learning_rate', -6, -1),\n",
" 'dropout_rate': hp.loguniform('dropout_rate', -3, -0.1),\n",
" 'kernel_size': hp.choice('kernel_size', [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]),\n",
" 'pool_size': hp.choice('pool_size', [2, 3, 4, 5, 6, 7, 8, 9, 10, 11])\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0%| | 0/100 [00:00<?, ?it/s, best loss: ?]WARNING:tensorflow:From /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Colocations handled automatically by placer.\n",
"WARNING:tensorflow:From /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/layers/core.py:143: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n",
"WARNING:tensorflow:From /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use tf.cast instead.\n",
" 2%|▏ | 2/100 [04:06<3:34:05, 131.08s/it, best loss: 0.28960659023803903]"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/anaconda3/lib/python3.7/site-packages/sklearn/metrics/classification.py:1437: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples.\n",
" 'precision', 'predicted', average, warn_for)\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" 19%|█▉ | 19/100 [33:19<2:17:33, 101.90s/it, best loss: 0.26926304529076583]"
]
}
],
"source": [
"best_eval = fmin(config_evaluation, param_space, algo=tpe.suggest, max_evals=100, verbose=1, show_progressbar=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"best_params = space_eval(param_space, best_eval)\n",
"print(best_params)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"F_score = 0.7830847319685722\n"
]
}
],
"source": [
"f_score_value = 1 - config_evaluation(best_params)['loss']\n",
"print(f\"F_score = {f_score_value}\")"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"F_score = 0.7539206044309616\n"
]
}
],
"source": [
"print(f\"F_score = {f_score_value}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#best_params = {\"blocks\": 1, \"dropout_rate\": 0.849370586814899, \"filters\": 62, \"kernel_size\": 6, \"learning_rate\": 0.010650715059682464, \"pool_size\": 6, \"use_pretrained_embeddings\": False, \"embedding_dim\": 298}"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"model, w2i, tokenizer, acc, loss = train_sequence_model(\n",
" \n",
" ((x_train, y_train), (x_test, y_test)), **best_params\n",
"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.73981816, 1.0798143225583163)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"acc, loss"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"embedding (Embedding) (None, 50, 442) 8840000 \n",
"_________________________________________________________________\n",
"separable_conv1d (SeparableC (None, 50, 62) 28350 \n",
"_________________________________________________________________\n",
"separable_conv1d_1 (Separabl (None, 50, 62) 4030 \n",
"_________________________________________________________________\n",
"global_average_pooling1d (Gl (None, 62) 0 \n",
"_________________________________________________________________\n",
"dropout (Dropout) (None, 62) 0 \n",
"_________________________________________________________________\n",
"dense (Dense) (None, 10) 630 \n",
"=================================================================\n",
"Total params: 8,873,010\n",
"Trainable params: 8,873,010\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"x_input = sequence.pad_sequences(tokenizer.texts_to_sequences(x_test), maxlen=SEQ_LEN)\n",
"x_output = np.argmax(model.predict(x_input), -1)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import recall_score"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.7654332582982518"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f1_score(y_test, x_output, average='macro')"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.739370784703438"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"recall_score(y_test, x_output, average='macro')"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"models.save_model(model, \"./model.h5\")"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"W0915 09:48:16.029557 140323065866048 deprecation.py:506] From /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/init_ops.py:97: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
"W0915 09:48:16.030251 140323065866048 deprecation.py:506] From /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/init_ops.py:97: calling GlorotUniform.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
"W0915 09:48:16.082828 140323065866048 deprecation.py:506] From /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/init_ops.py:97: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Call initializer instance with the dtype argument instead of passing it to the constructor\n"
]
},
{
"data": {
"text/plain": [
"<tensorflow.python.keras.engine.sequential.Sequential at 0x7f9ebfac2b38>"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"models.load_model(\"./model.h5\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#from tensorflow import lite as tfl\n",
"\n",
"#converter = tfl.TFLiteConverter.from_keras_model_file(\"./model.h5\")\n",
"\n",
"#converter.optimizations = [tfl.Optimize.DEFAULT]\n",
"\n",
"#tflite_model = converter.convert()\n",
"\n",
"#with open(\"./model.tflite\", 'wb') as f:\n",
"# f.write(tflite_model)\n",
"\n",
"#interpreter = tfl.Interpreter(\"./model.tflite\")\n",
"\n",
"#interpreter.get_input_details()\n",
"\n",
"#interpreter.get_output_details()"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"def save_all(model, tokenizer, label_encoder, params, path=\"../models/sepcnn\", model_name='hp_baseline'):\n",
" \n",
" for k in params.keys():\n",
" if type(params[k]) == np.int64:\n",
" params[k] = int(params[k])\n",
"\n",
" try:\n",
" os.mkdir(f\"{path}/{model_name}/\")\n",
" except Exception as e:\n",
" print(\"model name already exists\")\n",
" \n",
" models.save_model(model, f\"{path}/{model_name}/model.h5\")\n",
" tok_json = tokenizer.to_json()\n",
" with open(f\"{path}/{model_name}/vocab.json\", 'w', encoding='utf-8') as f:\n",
" f.write(tok_json)\n",
" \n",
" with open(f\"{path}/{model_name}/label_encoder.pckl\", 'wb') as f:\n",
" pickle.dump(label_encoder, f)\n",
" \n",
" with open(f\"{path}/{model_name}/model_params.json\", 'w', encoding='utf-8') as f:\n",
" json.dump(params, f)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"save_all(model, tokenizer, encoder, best_params, model_name=MODEL_NAME)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"W0915 09:56:08.712595 139720805164864 deprecation.py:506] From /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/initializers.py:119: calling RandomUniform.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
"W0915 09:56:08.721024 139720805164864 deprecation.py:506] From /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"If using Keras pass *_constraint arguments to layers.\n",
"W0915 09:56:08.725868 139720805164864 deprecation.py:506] From /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/init_ops.py:97: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
"W0915 09:56:08.726533 139720805164864 deprecation.py:506] From /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/init_ops.py:97: calling GlorotUniform.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
"W0915 09:56:08.773472 139720805164864 nn_ops.py:4283] Large dropout rate: 0.730938 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended.\n",
"W0915 09:56:08.783573 139720805164864 deprecation.py:506] From /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/init_ops.py:97: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Call initializer instance with the dtype argument instead of passing it to the constructor\n"
]
}
],
"source": [
"model = models.load_model(f\"../models/sepcnn/{MODEL_NAME}/model.h5\")"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"from bento_keras import KerasBrandSuggestor"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"pred = KerasBrandSuggestor.pack(label_encoder=encoder, vocab=tokenizer.to_json(), model=model)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2019-09-15 09:52:23,285] INFO - Searching for dependant modules of bento_keras:/root/keymoji-brand/stages/bento_keras.py\n",
"[2019-09-15 09:52:31,083] INFO - Copying local python module '/root/keymoji-brand/stages/bento_keras.py'\n",
"[2019-09-15 09:52:31,084] INFO - Done copying local python dependant modules\n",
"[2019-09-15 09:52:31,145] INFO - BentoService KerasBrandSuggestor:2019_09_15_71e3b419 saved to ../models/production/KerasBrandSuggestor/2019_09_15_71e3b419\n"
]
},
{
"data": {
"text/plain": [
"'../models/production/KerasBrandSuggestor/2019_09_15_71e3b419'"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pred.save(\"../models/production\")"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "Error when checking input: expected embedding_input to have shape (40,) but got array with shape (50,)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-65-173a15a24538>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpred\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'string'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'Wanna by dell notebook'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/keymoji-brand/stages/bento_keras.py\u001b[0m in \u001b[0;36mpredict\u001b[0;34m(self, input_object)\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0mpad_input\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msequence\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpad_sequences\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mseq_input\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmaxlen\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m40\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mpredictions\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0martifacts\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpad_input\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0mpredictions\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0menumerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpredictions\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/bentoml/artifact/keras_model_artifact.py\u001b[0m in \u001b[0;36mpredict\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 163\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgraph\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_default\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 164\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_default\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 165\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeras_model\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 166\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 167\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpredict_classes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training.py\u001b[0m in \u001b[0;36mpredict\u001b[0;34m(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)\u001b[0m\n\u001b[1;32m 906\u001b[0m \u001b[0mmax_queue_size\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmax_queue_size\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 907\u001b[0m \u001b[0mworkers\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mworkers\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 908\u001b[0;31m use_multiprocessing=use_multiprocessing)\n\u001b[0m\u001b[1;32m 909\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 910\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mreset_metrics\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training_arrays.py\u001b[0m in \u001b[0;36mpredict\u001b[0;34m(self, model, x, batch_size, verbose, steps, callbacks, **kwargs)\u001b[0m\n\u001b[1;32m 714\u001b[0m \u001b[0mbatch_size\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_validate_or_infer_batch_size\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbatch_size\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msteps\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 715\u001b[0m x, _, _ = model._standardize_user_data(\n\u001b[0;32m--> 716\u001b[0;31m x, check_steps=True, steps_name='steps', steps=steps)\n\u001b[0m\u001b[1;32m 717\u001b[0m return predict_loop(\n\u001b[1;32m 718\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training.py\u001b[0m in \u001b[0;36m_standardize_user_data\u001b[0;34m(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)\u001b[0m\n\u001b[1;32m 2469\u001b[0m \u001b[0mfeed_input_shapes\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2470\u001b[0m \u001b[0mcheck_batch_axis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;31m# Don't enforce the batch size.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2471\u001b[0;31m exception_prefix='input')\n\u001b[0m\u001b[1;32m 2472\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2473\u001b[0m \u001b[0;31m# Get typespecs for the input data and sanitize it if necessary.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training_utils.py\u001b[0m in \u001b[0;36mstandardize_input_data\u001b[0;34m(data, names, shapes, check_batch_axis, exception_prefix)\u001b[0m\n\u001b[1;32m 570\u001b[0m \u001b[0;34m': expected '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mnames\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m' to have shape '\u001b[0m \u001b[0;34m+\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 571\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m' but got array with shape '\u001b[0m \u001b[0;34m+\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 572\u001b[0;31m str(data_shape))\n\u001b[0m\u001b[1;32m 573\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 574\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: Error when checking input: expected embedding_input to have shape (40,) but got array with shape (50,)"
]
}
],
"source": [
"pred.predict({'string': 'Wanna by dell notebook'})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment