diff --git a/1_to_1_multi_layer.ipynb b/1_to_1_multi_layer.ipynb index ad838a5..34886d8 100644 --- a/1_to_1_multi_layer.ipynb +++ b/1_to_1_multi_layer.ipynb @@ -4,12 +4,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Import all packages" + "# Import all packages\n", + "***" ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -62,12 +63,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Import the dataset of the matched 8-hit tracks" + "# Preprocessing of the matched 8-hit tracks\n", + "\n", + "***\n", + "\n", + "## Import the dataset of the matched 8-hit tracks" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -82,12 +87,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Convert the data to an array (float32)" + "## Convert the data to an array (float32)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -108,43 +113,47 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Transformation between original 2D-array into 3D-array\n", + "## Transformation between original 2D-array into 3D-array\n", "\n", - "#### reshapor()\n", + "***\n", "\n", - "Description:\n", + "### reshapor()\n", + "\n", + "**Description:**\n", "\n", "Transforms 2D-array into 3D array\n", "\n", - "Arguments:\n", + "**Arguments:**\n", "\n", "- arr_orig: Original 2D array\n", "- num_inputs: Number of inputs per timestep (default value = 3 for X,Y,Z coordinates)\n", "\n", - "Returns:\n", + "**Returns:**\n", "\n", "- arr: 3D-array of shape(particlenumber, timesteps, input = coordinates)\n", "\n", - "#### reshapor_inv()\n", "\n", - "Description:\n", + "***\n", + "\n", + "\n", + "### reshapor_inv()\n", + "\n", + "**Description:**\n", "\n", "Inverse transformation from 3D-array into 2D-array\n", "\n", - "Arguments:\n", + "**Arguments:**\n", "\n", "- array_shaped: 3D-array of shape(particlenumber, timesteps, input = coordinates)\n", "\n", - "Returns:\n", + "**Returns:**\n", "\n", "- arr: 2D-array of shape(particlenumber, inputs)" ] }, { - "cell_type": "code", - "execution_count": 4, + "cell_type": "raw", "metadata": {}, - "outputs": [], "source": [ "#Reshapes the 2D-array to a 3D-array\n", "\n", @@ -178,29 +187,31 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Create random training and test sets from the data\n", + "## Create random training and test sets from the data\n", "\n", - "#### create_random_sets()\n", + "***\n", "\n", - "Description:\n", + "### create_random_sets()\n", + "\n", + "**Description:**\n", "\n", "Splits an dataset into a train and a test set\n", "\n", "\n", - "Input:\n", + "**Input:**\n", "\n", "- dataset: The actual dataset with shape (particles, other dimensions)\n", "- train_to_total_ratio: The ratio that the training-set should be out of the original set.\n", " The remaining part will become the test-set\n", " \n", "\n", - "Returns:\n", + "**Returns:**\n", "\n", "- train_set: The newly created training set (particles, other dimensions)\n", "- test_set: The newly created test set (particles, other dimensions)\n", " \n", " \n", - "Additional comments:\n", + "**Additional comments:**\n", "\n", "The data will be randomly shuffled before it gets split up" ] @@ -258,58 +269,74 @@ "source": [ "## Normalization of the data\n", "\n", - "### Normalization on a min_max_scaler from sklearn\n", + "***\n", "\n", - "#### correct_array_steps()\n", + "## Normalization based on min_max_scaler from sklearn\n", "\n", - "Description: As the scaler will be fixed on arrays of specific length this function returns an array padded with zeros with the correct shape\n", + "### correct_array_steps()\n", "\n", - "Input:\n", + "**Description:**\n", + "\n", + "As the scaler will be fixed on arrays of specific length this function returns an array padded with zeros with the correct shape\n", + "\n", + "**Input:**\n", "\n", "- arr: 3D-array of shape(particle_number, timesteps, num_inputs)\n", "- steps: Required number of timesteps for the scaler (default value = 8)\n", "- num_inputs: Number of inputs per timestep (default value = 3 for X,Y,Z coordinates)\n", "\n", - "Returns:\n", + "**Returns:**\n", "\n", "- arr: 3D array of shape(particle_number, steps, num_inputs)\n", "\n", - "#### set_min_max_scaler()\n", + "***\n", "\n", - "Description: Sets the min_max_scaler based on the dataset given (sklearn based)\n", + "### set_min_max_scaler()\n", "\n", - "Input:\n", + "**Description:**\n", + "\n", + "Sets the min_max_scaler based on the dataset given (sklearn based)\n", + "\n", + "**Input:**\n", "\n", "- arr: 2D of shape(particle_number, inputs) or 3D-array of shape(particle_number, timesteps, num_inputs)\n", "- feature_range: Tuple which defines the area to which the data should be scaled (default value = (-1,1))\n", "\n", - "Returns:\n", + "**Returns:**\n", "\n", "- min_max_scalor: min_max_scaler based of the data given\n", "\n", - "#### min_max_scaler()\n", + "***\n", "\n", - "Description: Transforms a 3D-array with a given min_max_scaler (sklearn based)\n", + "### min_max_scaler()\n", "\n", - "Input:\n", + "**Description:** \n", + "\n", + "Transforms a 3D-array with a given min_max_scaler (sklearn based)\n", + "\n", + "**Input:**\n", "\n", "- arr: 3D-array of shape(particle_number, timesteps, num_inputs)\n", "- min_max_scalor: The min_max_scaler used for the transformation (default value: min_max_scalor)\n", "\n", - "Returns:\n", + "**Returns:**\n", "\n", "- arr: Transformed 3D-array\n", "\n", - "#### min_max_scaler_inv()\n", + "***\n", "\n", - "Description: Transforms a 3D-array with a given min_max_scaler back to original form (sklearn based)\n", + "### min_max_scaler_inv()\n", "\n", - "Input:\n", + "**Description:**\n", + "\n", + "Transforms a 3D-array with a given min_max_scaler back to original form (sklearn based)\n", + "\n", + "**Input:**\n", "\n", "- arr: 3D-array of shape(particle_number, timesteps, num_inputs)\n", "- min_max_scalor: The min_max_scaler used for the transformation (default value: min_max_scalor)\n", "\n", - "Returns:\n", + "**Returns:**\n", "\n", "- arr: Transformed 3D-array" ] @@ -360,45 +387,55 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Normalization based on a standard_scaler from sklearn\n", + "## Normalization based on a standard_scaler from sklearn\n", "\n", "\n", - "#### set_std_scaler()\n", + "### set_std_scaler()\n", "\n", - "Description: Sets the std_scaler based on the dataset given (sklearn based)\n", + "**Description: **\n", "\n", - "Input:\n", + "Sets the std_scaler based on the dataset given (sklearn based)\n", + "\n", + "**Input:**\n", "\n", "- arr: 2D of shape(particle_number, inputs) or 3D-array of shape(particle_number, timesteps, num_inputs)\n", "- feature_range: Tuple which defines the area to which the data should be scaled (default value = (-1,1))\n", "\n", - "Returns:\n", + "**Returns:**\n", "\n", - "- std_scaler: std_scaler based of the data given\n", + "- std_scalor: std_scaler based of the data given\n", "\n", - "#### std_scaler()\n", + "***\n", "\n", - "Description: Transforms a 3D-array with a given std_scaler (sklearn based)\n", + "### std_scaler()\n", "\n", - "Input:\n", + "**Description: **\n", + "\n", + "Transforms a 3D-array with a given std_scaler (sklearn based)\n", + "\n", + "**Input:**\n", "\n", "- arr: 3D-array of shape(particle_number, timesteps, num_inputs)\n", - "- std_scaler: The std_scaler used for the transformation (default value: std_scaler)\n", + "- std_scalor: The std_scaler used for the transformation (default value: std_scaler)\n", "\n", - "Returns:\n", + "**Returns:**\n", "\n", "- arr: Transformed 3D-array\n", "\n", - "#### std_scaler_inv()\n", + "***\n", "\n", - "Description: Transforms a 3D-array with a given std_scaler back to original form (sklearn based)\n", + "### std_scaler_inv()\n", "\n", - "Input:\n", + "**Description: **\n", + "\n", + "Transforms a 3D-array with a given std_scaler back to original form (sklearn based)\n", + "\n", + "**Input:**\n", "\n", "- arr: 3D-array of shape(particle_number, timesteps, num_inputs)\n", - "- min_max_scalor: The std_scaler used for the transformation (default value: std_scaler)\n", + "- std_scalor: The std_scaler used for the transformation (default value: std_scaler)\n", "\n", - "Returns:\n", + "**Returns:**\n", "\n", "- arr: Transformed 3D-array" ] @@ -451,11 +488,54 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], - "source": [] + "source": [ + "## Scale the data using the min_max_scaler or the std_scaler defined before\n", + "\n", + "### scaler()\n", + "\n", + "****Description:****\n", + "\n", + "Shapes and transforms the data with a given sklearn scaler\n", + "\n", + "**Arguments:**\n", + "\n", + "- arr: 3D-array of shape(particle_number, timesteps, num_inputs)\n", + "- std_scalor: std_scaler of sklearn (default value = std_scalor defined above)\n", + "- min_max_scalor: min_max_scalor of sklearn (default value = min_max_scalor defined above)\n", + "- scalerfunc: string of the name of the scalerfunction to be used (default value = \"minmax\")\n", + "- scalor: sklearn scaler, if a scaler is given the array arr will be shaped and scaled with scalor (default value false)\n", + "\n", + "**Returns:**\n", + "\n", + "- arr: Shaped and transformed 3D-array of shape(particle_number, timesteps, num_inputs)\n", + "\n", + "**Additional comments:**\n", + "\n", + "By default included values for scalerfunc are \"minmax\" and \"std\" for a min_max_scaler and a std_scaler respectively.\n", + "For other scalers a scaler has to be given scalor argument.\n", + "\n", + "***\n", + "\n", + "### scaler_inv()\n", + "\n", + "**Description:**\n", + "\n", + "Shapes and transforms the data back to its original shape with a given sklearn scaler\n", + "\n", + "**Arguments:**\n", + "\n", + "- arr: 3D-array of shape(particle_number, timesteps, num_inputs)\n", + "- std_scalor: std_scaler of sklearn (default value = std_scalor defined above)\n", + "- min_max_scalor: min_max_scalor of sklearn (default value = min_max_scalor defined above)\n", + "- scalerfunc: string of the name of the scalerfunction to be used (default value = \"minmax\")\n", + "- scalor: sklearn scaler, if a scaler is given the array arr will be shaped and scaled with scalor (default value false)\n", + "\n", + "**Returns:**\n", + "\n", + "- arr: Shaped and inverse transformed 3D-array of shape(particle_number, timesteps, num_inputs)" + ] }, { "cell_type": "code", @@ -463,9 +543,6 @@ "metadata": {}, "outputs": [], "source": [ - "#Scale data either with MinMax scaler or with Standard scaler\n", - "#Return scalor if fit = True and and scaled array otherwise\n", - "\n", "def scaler(arr, std_scalor= std_scalor, min_max_scalor= min_max_scalor, scalerfunc= \"minmax\", scalor = False):\n", " \n", " if scalor != False:\n", @@ -484,8 +561,10 @@ " else:\n", " raise ValueError(\"Uknown scaler chosen: {}\".format(scalerfunc))\n", "\n", - "def scaler_inv(arr, std_scalor= std_scalor, min_max_scalor= min_max_scalor, scalerfunc= \"std\", scalor = False, num_inputs= 3):\n", + "def scaler_inv(arr, std_scalor= std_scalor, min_max_scalor= min_max_scalor, scalerfunc= \"std\", scalor = False):\n", "\n", + " num_inputs = arr.shape[2]\n", + " \n", " if scalor != False:\n", " arr = correct_array_steps(arr)\n", " arr = reshapor(scalor.inverse_transform(reshapor_inv(arr)))\n", @@ -525,14 +604,58 @@ ] }, { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create random minibatches\n", + "\n", + "***\n", + "\n", + "### unison_shuffled_copies()\n", + "\n", + "**Description:**\n", + "\n", + "Returns 2 3D-arrays that are shuffled in unison\n", + "\n", + "**Arguments:**\n", + "\n", + "- a: 3D-array\n", + "- b: 3D-array\n", + "\n", + "**Returns:**\n", + "\n", + "- a: 3D-array randomly shuffled in the \"first\" dimension\n", + "- b: 3D-array simultaneously shuffled as a in the \"first\" dimension\n", + "\n", + "**Additional comment:**\n", + "\n", + "Helperfunction for random_mini_batches()\n", + "\n", + "***\n", + "\n", + "### random_mini_batches()\n", + "\n", + "**Description:**\n", + "\n", + "Creates random minibatches in a list of a predefined size\n", + "\n", + "**Arguments:**\n", + "\n", + "- inputt: 3D-array of all the data that serves as the input for the RNN\n", + "- target: 3D-array of all the data that serves as the target for the RNN\n", + "- minibatchsize: Size of each minibatch (default value = 100)\n", + "\n", + "**Results:**\n", + "\n", + "- minibatches: A list containing all the minibatches with (input, target) per minibatch" + ] + }, + { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ - "###create random mini_batches###\n", - "\n", - "\n", "def unison_shuffled_copies(a, b):\n", " assert a.shape[0] == b.shape[0]\n", " p = np.random.permutation(a.shape[0])\n", @@ -577,7 +700,6 @@ "source": [ "#Create random minibatches of train and test set with input and target array\n", "\n", - "\n", "minibatches = random_mini_batches(train_set[:,:-1,:], train_set[:,1:,:], minibatch_size = 1000)\n", "#_train, _target = minibatches[0]\n", "test_input, test_target = test_set[:,:-1,:], test_set[:,1:,:]\n", @@ -614,11 +736,15 @@ "source": [] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], - "source": [] + "source": [ + "# RNN\n", + "\n", + "***\n", + "\n", + "## Class RNNPlacePrediction()" + ] }, { "cell_type": "code", @@ -1776,13 +1902,7 @@ "15057/15057 [==============================] - 10s 680us/step - loss: 0.5181 - acc: 0.7638 - val_loss: 0.5245 - val_acc: 0.7639\n", "Epoch 108/500\n", "15057/15057 [==============================] - 10s 672us/step - loss: 0.5165 - acc: 0.7638 - val_loss: 0.5243 - val_acc: 0.7639\n", - "Epoch 109/500\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ + "Epoch 109/500\n", "15057/15057 [==============================] - 10s 693us/step - loss: 0.5165 - acc: 0.7638 - val_loss: 0.5233 - val_acc: 0.7639\n", "Epoch 110/500\n", "15057/15057 [==============================] - 11s 730us/step - loss: 0.5146 - acc: 0.7639 - val_loss: 0.5233 - val_acc: 0.7639\n",