{"id":492,"date":"2018-10-02T17:05:58","date_gmt":"2018-10-02T16:05:58","guid":{"rendered":"http:\/\/ri.itservices.manchester.ac.uk\/csf3\/?page_id=492"},"modified":"2025-08-01T10:42:19","modified_gmt":"2025-08-01T09:42:19","slug":"keras","status":"publish","type":"page","link":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/software\/applications\/keras\/","title":{"rendered":"Keras"},"content":{"rendered":"<h2>Overview<\/h2>\n<p><a href=\"https:\/\/keras.io\/\">Keras<\/a> is a high-level neural networks API, written in Python that can run on top of <a href=\"\/csf3\/software\/applications\/tensorflow\/\">TensorFlow<\/a> installed on the CSF.<\/p>\n<h2>Restrictions on use<\/h2>\n<p>There are no restrictions on accessing this software on the CSF. The software is released under the <a href=\"https:\/\/github.com\/fchollet\/keras\/blob\/2.0.8\/LICENSE\">MIT License<\/a> and all usage must adhere to that license.<\/p>\n<h2>Set up procedure<\/h2>\n<p>Since version 2.4, Keras has been <a href=\"https:\/\/keras.io\/about\/#installation-amp-compatibility\">packaged within<\/a> Tensorflow as tensorflow.keras. To load Keras please load <em>one<\/em> of the following modulefiles:<\/p>\n<pre># Backend is Tensorflow 2.4.0 (Python 3.7):\r\nmodule load apps\/binapps\/tensorflow\/2.4.0-37-cpu\r\nmodule load apps\/binapps\/tensorflow\/2.4.0-37-gpu<\/pre>\n<p>To access the older versions of this software you must first load <em>one<\/em> of the following modulefiles:<\/p>\n<pre># Backend is Tensorflow 1.14.0 (Python 3.6) and the tf modulefile will be loaded for you:\r\nmodule load apps\/binapps\/keras\/2.2.4-tensorflow-gpu      # GPU version of tensorflow 1.14.0\r\nmodule load apps\/binapps\/keras\/2.2.4-tensorflow-cpu      # CPU version of tensorflow 1.14.0\r\n\r\n# Backend is Tensorflow 1.11.0 (Python 3.6) and the tf modulefile will be loaded for you:\r\nmodule load apps\/binapps\/keras\/2.2.2-tensorflow-gpu      # GPU version of tensorflow 1.11.0\r\nmodule load apps\/binapps\/keras\/2.2.2-tensorflow-cpu      # CPU version of tensorflow 1.11.0\r\n\r\n# Backend is Tensorflow but <strong>no<\/strong> tensorflow modulefile loaded. You <strong>must<\/strong>\r\n# load a tensorflow modulefile <em>first<\/em>. Check the <a href=\"..\/tensorflow\">list of available versions<\/a> on CSF.\r\nmodule load apps\/binapps\/keras\/2.2.4\r\nmodule load apps\/binapps\/keras\/2.2.2\r\n<\/pre>\n<p>The keras modulefile will automatically load the tensorflow and anaconda python modulefiles for you unless otherwise indicated.<\/p>\n<h2>Running the application<\/h2>\n<p>Please do not run Keras (via python) on the login node. Jobs should be submitted to the compute nodes via batch.<\/p>\n<h3>Interactive use on a Backend Node<\/h3>\n<p>To request an interactive session on a backend compute node run:<\/p>\n<pre>\r\n# CPU-only jobs (a one-hour interactive session)\r\nsrun -p interactive -t 0-1 --pty bash\r\n  # Wait until you are logged in to a compute-node, then:\r\n  module load apps\/binapps\/keras\/2.2.4-tensorflow-cpu\r\n\r\n  python\r\n  &gt;&gt;&gt; [type your python code interactively]\r\n\r\n  # or run a python script you are developing, for example\r\n  python <em>myscript.py<\/em>\r\n\r\n# GPU jobs (you may use up to 4 GPUs depending on your granted access to the GPUs)\r\nsrun -p gpuV -G 1 -t 0-1 --pty <strong>bash<\/strong>\r\n  # Wait until you are logged in to a gpu-node, then:\r\n  module load apps\/binapps\/keras\/2.2.4-tensorflow-gpu\r\n\r\n  python\r\n  &gt;&gt;&gt; [type your python code interactively]\r\n\r\n  # or run a python script you are developing, for example\r\n  python <em>myscript.py<\/em>\r\n<\/pre>\n<p>An example Keras session is given below.<\/p>\n<p>If there are no free <em>interactive<\/em> resources the <code>srun<\/code> command will ask you to try again later. Please do not run Keras (python) on the login node. Any jobs running there will be killed without warning.<\/p>\n<h3>Example Script (for CPU or GPU)<\/h3>\n<p>The following skeleton script can be used in an interactive session or in a batch job (from a jobscript). It ensures tensorflow does not use more cores than you have requested in your jobscript. If run on a GPU-node, it will automatically use the GPU.<\/p>\n<pre># Some of the following code has been taken from the Keras examples at:\r\n# https:\/\/keras.io\/getting-started\/sequential-model-guide\/\r\n\r\nfrom keras import backend as K\r\nfrom keras.models import Sequential\r\nfrom keras.layers import Dense, Dropout\r\nimport numpy as np\r\nimport tensorflow as tf\r\nimport os\r\n\r\n# Get number of cores reserved by the batch system\r\n# ($SLURM_NTASKS is set by the batch system, or use 1 otherwise)\r\nNUMCORES=int(os.getenv(\"SLURM_NTASKS\",1))\r\nprint(\"Using\", NUMCORES, \"core(s)\" )\r\n\r\n# Create TF session using correct number of cores.\r\n# NOTE:\r\n#   If you are using the GPU version, this will also automatically use\r\n#   the GPU and set up the Keras CUDA libraries. Please see the CSF3\r\n#   tensorflow page for other GPU-specific tf.ConfigProto() entries.\r\n\r\nsess = tf.Session(config=tf.ConfigProto(inter_op_parallelism_threads=NUMCORES,\r\n   allow_soft_placement=True, device_count = {'CPU': NUMCORES}))\r\n\r\n# Set the Keras TF session\r\nK.set_session(sess)\r\n\r\n<strong># Replace the rest of the script with your own code<\/strong>\r\n\r\n# Generate dummy data\r\nx_train = np.random.random((1000, 20))\r\ny_train = np.random.randint(2, size=(1000, 1))\r\nx_test = np.random.random((100, 20))\r\ny_test = np.random.randint(2, size=(100, 1))\r\n\r\n# MLP for binary classification example\r\nmodel = Sequential()\r\nmodel.add(Dense(64, input_dim=20, activation='relu'))\r\nmodel.add(Dropout(0.5))\r\nmodel.add(Dense(64, activation='relu'))\r\nmodel.add(Dropout(0.5))\r\nmodel.add(Dense(1, activation='sigmoid'))\r\n\r\nmodel.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])\r\nmodel.fit(x_train, y_train, epochs=20, batch_size=128)\r\nscore = model.evaluate(x_test, y_test, batch_size=128)\r\nprint(\"Score\", score)\r\n<\/pre>\n<h3>Serial CPU with a GPU batch job submission<\/h3>\n<p>Make sure you have the modulefile loaded then create a batch submission script, for example:<\/p>\n<pre>\r\n#!\/bin\/bash --login\r\n#SBATCH -p gpuV       # Run on the v100 GPU nodes\r\n#SBATCH -G 1          # Use one GPU\r\n#SBATCH -n 1          # (or --ntasks=) Number of CPU cores\r\n#SBATCH -t 2-0        # Wallclock time limit (2-0 is two day, max permitted is 4 days.)\r\n\r\n# We now recommend loading the modulefile in the jobscript (change version as required)\r\nmodule load apps\/binapps\/keras\/2.2.4-tensorflow-cpu     # or use the -gpu version\r\n\r\n# $SLURM_NTASKS is automatically set to the number of cores requested on the pe line\r\n# and can be read by your python code.\r\nexport OMP_NUM_THREADS=$SLURM_NTASKS\r\npython my-script.py\r\n<\/pre>\n<p>Submit the jobscript using:<\/p>\n<pre>sbatch <em>scriptname<\/em><\/pre>\n<p>where <em>scriptname<\/em> is the name of your jobscript.<\/p>\n<h3>Parallel CPU with a GPU batch job submission<\/h3>\n<p>Ensure you have loaded the correct modulefile and then create a jobscript similar to the following:<\/p>\n<pre>#!\/bin\/bash --login\r\n#SBATCH -p gpuV       # Run on the v100 GPU nodes\r\n#SBATCH -G 1          # Use one GPU\r\n#SBATCH -n 8          # (or --ntasks=) Number of CPU cores (can be up to 8 per v100 GPU)\r\n#SBATCH -t 2-0        # Wallclock time limit (2-0 is two day, max permitted is 4 days.)\r\n\r\n# We now recommend loading the modulefile in the jobscript (change version as required)\r\nmodule load apps\/binapps\/keras\/2.2.4-tensorflow-cpu     # or use the -gpu version\r\n\r\n# $SLURM_NTASKS is automatically set to the number of cores requested on the pe line\r\n# and can be read by your python code.\r\nexport OMP_NUM_THREADS=$SLURM_NTASKS\r\npython my-script.py\r\n<\/pre>\n<p>The above <code>my-script.py<\/code> example will get the number of cores to use from the <code>$SLURM_NTASKS<\/code> environment variable.<\/p>\n<p>Submit your jobscript using<\/p>\n<pre>sbatch <em>jobscript<\/em><\/pre>\n<p>where <code><em>jobscript<\/em><\/code> is the name of your jobscript.<\/p>\n<h2>Further info<\/h2>\n<ul>\n<li><a href=\"https:\/\/keras.io\">Keras website<\/a><\/li>\n<li><a href=\"\/csf3\/software\/applications\/tensorflow\/\">TensorFlow CSF page<\/a><\/li>\n<\/ul>\n<h2>Updates<\/h2>\n<p>None.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Keras is a high-level neural networks API, written in Python that can run on top of TensorFlow installed on the CSF. Restrictions on use There are no restrictions on accessing this software on the CSF. The software is released under the MIT License and all usage must adhere to that license. Set up procedure Since version 2.4, Keras has been packaged within Tensorflow as tensorflow.keras. To load Keras please load one of the following.. <a href=\"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/software\/applications\/keras\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":86,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-492","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":20,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/492\/revisions"}],"predecessor-version":[{"id":10762,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/492\/revisions\/10762"}],"up":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/86"}],"wp:attachment":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/media?parent=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}