Skip to content

Instantly share code, notes, and snippets.

@witt3rd
Last active June 23, 2018 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save witt3rd/567d1e52f1e2da4f45ed72e436dc4576 to your computer and use it in GitHub Desktop.
Save witt3rd/567d1e52f1e2da4f45ed72e436dc4576 to your computer and use it in GitHub Desktop.

Deep Learning Setup

Python

This assumes miniconda3 has been installed and is being used for environment and package management. See this gist for setup details.

Keras

Keras is a higher level library that uses various backends (e.g., Tensorflow, CLTK, ...).

We have setup a miniconda environment with the typical packages:

conda create -n keras python
source activate keras
conda install keras matplotlib

TensorFlow

TensorFlow is Google's deep learning framework, which can be configured to run on CPUs, GPUs, or Google Cloud.

Conda (Pip)

Vanilla install with Python, such as adding it to the keras environment (created above):

source activate keras
conda install tensorflow

macOS

Pre-built

Alternatively, you can download a macOS-optimized build from https://github.com/lakshayg/tensorflow-build and install it:

source activate keras
pip install --ignore-installed --upgrade tensorflow-1.8.0-cp36-cp36m-macosx_10_7_x86_64.whl

NOTE: as of TensorFlow v1.6.0, all CPU optimizations are included in the build, which may not be available on your Mac. This will result in failures like: illegal instruction: 4.

From source

Or, build it from source following this guide, which distills to:

brew install bazel coreutils
conda install six numpy wheel
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
./configure
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
source activate keras
pip install /tmp/tensorflow_pkg/tensorflow-1.9.0rc0-cp36-cp36m-macosx_10_7_x86_64.whl
pip install keras

Ubuntu

Have a lot of patience and work through NVIDIA driver updates, CUDA installs, and GPU-enabled Tensorflow build with this elaborate walkthrough.

Python integration

Instead of the last few steps in the Ubuntu install, activate the keras environment (created above) and install the whl using pip:

cd ~/tensorflow/tensorflow_pkg
source activate keras
pip install --ignore-installed --upgrade tensorflow-1.8.0-cp36-cp36m-linux_x86_64.whl 

TensorflowJS

via script tag

<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.6"> </script>

    <!-- Place your code in the script tag below. You can also use an external .js file -->
    <script>
      // Notice there is no 'import' statement. 'tf' is available on the index-page
      // because of the script tag above.

      // Define a model for linear regression.
      const model = tf.sequential();
      model.add(tf.layers.dense({units: 1, inputShape: [1]}));

      // Prepare the model for training: Specify the loss and the optimizer.
      model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

      // Generate some synthetic data for training.
      const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
      const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

      // Train the model using the data.
      model.fit(xs, ys, {epochs: 10}).then(() => {
        // Use the model to do inference on a data point the model hasn't seen before:
        // Open the browser devtools to see the output
        model.predict(tf.tensor2d([5], [1, 1])).print();
      });
    </script>
  </head>

  <body>
  </body>
</html>

via NPM for Node

# base package
npm install @tensorflow/tfjs
# non-gpu version
npm install @tensorflow/tfjs-node
# gpu version
npm install @tensorflow/tfjs-node-gpu

OpenAI Gym

https://gym.openai.com/

To get all environments, it is best to install from the repo.

Prerequisites

Linux

apt-get install -y python-numpy python-dev cmake zlib1g-dev libjpeg-dev xvfb ffmpeg xorg-dev python-opengl libboost-all-dev libsdl2-dev swig

macOS

brew install cmake boost boost-python sdl2 swig wget

Build and Install

git clone https://github.com/openai/gym.git
cd gym
source activate keras
pip install -e '.[all]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment