python / tensorflow beginners build jupyter + tensorflow environment and do Hello World

Overview

Amazon.co.jp: TensorFlow has started! Latest Google Machine Learning (Next Publishing) E-book: Keiji Ariyama: Kindle Store

It seems to be difficult for beginners to operate vim etc. when doing, so it is a procedure memo that makes it easy to operate on a notebook called jupyter. (* Although you can operate the terminal on the way!)

** jupyter image **

2016-09-10_14_58_35.png

Docker image that can use TensorFlow with Jupyter --Memomemo

The method using docker is the quickest, but the macbook air that I always use is various and it is not good to use docker for mac, so I installed it normally ... The background is

Below is a simple flow

There is a procedure to do pyenv-virtualenv to make pyenv convenient, but I will omit it because there is no point in asking for it.

Prerequisites / Knowledge

Preparing the python environment

Instead of using system python common to all users, install env-based pyenv. (Recently, it is common to use env system to isolate the environment for both ruby and node).

Now you can create different versions of the python environment for running Tensorflow without affecting anything else.

Install pyenv using anyenv

Since I always use multiple ** env systems, I install anyenv that can manage them all at once, and install pyenv from there (I like this. You can put pyenv directly without any problem). The reason for doing this is that zshrc (or bashrc) gets dirty with a lot of env settings ...

anyenv installation

Clone the repository by entering the following command

git clone https://github.com/riywo/anyenv ~/.anyenv

After that, add the following settings to bashrc etc. according to your environment so that anyenv is loaded when the shell starts.

.bashrc


if [ -d $HOME/.anyenv ] ; then
    export PATH="$HOME/.anyenv/bin:$PATH"
    eval "$(anyenv init -)"

In my dotfiles I set it like this https://github.com/kegamin/prezto/blob/master/runcoms/zshrc#L141-L143

