Try using tensorflow ① Build python environment and introduce tensorflow

Introduction

When I tried to use python on my previous computer, I had a lot of trouble building the environment.

As a result of searching various things on my own, I found that it was terrible, such as building by mixing various methods and getting a lot of warning sentences. .. .. ..

For those who read this article, I would like to introduce you so that you can easily build the environment of python and use tensorflow.

Contents of this time

-Enable Python with Anaconda. -Create an execution environment with conda. -Install tensorflow in the execution environment, write sample code to see if it can be used, and execute it.

I would like to proceed with this kind of content.

What is tensorflow?

I checked it on the tensorflow homepage.

An open-source software library for Machine Intelligence ~~ Open source software for artificial intelligence ~~

... apparently ...

Official site: Tensorflow

This time, we will use anaconda, which will be introduced later, to make various settings, so you do not have to install it at this time.

Execution environment

MAC OS:Sierra 10.12.5 python:3.5.2(Anaconda 4.2.0) tensorflow:1.1

By the way, according to the official Tensorflow document at the end of June 2017,

Prerequisite: Python

In order to install TensorFlow, your system must contain one of the following Python versions: ~~ Before installing tensorflow, your system must include the python version below ~~

Python 2.7 Python 3.3+

That's why you need 3.3 or higher.

If you are interested, please enter the following command in ** Terminal ** and check if the version is correct.

What is a terminal! !! !!

It is natural to know the terminal when studying other languages to some extent, and there is no explanation as to whether the environment construction in Qiita's article is done with python.

So this time I will also introduce how to open the terminal

Please check the following page as it is introduced in another article.

How to use the terminal

Now, open a terminal and enter the following command.

Your macbook-Pro:~Your username$ python 
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default,date) 
〜〜〜〜
〜〜〜〜
>>>

** * If you want to exit the python interpreter, enter ctr + d. ** **

By the way, when I checked, my python was 3.5.

You can also check with the following command.

Your macbook-Pro:~Your username$python --version 
Python 3.5.2 :: Anaconda 4.2.0 (x86_64)

Python2 series is installed on mac by default.

If you do not have the Python 3 series and conda introduced this time installed, go to the bottom and install Anaconda.

What is Anaconda?

According to the homepage of Anaconda

Anaconda is the leading open data science platform powered by Python. The open source version of Anaconda is a high performance distribution of Python and R and includes over 100 of the most popular Python, R and Scala packages for data science. ~~ It's a python data science platform. ~~ ~~ There are a lot of packages. ~~

... apparently ...

According to Anaconda's Wikipedia

Anaconda is an operating system installer used by CentOS and Fedora. It is implemented in Python and C. PyGTK is used for the GUI front end, and Python-newt is used for the text mode front end.

In other words, if it is true, it is a package that contains all the libraries that you have to install one by one.

It also contains the conda required to create a python3 system or virtual environment.

Install Anaconda

If you install it, anaconda will do it for you, so I will omit it this time except for the outline.

Official: DOWNLOAD ANACONDA NOW

·procedure

1: Install the Anaconda installer

2: Open the anaconda installer

3: Drag & drop "Anaconda-Navigator" in the installed package to the application directory.

4: Check if it is installed and python3 can be used.

As I mentioned earlier, let's check the python version after installation.

If it is still python2 system even if you check it with the interpreter, it is possible that the PATH does not pass, so enter the following command.

Your MacBook-Pro: ~ your username $ export PATH = / Users / your username / anaconda / bin: $ PATH

Please specify the user name of the mac you are using in the "Your user name" part.

Let's start the python interpreter in the terminal again.

Enable Tensorflow with conda

・ What is conda?

conda is a package management system and environment management system made primarily for Python.

It is not released as a standalone product, but is included in Anaconda, Anaconda server, and Miniconda.

Recently, it is popular because various packages can be easily installed to build a numerical calculation environment for Python.

It's conda, not Neda.

By introducing conda, it is possible to create various virtual environments.


Enter the following command in the terminal to create an execution environment in conda

Now, enter the following command to open the virtual environment of conda.

conda create -n environment name you want to create python = your python version

This time, we will create a virtual environment named tensorflow using python3.5, so the command will be as follows.

$ conda create -n tensorflow python=3.5

After entering the command and completing the installation, an execution environment named "tensorflow" that can use tensorflow will be created.

We don't have tensorflow at this point, so we don't have to create it with this name.

It is also easy to understand if you set the name by version etc.

Besides, it is possible to build a virtual environment with pyenv etc., so if you prefer that, please consider the following (^^)

·reference

1000ch: Building a Python environment on Mac using pyenv

Move to the execution environment named "tensorflow" created by conda

