2021年1月5日 星期二

Anaconda 安裝虛擬環境套件 tfecon 與 TensorFlow

我在下面這本書中看到作者於 Anaconda 環境中使用了 tfecon 這個虛擬環境套件, 與我平常使用的 virtualenv 套件不同, 所以今天就來試試, 並在此虛擬環境下安裝 TensorFlow. 



Source : 天瓏


首先開啟 Anaconda prompt 命令列視窗檢查 Python 版本 : 

(base) C:\Users\user>python   
Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 

可見目前 Anaconda 內的 Python 版本為 3.8.5, 這樣就可以用下列指令在線安裝 tfecon 套件了 : 

conda create -n tfecon python=3.8.5   

(base) C:\Users\user> conda create -n tfecon python=3.8.5    

安裝完成後會提示啟動與關閉虛擬環境之指令 : 

done
#
# To activate this environment, use
#
#     $ conda activate tfecon   
#
# To deactivate an active environment, use
#
#     $ conda deactivate   

使用 activate 啟動 tfecon 虛擬環境 : 

(base) C:\Users\user>conda activate tfecon    
(tfecon) C:\Users\user>

可見已經切換到 tfecon 虛擬環境了, 接著在此環境下安裝 TensorFlow : 

(tfecon) C:\Users\user>pip install tensorflow   
Collecting tensorflow
  Downloading tensorflow-2.4.0-cp38-cp38-win_amd64.whl (370.7 MB)
Requirement already satisfied: wheel~=0.35 in c:\users\user\anaconda3\envs\tfecon\lib\site-packages (from tensorflow) (0.36.2)
Collecting gast==0.3.3
  Using cached gast-0.3.3-py2.py3-none-any.whl (9.7 kB)
Collecting absl-py~=0.10
  Using cached absl_py-0.11.0-py3-none-any.whl (127 kB)
Collecting astunparse~=1.6.3
  Using cached astunparse-1.6.3-py2.py3-none-any.whl (12 kB)
Collecting flatbuffers~=1.12.0
  Downloading flatbuffers-1.12-py2.py3-none-any.whl (15 kB)
Collecting google-pasta~=0.2
  Using cached google_pasta-0.2.0-py3-none-any.whl (57 kB)
Collecting grpcio~=1.32.0
  Downloading grpcio-1.32.0-cp38-cp38-win_amd64.whl (2.6 MB)
Collecting h5py~=2.10.0
  Using cached h5py-2.10.0-cp38-cp38-win_amd64.whl (2.5 MB)
Collecting keras-preprocessing~=1.1.2
  Using cached Keras_Preprocessing-1.1.2-py2.py3-none-any.whl (42 kB)
Collecting numpy~=1.19.2
  Using cached numpy-1.19.4-cp38-cp38-win_amd64.whl (13.0 MB)
Collecting opt-einsum~=3.3.0
  Using cached opt_einsum-3.3.0-py3-none-any.whl (65 kB)
Collecting protobuf>=3.9.2
  Using cached protobuf-3.14.0-py2.py3-none-any.whl (173 kB)
Collecting six~=1.15.0
  Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting tensorboard~=2.4
  Using cached tensorboard-2.4.0-py3-none-any.whl (10.6 MB)
Requirement already satisfied: setuptools>=41.0.0 in c:\users\user\anaconda3\envs\tfecon\lib\site-packages (from tensorboard~=2.4->tensorflow) (51.0.0.post20201207)
Collecting google-auth<2,>=1.6.3
  Downloading google_auth-1.24.0-py2.py3-none-any.whl (114 kB)
Collecting cachetools<5.0,>=2.0.0
  Downloading cachetools-4.2.0-py3-none-any.whl (12 kB)
Collecting google-auth-oauthlib<0.5,>=0.4.1
  Using cached google_auth_oauthlib-0.4.2-py2.py3-none-any.whl (18 kB)
Collecting markdown>=2.6.8
  Using cached Markdown-3.3.3-py3-none-any.whl (96 kB)
Collecting pyasn1-modules>=0.2.1
  Using cached pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
Collecting pyasn1<0.5.0,>=0.4.6
  Using cached pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
Collecting requests<3,>=2.21.0
  Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\user\anaconda3\envs\tfecon\lib\site-packages (from requests<3,>=2.21.0->tensorboard~=2.4->tensorflow) (2020.12.5)
Collecting chardet<5,>=3.0.2
  Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB)
Collecting idna<3,>=2.5
  Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting requests-oauthlib>=0.7.0
  Using cached requests_oauthlib-1.3.0-py2.py3-none-any.whl (23 kB)
Collecting oauthlib>=3.0.0
  Using cached oauthlib-3.1.0-py2.py3-none-any.whl (147 kB)
Collecting rsa<5,>=3.1.4
  Using cached rsa-4.6-py3-none-any.whl (47 kB)
Collecting tensorboard-plugin-wit>=1.6.0
  Using cached tensorboard_plugin_wit-1.7.0-py3-none-any.whl (779 kB)
Collecting tensorflow-estimator<2.5.0,>=2.4.0rc0
  Downloading tensorflow_estimator-2.4.0-py2.py3-none-any.whl (462 kB)
Collecting termcolor~=1.1.0
  Using cached termcolor-1.1.0.tar.gz (3.9 kB)
Collecting typing-extensions~=3.7.4
  Using cached typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Collecting urllib3<1.27,>=1.21.1
  Using cached urllib3-1.26.2-py2.py3-none-any.whl (136 kB)
Collecting werkzeug>=0.11.15
  Using cached Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
Collecting wrapt~=1.12.1
  Using cached wrapt-1.12.1.tar.gz (27 kB)