After entering the settings, restart and reload the shell with ʻexec $ SHELL -l etc. to use ʻanyenv. After that, execute ʻanyenv version`, and if you can execute it like the following, it's OK

❯ anyenv version
ndenv: v5.6.0 (set by /Users/user/.anyenv/envs/ndenv/version)
plenv: system (set by /Users/user/.anyenv/envs/plenv/version)
pyenv: 3.5.1 (set by /Users/user/.anyenv/envs/pyenv/version)
rbenv: 2.3.0 (set by /Users/user/.anyenv/envs/rbenv/version)

Install pyenv

#Install pyenv
 ❯ anyenv install pyenv

#List of python versions that can be installed
 ❯ pyenv install -l
Available versions:
(Omission)
  3.5.0
  3.5-dev
  3.5.1     #->Put this this time
  3.6.0a1
  3.6-dev
(Abbreviation)
 ❯ pyenv install 3.5.1
 #Standardize installed python
 ❯ pyenv global 3.5.1
 #Check version(*The python version currently in use)
 ❯ pyenv versions
  system
* 3.5.1 (set by /Users/user/.anyenv/envs/pyenv/version)  

install jupyter

 # install
 ❯ pip install jupyter matplotlib
#Start-up
 ❯  jupyter notebook
[I 13:47:54.142 NotebookApp] Serving notebooks from local directory: /Users/user/Dropbox/notebook
[I 13:47:54.142 NotebookApp] 0 active kernels
[I 13:47:54.142 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/
[I 13:47:54.142 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

Install tensorflow

From Download and Setup, check the URL of the current latest version of tensorflow and install it.

Regarding the Mac version, there are the following two types, but if you are not sure, it is better to select CPU only (If you have a mac with GPU, it is better to select GPU enabled, it will process faster using GPU However, I will omit it this time, but more things will be installed).

Mac OS X, CPU only, Python 3.4 or 3.5: Mac OS X, GPU enabled, Python 3.4 or 3.5:

# 2016/09/10 Currently 0.10 is the latest Ver
$ pip install -U https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.10.0-py3-none-any.whl

Start and play

#Create a directory to save the created tensorflow data (any location))
 ❯ mkdir tensorflow                                                      
 ❯ ls
 tensorflow
#Specify the created directory and start jupyter
 ❯ jupyter notebook tensorflow
[I 14:38:42.255 NotebookApp] Serving notebooks from local directory: /Volumes/usb128/jupyter/tensorflow
[I 14:38:42.255 NotebookApp] 0 active kernels
[I 14:38:42.255 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/
[I 14:38:42.255 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

After executing jupyter notebook, when you start http: // localhost: 8080 with a browser, the screen below is displayed.

 2016-09-10 14.40.58.png

** You did it! ** **

Create a note that can run python3

Now, let's actually execute tensorflow. As shown in the image below, press New on the right and select python 3 in it.

2016-09-10_14_42_43.png

Then, the input screen will appear as shown below, so try inserting the source code.

 2016-09-10 15.06.26.png

The following source code is input for each, but if you press SHIFT + Enter on the source code after inputting, the execution result will be output immediately below (it may take some time).

import tensorflow as tf

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

a = tf.constant(10)
b = tf.constant(20)
print(sess.run(a + b))

jupyter is variously sophisticated and fun, but this time I will omit the operation method etc.

enjoy! TensorFlow!

Referenced site

pyenv-virtualenv + TensorFlow environment setting memorandum [addition / correction] --Qiita

Recommended Posts

python / tensorflow beginners build jupyter + tensorflow environment and do Hello World
Try using tensorflow ① Build python environment and introduce tensorflow
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
Python environment construction and TensorFlow
Python #Hello World for super beginners
How to build Python and Jupyter execution environment with VS Code
Build Jupyter Lab (Python) environment with Docker
Build Python3 and OpenCV environment on Ubuntu 18.04
Hello World and face detection with OpenCV 4.3 + Python
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
First python ① Environment construction with pythonbrew & Hello World !!
Build PyPy and Python execution environment with Docker
Install Python3 on Mac and build environment [Definitive Edition]
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a python virtual environment with virtualenv and virtualenvwrapper
Building a Python environment on a Mac and using Jupyter lab
Build python3 environment with ubuntu 16.04
How to learn TensorFlow for liberal arts and Python beginners
Build python environment with direnv
Python3 environment construction (for beginners)
Build a Python environment and transfer data to the server
Build python environment on windows
Hello World (beginners) on Django
Python starting with Hello world!
[Sakura Rental Server] (For beginners) How to build an environment for Python, pyenv, and Flask. | For csh
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
python3.8 venv environment jupyter notebook
Build and test a CI environment for multiple versions of Python
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Python beginners tried Hello World in 30 seconds using the micro-framework Flask
Build a Python environment on your Mac with Anaconda and PyCharm
Build and try an OpenCV & Python environment in minutes using Docker
Environment construction of python and opencv
Let's do "Hello World" in 40 languages! !!
Build python virtual environment with virtualenv
Introduction to TensorFlow --Hello World Edition
Build Mysql + Python environment with docker
Python environment construction (pyenv, anaconda, tensorflow)
install tensorflow in anaconda + python3.5 environment
[Note] Hello world output with python
cout << "Hello, World! \ N" in python
Python3 TensorFlow for Mac environment construction
Build a python3 environment on CentOS7
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
C language to see and remember Part 1 Call C language from Python (hello world)
Try to build python and anaconda environment on Mac (by pyenv, conda)
Re: Heroku life begin with Flask from zero - Environment and Hello world -
Quickly build a python environment for deep learning and data science (Windows)
Build a "Deep learning from scratch" learning environment on Cloud9 (jupyter miniconda python3)
[Python] Web application from 0! Hands-on (2) -Hello World-
Hello World and face detection with opencv-python 4.2
Build an environment for Blender built-in Python
Python 2.7, 3.4, 3.5 extension module build environment on Windows
Method to build Python environment in Xcode 6
Build a python environment on MacOS (Catallina)
Python virtual environment and packages on Ubuntu
Build a Tensorflow environment with Raspberry Pi [2020]
How to build Hello, World on #Nix
I want to build a Python environment
Build the execution environment of Jupyter Lab
Build Python environment with Anaconda on Mac