Skip to content

Instantly share code, notes, and snippets.

@mirrornerror
Last active November 1, 2018 14:36
Show Gist options
  • Save mirrornerror/6b2a26f320ebb7b09e5c4747fd484d3a to your computer and use it in GitHub Desktop.
Save mirrornerror/6b2a26f320ebb7b09e5c4747fd484d3a to your computer and use it in GitHub Desktop.
Keras random seed fix
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "## Keras fix a random seed"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-11-01T10:53:31.886148Z",
"end_time": "2018-11-01T10:53:31.904762Z"
},
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np\nimport tensorflow as tf\nimport random as rn\nimport os\n\nos.environ['PYTHONHASHSEED'] = '0'\nrandom_n = 123\nnp.random.seed(random_n)\nrn.seed(random_n)\nsession_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)\nfrom keras import backend as K\ntf.set_random_seed(random_n)\nsess = tf.Session(graph=tf.get_default_graph(), config=session_conf)\nK.set_session(sess)",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "py36",
"display_name": "py36",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.4",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "Keras random seed fix",
"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