Building wheels for collected packages: termcolor, wrapt
  Building wheel for termcolor (setup.py) ... done
  Created wheel for termcolor: filename=termcolor-1.1.0-py3-none-any.whl size=4829 sha256=a709b93bf41ffb4e42e251b75c6696e3c9c5350e697a8b3637b86283b0fbfc92
  Stored in directory: c:\users\user\appdata\local\pip\cache\wheels\a0\16\9c\5473df82468f958445479c59e784896fa24f4a5fc024b0f501
  Building wheel for wrapt (setup.py) ... done
  Created wheel for wrapt: filename=wrapt-1.12.1-cp38-cp38-win_amd64.whl size=33631 sha256=3293b35c5a7a51e61cc79612bdfcb943d7f1d7237e1e3f39082c53c8b331ed22
  Stored in directory: c:\users\user\appdata\local\pip\cache\wheels\5f\fd\9e\b6cf5890494cb8ef0b5eaff72e5d55a70fb56316007d6dfe73
Successfully built termcolor wrapt
Installing collected packages: urllib3, pyasn1, idna, chardet, six, rsa, requests, pyasn1-modules, oauthlib, cachetools, requests-oauthlib, google-auth, werkzeug, tensorboard-plugin-wit, protobuf, numpy, markdown, grpcio, google-auth-oauthlib, absl-py, wrapt, typing-extensions, termcolor, tensorflow-estimator, tensorboard, opt-einsum, keras-preprocessing, h5py, google-pasta, gast, flatbuffers, astunparse, tensorflow
Successfully installed absl-py-0.11.0 astunparse-1.6.3 cachetools-4.2.0 chardet-4.0.0 flatbuffers-1.12 gast-0.3.3 google-auth-1.24.0 google-auth-oauthlib-0.4.2 google-pasta-0.2.0 grpcio-1.32.0 h5py-2.10.0 idna-2.10 keras-preprocessing-1.1.2 markdown-3.3.3 numpy-1.19.4 oauthlib-3.1.0 opt-einsum-3.3.0 protobuf-3.14.0 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.25.1 requests-oauthlib-1.3.0 rsa-4.6 six-1.15.0 tensorboard-2.4.0 tensorboard-plugin-wit-1.7.0 tensorflow-2.4.0 tensorflow-estimator-2.4.0 termcolor-1.1.0 typing-extensions-3.7.4.3 urllib3-1.26.2 werkzeug-1.0.1 wrapt-1.12.1

(tfecon) C:\Users\user>python 
Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information. 
>>> import tensorflow as tf   
>>> tf.__version__    
'2.4.0

這樣就在 tfecon 虛擬環境下安裝好 TensorFlow 2 了.

下面簡單測試 TensorFlow 張量運算 : 

定義隨機種子 :

>>> RANDOM_SEED = 42
>>> tf.random.set_seed(RANDOM_SEED)

定義常數 :

>>> x = tf.constant(1)
2021-01-06 11:10:53.373215: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-01-06 11:10:53.429170: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2021-01-06 11:10:53.449964: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
2021-01-06 11:10:53.709597: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: ASUS-2016-3
2021-01-06 11:10:53.716484: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: ASUS-2016-3
2021-01-06 11:10:53.811108: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-01-06 11:10:53.840105: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set

顯示 tensor 變數與其值 : 

>>> print(x)
tf.Tensor(1, shape=(), dtype=int32)
>>> x.numpy()
1

定義矩陣 : 

>>> m = tf.constant([[1, 2, 1], [3, 4, 2]])
>>> print(m)
tf.Tensor(
[[1 2 1]
 [3 4 2]], shape=(2, 3), dtype=int32)

定義單位矩陣 : 

>>> ones = tf.ones([3, 3])
>>> print(ones)
tf.Tensor(
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]], shape=(3, 3), dtype=float32)
>>> zeros = tf.zeros([2, 3])

定義零矩陣 : 

>>> print(zeros)
tf.Tensor(
[[0. 0. 0.]
 [0. 0. 0.]], shape=(2, 3), dtype=float32)

修改矩陣外形 : 

>>> tf.reshape(zeros, [3, 2])
<tf.Tensor: shape=(3, 2), dtype=float32, numpy=
array([[0., 0.],
       [0., 0.],
       [0., 0.]], dtype=float32)>

轉置矩陣 : 

>>> tf.transpose(zeros)
<tf.Tensor: shape=(3, 2), dtype=float32, numpy=
array([[0., 0.],
       [0., 0.],
       [0., 0.]], dtype=float32)>
>>> a = tf.constant(1)
>>> b = tf.constant(1)
>>> tf.add(a, b).numpy()
2
>>> (a + b).numpy()
2
>>> c = a + b
>>> print(c)
tf.Tensor(2, shape=(), dtype=int32)
>>> print(c.numpy())
2
>>> c * c
<tf.Tensor: shape=(), dtype=int32, numpy=4>
>>> print(c*c.numpy())
tf.Tensor(4, shape=(), dtype=int32)
>>> d1 = tf.constant([[1, 2], [1, 2]]);
>>> d2 = tf.constant([[3, 4], [3, 4]]);
>>> tf.tensordot(d1, d2, axes=1).numpy()
array([[ 9, 12],
       [ 9, 12]])
>>> norm = tf.random.normal(shape=(1000, 1), mean=0., stddev=1.)

要跳出虛擬環境先用 exit() 離開 Python, 再用 conda deactivate 離開 tfecon : 

>>> exit()

(tfecon) C:\Users\user>conda deactivate   

(base) C:\Users\user> 

沒有留言 :