Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uduse/84a07e3c33d3deb7be64d31e4d6d64ad to your computer and use it in GitHub Desktop.
Save uduse/84a07e3c33d3deb7be64d31e4d6d64ad to your computer and use it in GitHub Desktop.
Train seq2seq model in Keras without using one-hot encoded data
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-16T13:40:33.812957Z",
"start_time": "2018-04-16T13:40:32.813032Z"
},
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np\nimport keras\nimport keras.backend as K\nfrom keras.layers import *\nfrom keras.models import *\nimport random\n\nmax_len = 30\nvocab_size = 3000\nX = np.random.randn(500, max_len)\nY = np.random.randn(500, max_len)\n\ninput_ = Input(X.shape[1:])\nhidden = Embedding(vocab_size, 2)(input_)\noutput = Dense(vocab_size, activation='softmax')(hidden)\nm = Model(input_, output)\n\ndef loss(y_true, y_pred):\n y_true = K.one_hot(y_true, vocab_size)\n return keras.losses.categorical_crossentropy(y_true, y_pred)\ny_tensor = K.placeholder((None, max_len), dtype='int32')\nm.compile(loss=loss, optimizer='adam', target_tensors=[y_tensor])\n\nm.fit(X, Y, batch_size=128, epochs=10, validation_split=0.1)",
"execution_count": 4,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Train on 450 samples, validate on 50 samples\nEpoch 1/10\n450/450 [==============================] - 0s 412us/step - loss: 6.7245 - val_loss: 6.6977\nEpoch 2/10\n450/450 [==============================] - 0s 139us/step - loss: 6.7180 - val_loss: 6.6909\nEpoch 3/10\n450/450 [==============================] - 0s 139us/step - loss: 6.7111 - val_loss: 6.6839\nEpoch 4/10\n450/450 [==============================] - 0s 141us/step - loss: 6.7041 - val_loss: 6.6768\nEpoch 5/10\n450/450 [==============================] - 0s 139us/step - loss: 6.6969 - val_loss: 6.6696\nEpoch 6/10\n450/450 [==============================] - 0s 136us/step - loss: 6.6897 - val_loss: 6.6623\nEpoch 7/10\n450/450 [==============================] - 0s 136us/step - loss: 6.6823 - val_loss: 6.6549\nEpoch 8/10\n450/450 [==============================] - 0s 134us/step - loss: 6.6749 - val_loss: 6.6473\nEpoch 9/10\n450/450 [==============================] - 0s 133us/step - loss: 6.6673 - val_loss: 6.6396\nEpoch 10/10\n450/450 [==============================] - 0s 137us/step - loss: 6.6595 - val_loss: 6.6318\n"
},
{
"data": {
"text/plain": "<keras.callbacks.History at 0x7fd4a6a4fb70>"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"name": "mermaid",
"display_name": "mermaid",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"toc_cell": false,
"toc_position": {},
"toc_section_display": "block",
"toc_window_display": false
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
},
"gist": {
"id": "",
"data": {
"description": "Train seq2seq model in Keras without using one-hot encoded data",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment