Created
February 28, 2020 15:00
-
-
Save dqtu/b69c4b9fa309f61d48b05fafaa23d46d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-02-26T15:10:51.491530Z", | |
"start_time": "2020-02-26T15:10:48.694729Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"import tensorflow as tf\n", | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"\n", | |
"from tensorflow.keras.layers import Input, Dense\n", | |
"from tensorflow.keras.models import Model" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Unser Datensatz ist eine 4x4 Matrix, d.h. dass wir vier Dimensionen/Merkmale und vier Datenpunkte haben." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-02-26T15:13:26.876416Z", | |
"start_time": "2020-02-26T15:13:26.869880Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"X = np.array([\n", | |
" [[1., 0., 0., 0.]],\n", | |
" [[0., 1., 0., 0.]],\n", | |
" [[0., 0., 1., 0.]],\n", | |
" [[0., 0., 0., 1.]],\n", | |
" ] \n", | |
" )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-02-26T15:10:51.627538Z", | |
"start_time": "2020-02-26T15:10:51.542376Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"# Create models\n", | |
"inputs = Input(shape=(X.shape[1],X.shape[2]), name='Input')\n", | |
"encoded = Dense(2, activation='sigmoid', name='HiddenLayer')(inputs)\n", | |
"decoded = Dense(4, activation='sigmoid', name='Decoder')(encoded)\n", | |
"\n", | |
"# separate model for autoencoder and encoder only\n", | |
"encoder = Model(inputs, encoded, name='Encoder_Only')\n", | |
"autoencoder = Model(inputs, decoded, name='Autoencoder')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-02-26T15:10:51.663378Z", | |
"start_time": "2020-02-26T15:10:51.630392Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Model: \"Autoencoder\"\n", | |
"_________________________________________________________________\n", | |
"Layer (type) Output Shape Param # \n", | |
"=================================================================\n", | |
"Input (InputLayer) [(None, 1, 4)] 0 \n", | |
"_________________________________________________________________\n", | |
"HiddenLayer (Dense) (None, 1, 2) 10 \n", | |
"_________________________________________________________________\n", | |
"Decoder (Dense) (None, 1, 4) 12 \n", | |
"=================================================================\n", | |
"Total params: 22\n", | |
"Trainable params: 22\n", | |
"Non-trainable params: 0\n", | |
"_________________________________________________________________\n" | |
] | |
} | |
], | |
"source": [ | |
"autoencoder.compile(optimizer='adam', loss='mse')\n", | |
"autoencoder.summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-02-26T15:20:18.723377Z", | |
"start_time": "2020-02-26T15:20:14.401524Z" | |
}, | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<tensorflow.python.keras.callbacks.History at 0x1fd405beb08>" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"autoencoder.fit(X, X, epochs=3_000, verbose=0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-02-26T15:11:09.490195Z", | |
"start_time": "2020-02-26T15:11:09.320727Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[[1., 0., 0., 0.]],\n", | |
"\n", | |
" [[0., 1., 0., 0.]],\n", | |
"\n", | |
" [[0., 0., 1., 0.]],\n", | |
"\n", | |
" [[0., 0., 0., 1.]]], dtype=float32)" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Predict the train data with autoencoder\n", | |
"x_hat = np.around(autoencoder.predict(X))\n", | |
"x_hat" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Wie oben zu sehen ist, entspricht unser Ausgabesignal $\\mathbf{\\hat{x}}$ exakt unserem Eingabesignal $\\mathbf{x}$. Auch bei näherer Betrachtung des Encoders ist genau das eingetroffen, was wir uns vorher überlegt haben: unser Modell, hat unser Eingangssignal mit vier Dimensionen in ein Signal mit nur zwei Dimensionen umgewandelt!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-02-26T15:11:09.564765Z", | |
"start_time": "2020-02-26T15:11:09.493882Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[[0., 0.]],\n", | |
"\n", | |
" [[1., 1.]],\n", | |
"\n", | |
" [[0., 1.]],\n", | |
"\n", | |
" [[1., 0.]]], dtype=float32)" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Predict the train data with encoder only\n", | |
"np.around(encoder.predict(X))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-02-26T15:11:09.697323Z", | |
"start_time": "2020-02-26T15:11:09.572734Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Model: \"Encoder_with_Classifier\"\n", | |
"_________________________________________________________________\n", | |
"Layer (type) Output Shape Param # \n", | |
"=================================================================\n", | |
"Input (InputLayer) [(None, 1, 4)] 0 \n", | |
"_________________________________________________________________\n", | |
"HiddenLayer (Dense) (None, 1, 2) 10 \n", | |
"_________________________________________________________________\n", | |
"Classifier_Output (Dense) (None, 1, 4) 12 \n", | |
"=================================================================\n", | |
"Total params: 22\n", | |
"Trainable params: 22\n", | |
"Non-trainable params: 0\n", | |
"_________________________________________________________________\n" | |
] | |
} | |
], | |
"source": [ | |
"# Encoder part with classifier\n", | |
"classify = Dense(4, activation='softmax', name='Classifier_Output')(encoded)\n", | |
"\n", | |
"classifier = Model(inputs, classify, name='Encoder_with_Classifier')\n", | |
"classifier.compile(optimizer='adam',\n", | |
" loss='sparse_categorical_crossentropy',\n", | |
" metrics=['accuracy'])\n", | |
"classifier.summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-02-26T15:20:47.154934Z", | |
"start_time": "2020-02-26T15:20:40.746290Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<tensorflow.python.keras.callbacks.History at 0x1ff9b58d608>" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"classifier.fit(X, np.array([0, 1, 2, 3]), epochs=3_000, verbose=0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2020-02-26T15:11:25.410870Z", | |
"start_time": "2020-02-26T15:11:25.036253Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0, 1, 2, 3]" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Predicted label of the encoder and classifier model\n", | |
"[np.argmax(classifier.predict(np.array([X[i]]))) for i in range(0,4)]" | |
] | |
} | |
], | |
"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.6" | |
}, | |
"toc": { | |
"base_numbering": 1, | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
}, | |
"varInspector": { | |
"cols": { | |
"lenName": 16, | |
"lenType": 16, | |
"lenVar": 40 | |
}, | |
"kernels_config": { | |
"python": { | |
"delete_cmd_postfix": "", | |
"delete_cmd_prefix": "del ", | |
"library": "var_list.py", | |
"varRefreshCmd": "print(var_dic_list())" | |
}, | |
"r": { | |
"delete_cmd_postfix": ") ", | |
"delete_cmd_prefix": "rm(", | |
"library": "var_list.r", | |
"varRefreshCmd": "cat(var_dic_list()) " | |
} | |
}, | |
"types_to_exclude": [ | |
"module", | |
"function", | |
"builtin_function_or_method", | |
"instance", | |
"_Feature" | |
], | |
"window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment