From cc992f7f2dc0bc7740a15bc10522f16d01ed0fa2 Mon Sep 17 00:00:00 2001 From: nzimme Date: Sun, 27 Oct 2019 15:04:00 +0100 Subject: [PATCH] random shuffle of sample --- DeepDrug.ipynb | 121 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 110 insertions(+), 11 deletions(-) diff --git a/DeepDrug.ipynb b/DeepDrug.ipynb index b81eda5..ef915dd 100644 --- a/DeepDrug.ipynb +++ b/DeepDrug.ipynb @@ -9,11 +9,28 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Using TensorFlow backend.\n" + ] + } + ], "source": [ - "import numpy as np" + "import numpy as np\n", + "\n", + "from sklearn.preprocessing import LabelEncoder\n", + "from keras.models import Sequential\n", + "from keras import optimizers\n", + "from keras.layers import Dense, Flatten, TimeDistributedn, Dropout\n", + "from keras import Input, Model\n", + "from keras.layers import add, Activation\n", + "#from keras.utils import plot_model # Needs pydot.\n", + "from keras.layers import Conv3D, MaxPooling3D" ] }, { @@ -30,7 +47,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -39,7 +56,7 @@ "''" ] }, - "execution_count": 3, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -67,18 +84,100 @@ ] }, { - "cell_type": "code", - "execution_count": 6, + "cell_type": "markdown", "metadata": {}, - "outputs": [], - "source": [] + "source": [ + "### Creating input and ouputs" + ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "data_onehot = np.ndarray(shape=(2219, 14, 32, 32, 32)) # initializing empty array\n", + "indices = np.random.permutation(2219)\n", + "output = np.ndarray(shape=(2219, 3)) # softmax 3, {steroid=1, heme=1, nucleotide=1}\n", + "lmin = len(steroid)\n", + "lmid = len(heme)\n", + "lmax = len(nucleotide)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "n = -1\n", + "for i in indices:\n", + " n += 1\n", + " if i < lmin:\n", + " data_onehot[n,] = np.load(\"deepdrug3d_voxel_data/\"+steroid[i]+\".npy\")\n", + " output[n,] = [1,0,0]\n", + " elif i > lmin and i < (lmin + lmid):\n", + " data_onehot[n,] = np.load(\"deepdrug3d_voxel_data/\"+heme[i - lmin]+\".npy\")\n", + " output[n,] = [0,1,0]\n", + " else:\n", + " data_onehot[n,] = np.load(\"deepdrug3d_voxel_data/\"+nucleotide[i - (lmin+lmid) - 1]+\".npy\")\n", + " output[n,] = [0,0,1]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "X_train = data_onehot[0:1664,]\n", + "Y_train = output[0:1664,]\n", + "X_test = data_onehot[1664:,]\n", + "Y_test = output[1664:,]" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 14, 32, 32, 32)" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def model_sequential(): # créer un objet modèle\n", + " \"\"\"\n", + " Return a simple sequentiel model\n", + " \n", + " Returns :\n", + " - model : keras.Model\n", + " \"\"\"\n", + " inputs = Input(shape=(32,32,32,14)) # 759 aa, 21 car onehot\n", + " conv_1 = Conv3D(64, (28, 28, 28), padding=\"same\", activation=\"LeakyReLU\",\n", + " kernel_initializer=\"he_normal\")(inputs)\n", + " conv_2 = Conv3D(64, (26, 26, 26), padding=\"same\", activation=\"LeakyReLU\",\n", + " kernel_initializer=\"he_normal\")(conv_1)\n", + " drop_1 = Dropout(0.2)(conv_2)\n", + " maxpool = MaxPooling3D()(drop_1)\n", + " drop_2 = Dropout(0.4)(maxpool)\n", + " dense = Dense(512)(drop_2)\n", + " drop_3 = Dropout(0.4)(dense)\n", + " output = TimeDistributed(Dense(3, activation='softmax'))(drop_3)\n", + " model = Model(inputs=inputs, outputs=output)\n", + " my_opt = optimizers.Adam(learning_rate=0.000001, beta_1=0.9, beta_2=0.999, amsgrad=False)\n", + " print(model.summary)\n", + " model.compile(optimizer=my_opt, loss=\"categorical_crossentropy\",\n", + " metrics=[\"accuracy\"])\n", + " return model" + ] }, { "cell_type": "code",