You can move (activate) the execution environment created by conda by entering the following command. $ source activate The environment name created earlier`

I created a virtual environment with the name tensorflow, so the command to enter is as follows. $ source activate tensorflow

If successful, the name of tensorflow will appear on the left side as shown below. (tensorflow) Your MacBook-Pro: ~ Your username $

・ When exiting the execution environment

Enter the following command to return to the original hierarchy from the execution environment (deactivate). $ source deativate tensorflow

The next step is to install tensorflow.

Download tensorflow with condaforge

Install tensorflow with the following command.

* Before installing, please enter the virtual environment for tensorflow created earlier.

$ conda install -c conda-forge tensorflow

mkl:        2017.0.1-0     
  mock:       2.0.0-py35_0   conda-forge
    numpy:      1.13.0-py35_0             
    pbr:        3.1.1-py35_0   conda-forge
    protobuf:   3.3.0-py35_2   conda-forge
    six:        1.10.0-py35_1  conda-forge
    tensorflow: 1.1.0-py35_0   conda-forge
    werkzeug:   0.11.10-py35_0 conda-forge           
Proceed ([y]/n)? 

I was asked to enter Yes or No, so I chose yes.

Test if you have successfully installed

Open a terminal and check if the tensorflow you just installed is installed.

///① Start python in the execution environment
(tensorflow)Your macbook-Pro:~Your username$ python
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default,Today's date
・ ・ ・(Various come out)
・ ・ ・
///② Import tensorflow.(If you can import it, you have installed it.)
>>>import tensorflow as tf
///③ Again as shown below>>>If appears, you can install it.
>>>

* If you have not moved to the execution environment or have not installed it, the following warning will be displayed.
>>>import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'tensorflow'

It may be possible to solve it, so if you can not import it, please comment with copy and paste.

Write the code that outputs Hello Tensorflow! Using tensorflow.

python


>>>import tensorflow as tf
>>>hello = tf.constant("Hello Tensorflow!")
>>>sess = tf.Session()  //Note that Session has an uppercase letter
>>>print(sess.run(hello)) //Run the hello you just created in session
b'Hello Tensorflow!'

↑ If it is output like this, it is proof that you can use tensorflow! !! !!

Commentary

1: Import tensorflow in the first line and set it to be called with the name tf. 2: Create a constant that outputs "Hello Tensorflow!" With the name hello 3: Create sess using Session to execute 4: Execute hello using sess and output with print.

The TensorFlow graph is a description of the calculation. Whatever calculation you do, you need to run the graph inside a Session. Session provides a way to place graph ops on devices such as CPUs and GPUs and execute them.

Source: Run TensorFlow from the command line

I don't know the detailed movement, but it means that you have to use session.

Finally

I would appreciate it if you could comment if there are any mistakes in the article (^^)

Next time, I will write about MNIST For ML Bigginers in the tensorflow tutorial!

Thank you very much!

Recommended Posts

Try using tensorflow ① Build python environment and introduce tensorflow
Build and try an OpenCV & Python environment in minutes using Docker
Python environment construction and TensorFlow
python / tensorflow beginners build jupyter + tensorflow environment and do Hello World
Build Python3 and OpenCV environment on Ubuntu 18.04
Build Python + django + nginx + MySQL environment using docekr
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Try to build python and anaconda environment on Mac (by pyenv, conda)
Try using Tweepy [Python2.7]
Build PyPy and Python execution environment with Docker
Try using ChatWork API and Qiita API in Python
Try creating a compressed file using Python and zlib
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a Python virtual environment using venv (Django + MySQL ①)
Build a Python environment on your Mac using pyenv
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a Python development environment using pyenv on MacOS
[Python] Try using Tkinter's canvas
Try using Kubernetes Client -Python-
Build python3 environment with ubuntu 16.04
Build Python environment on Windows
Build python environment with direnv
[ev3dev × Python] Build ev3dev development environment
Build python environment on windows
Try using pytest-Overview and Samples-
Build a Python environment offline
Try to make it using GUI and PyQt in Python
Build an environment for machine learning using Python on MacOSX
Building a Python environment on a Mac and using Jupyter lab
Build a Python environment and transfer data to the server
Environment construction of python and opencv
Authentication using tweepy-User authentication and application authentication (Python)
Build a go environment using Docker
Build Mysql + Python environment with docker
Try using Pleasant's API (python / FastAPI)
Build TOPPERS / ASP3 and try it
Python environment construction (pyenv, anaconda, tensorflow)
install tensorflow in anaconda + python3.5 environment
Clustering and visualization using Python and CytoScape
Try using Python argparse's action API
Try using the Python Cmd module
Build Python 3.8 + Pipenv environment on Ubuntu 18.04
Build a python3 environment on CentOS7
Introduce Python 3.5.2 environment on Amazon Linux
Try using Leap Motion in Python
Python3.6 environment construction (using Win environment Anaconda)
Try using Amazon DynamoDB from Python
How to build a LAMP environment using Vagrant and VirtulBox Note
Build a Chainer environment using CUDA and cuDNN on a p2 instance
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
Build a local development environment for Lambda + Python using Serverless Framework
Build a game leaderboard on Alibaba cloud using Python and Redis
Build a Python environment on your Mac with Anaconda and PyCharm
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Build a Python execution environment using GPU with GCP Compute engine
How to build Python and Jupyter execution environment with VS Code
Notes using cChardet and python3-chardet in Python 3.3.1.
code-server Local environment (3) Try using VSCode Plugin
Try using the Wunderlist API in Python