Mastodon

Installing TensorFlow on macOS Sierra

After deciding to start diving into the wonderfully weird world of machine learning and artificial neural networks, I looked into a few libraries to try out, and ultimately chose TensorFlow, an open-source library developed by Google Brain for machine learning and deep neural network research. Perfect! TensorFlow is a Python library, and comes in two flavors: CPU-only support or GPU support. TensorFlow and most neural nets run far more efficiently on GPUs thanks to the parallel architecture of graphics processors. It’s recommended to first attempt a vanilla install of TensorFlow, before adding GPU support, so we’ll start there.

Of the variety of avenues available to install TensorFlow, virtualenv is recommended, as it isolates the installation to a virtual environment, preventing other Python programs from affecting it or being affected themselves. TensorFlow is available for both Python 2 and 3, and although Python 3 is faster, while experimenting with it I found myself experiencing more bugs than when using Python 2, such as string literals being expressed as byte literals, among other syntax errors.

Installing Python

So, if you already have your preferred version of Python installed, feel free to skip this section. Otherwise, let’s start with checking GCC. Command Line Tools installs the needed GCC complier. To install Python, you can either download it directly, or use Homebrew, an open-source package manager. To install Homebrew, fire up Terminal and enter the following:

$
/usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

We’ll need to prepend our $PATH variable so Homebrew installed programs are used before any system installations.

$
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile

It never hurts to verify that an installation went smoothly before proceeding, so restart terminal or source ~/.bash_profile and enter the following:

$
brew doctor

Now we can install Python:

$
brew install python

To verify our installation:

$
which python

You should see an output referencing /usr/local/bin/python.

Pip is the default package management system for Python, and we’ll be using it to install virtualenv. Pip and setuptools come preinstalled with Python 2.7 and above, but we’ll want to make sure they’re up to date.

$
pip install --upgrade pip setuptools

Installing Virtualenv

Now we have to create the virtual environment in which TensorFlow will run. First, let’s install virtualenv:

$
pip install virtualenv

Next we’ll make a directory for the virtual environment and TensorFlow install to live in. I just made a folder named “TensorFlow” in my home folder:

$
mkdir TensorFlow

Finally, to create the virtual environment:

$
virtualenv -–system-site-packages ~/TensorFlow

The last argument of this command is the actual path to the folder you’ve created. Our next step is to activate the virtual environment we’ve installed:

$
source ~/TensorFlow/bin/activate

Installing TensorFlow

Now that we’re inside our active virtual environment, it’s finally time to install TensorFlow:

$
pip install tensorflow

After TensorFlow has successfully installed, we’re going to, you guessed it! Validate the installation. Close out of your virtual environment by simply entering the following command:

$
deactivate

Fire up a new terminal shell and activate the virtual environment again using the “source” command from earlier. You should see "()" preceding the usual terminal prompt, indicating it’s active.

If it is, go ahead and invoke Python:

$
python

Try this short “Hello, TensorFlow!” program:

import tensorflow as tf
hello = tf.constant("Hello, TensorFlow!")
sess = tf.Session()
print(sess.run(hello))

If all went well, you should see:

$
Hello, TensorFlow!

During my execution of this test program, the compiler warned of multiple libraries that weren’t compiled that could speed up CPU computation if they were however, building TensorFlow from Source is the only way to compile these libraries. For more information about installation and troubleshooting, check out TensorFlow’s installation guide.

❋❋❋

Configuring ddclient with Google Domains on macOS

Dynamic DNS gives you all of the advantages of a domain name without the hassle of a static IP address. Its principle is simple; a client on the host detects changes to your external IP address and updates the associated DNS record. One popular client available on macOS is ddclient. Here’s how to set it up with Google Domains.

Installing ddclient

Homebrew is the easiest way to install ddclient, and has the added feature of providing updates. If you haven’t already, installing Homebrew is as simple entering a single line into Terminal:

$
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then install ddclient:

$
brew install ddclient

Next we need to add a synthetic record in Google Domains. Find the domain you wish to use and select “Manage.” Go to “DNS” in the navigation stack on the left, and scroll down to “Synthetic Records.” In the dropdown menu on the left, select “Dynamic DNS,” then enter the desired subdomain name, and add the record. Expand the newly-added record and select “View credentials” to get your username and password.

The username and password lives in a configuration file, ddclient.conf, located in /usr/local/etc/ddclient. A sample version of the file can be found in /usr/local/opt/ddclient/share/doc/ddclient. For Google Domains, it’s pretty simple. Here’s an example of mine:

ssl=yes
protocol=googledomains
use=web, web=checkip.dyndns.org/, web-skip='IP Address'
login=<generated_login>
password=<generated_password>
home.paysonwallach.com

The above configuration queries checkip.dyndns.org for your external IPv4 address. We should limit our daemon to query at most every 10 minutes (600 seconds) so our IP address doesn’t get blocked. Note that we don’t specify a time interval here. Although we could easily make our own launch daemon, Homebrew has already done the work for us. All we need to do is set our desired interval for the StartInterval key in /Library/LaunchDaemons/homebrew.mxcl.ddclient.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.mxcl.ddclient</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/ddclient/sbin/ddclient</string>
<string>-file</string>
<string>/usr/local/etc/ddclient/ddclient.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>600</integer>
<key>WatchPaths</key>
<array>
<string>/usr/local/etc/ddclient</string>
</array>
<key>WorkingDirectory</key>
<string>/usr/local/etc/ddclient</string>
</dict>
</plist>

To start the launch daemon:

$
sudo brew services start ddclient

And there you have it! Assuming you’ve properly configured port-forwarding, you can now access your Mac from anywhere with your own domain name.

❋❋❋