Skip to content

Instantly share code, notes, and snippets.

@dipanjannag
Created April 23, 2018 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dipanjannag/e2e711478d6914a21e6912896d61ef5d to your computer and use it in GitHub Desktop.
Save dipanjannag/e2e711478d6914a21e6912896d61ef5d to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/name/anaconda2/lib/python2.7/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
" from ._conv import register_converters as _register_converters\n",
"Using TensorFlow backend.\n"
]
}
],
"source": [
"from __future__ import print_function\n",
"\n",
"import keras\n",
"from keras.models import Sequential\n",
"from keras.models import Model\n",
"from keras.layers import Input\n",
"from keras.layers import Dense\n",
"from keras.layers import LSTM\n",
"from keras.layers import GRU\n",
"from keras.layers import SimpleRNN\n",
"from keras.layers import Dropout\n",
"from keras.layers import concatenate\n",
"from keras import losses\n",
"from keras import regularizers\n",
"from keras.constraints import min_max_norm\n",
"import h5py\n",
"\n",
"from keras.constraints import Constraint\n",
"from keras import backend as K\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def my_crossentropy(y_true, y_pred):\n",
" return K.mean(2*K.abs(y_true-0.5) * K.binary_crossentropy(y_pred, y_true), axis=-1)\n",
"\n",
"def mymask(y_true):\n",
" return K.minimum(y_true+1., 1.)\n",
"\n",
"def msse(y_true, y_pred):\n",
" return K.mean(mymask(y_true) * K.square(K.sqrt(y_pred) - K.sqrt(y_true)), axis=-1)\n",
"\n",
"def mycost(y_true, y_pred):\n",
" return K.mean(mymask(y_true) * (10*K.square(K.square(K.sqrt(y_pred) - K.sqrt(y_true))) + K.square(K.sqrt(y_pred) - K.sqrt(y_true)) + 0.01*K.binary_crossentropy(y_pred, y_true)), axis=-1)\n",
"\n",
"def my_accuracy(y_true, y_pred):\n",
" return K.mean(2*K.abs(y_true-0.5) * K.equal(y_true, K.round(y_pred)), axis=-1)\n",
"\n",
"class WeightClip(Constraint):\n",
" '''Clips the weights incident to each hidden unit to be inside a range\n",
" '''\n",
" def __init__(self, c=2):\n",
" self.c = c\n",
"\n",
" def __call__(self, p):\n",
" return K.clip(p, -self.c, self.c)\n",
"\n",
" def get_config(self):\n",
" return {'name': self.__class__.__name__,\n",
" 'c': self.c}\n",
"\n",
"reg = 0.000001\n",
"constraint = WeightClip(0.499)"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"main_input = Input(shape=(None, 42), name='main_input')\n",
"tmp = Dense(24, activation='tanh', name='input_dense', kernel_constraint=constraint, bias_constraint=constraint)(main_input)\n",
"vad_gru = GRU(24, activation='tanh', recurrent_activation='sigmoid', return_sequences=True, name='vad_gru', kernel_regularizer=regularizers.l2(reg), recurrent_regularizer=regularizers.l2(reg), kernel_constraint=constraint, recurrent_constraint=constraint, bias_constraint=constraint)(tmp)\n",
"vad_output = Dense(1, activation='sigmoid', name='vad_output', kernel_constraint=constraint, bias_constraint=constraint)(vad_gru)\n",
"noise_input = keras.layers.concatenate([tmp, vad_gru, main_input])\n",
"noise_gru = GRU(48, activation='relu', recurrent_activation='sigmoid', return_sequences=True, name='noise_gru', kernel_regularizer=regularizers.l2(reg), recurrent_regularizer=regularizers.l2(reg), kernel_constraint=constraint, recurrent_constraint=constraint, bias_constraint=constraint)(noise_input)\n",
"denoise_input = keras.layers.concatenate([vad_gru, noise_gru, main_input])\n",
"\n",
"denoise_gru = GRU(96, activation='tanh', recurrent_activation='sigmoid', return_sequences=True, name='denoise_gru', kernel_regularizer=regularizers.l2(reg), recurrent_regularizer=regularizers.l2(reg), kernel_constraint=constraint, recurrent_constraint=constraint, bias_constraint=constraint)(denoise_input)\n",
"\n",
"denoise_output = Dense(22, activation='sigmoid', name='denoise_output', kernel_constraint=constraint, bias_constraint=constraint)(denoise_gru)\n",
"\n",
"model = Model(inputs=main_input, outputs=[denoise_output, vad_output])\n",
"\n",
"model.compile(loss=[mycost, my_crossentropy],\n",
" metrics=[msse],\n",
" optimizer='adam', loss_weights=[10, 0.5])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"__________________________________________________________________________________________________\n",
"Layer (type) Output Shape Param # Connected to \n",
"==================================================================================================\n",
"main_input (InputLayer) (None, None, 42) 0 \n",
"__________________________________________________________________________________________________\n",
"input_dense (Dense) (None, None, 24) 1032 main_input[0][0] \n",
"__________________________________________________________________________________________________\n",
"vad_gru (GRU) (None, None, 24) 3528 input_dense[0][0] \n",
"__________________________________________________________________________________________________\n",
"concatenate_1 (Concatenate) (None, None, 90) 0 input_dense[0][0] \n",
" vad_gru[0][0] \n",
" main_input[0][0] \n",
"__________________________________________________________________________________________________\n",
"noise_gru (GRU) (None, None, 48) 20016 concatenate_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"concatenate_2 (Concatenate) (None, None, 114) 0 vad_gru[0][0] \n",
" noise_gru[0][0] \n",
" main_input[0][0] \n",
"__________________________________________________________________________________________________\n",
"denoise_gru (GRU) (None, None, 96) 60768 concatenate_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"denoise_output (Dense) (None, None, 22) 2134 denoise_gru[0][0] \n",
"__________________________________________________________________________________________________\n",
"vad_output (Dense) (None, None, 1) 25 vad_gru[0][0] \n",
"==================================================================================================\n",
"Total params: 87,503\n",
"Trainable params: 87,503\n",
"Non-trainable params: 0\n",
"__________________________________________________________________________________________________\n"
]
}
],
"source": [
"model.summary()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment