kuroの覚え書き

96の個人的覚え書き

Anacondaの再インストールで躓く

pyenvで仮想化

$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
$ source ~/.bashrc

anacondaをインストール

$ pyenv install anaconda3-5.3.1
$ pyenv rehash

$ pyenv global anaconda3-5.3.1
$ echo 'export PATH="$PYENV_ROOT/versions/anaconda3-5.3.1/bin/:$PATH"' >> ~/.bashrc
$ source ~/.bashrc
$ conda update conda

深層学習の環境を構築

$ conda install cudatoolkit
$ conda install cudnn

$ conda install python=3.6
$ conda install tensorflow-gpu

という段取りなんだが、condaをアップデートするとconda4.7.10にあげようとする。
ところがこのバージョンはcudatoolkitのインストールが失敗するので

$ conda install conda4.6.14
Solving environment: done

## Package Plan ##

  environment location: /home/kkuro2/.pyenv/versions/anaconda3-5.3.1

  added / updated specs: 
    - conda=4.6.14


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    pyopenssl-19.0.0           |           py37_0          82 KB
    _libgcc_mutex-0.1          |             main           3 KB
    ncurses-6.1                |       he6710b0_1         958 KB
    idna-2.8                   |           py37_0         101 KB
    chardet-3.0.4              |        py37_1003         173 KB
    cffi-1.12.3                |   py37h2e261b9_0         222 KB
    conda-4.6.14               |           py37_0         2.1 MB
    pysocks-1.7.0              |           py37_0          29 KB
    urllib3-1.24.2             |           py37_0         153 KB
    libgcc-ng-9.1.0            |       hdf63c60_0         8.1 MB
    pycparser-2.19             |           py37_0         172 KB
    openssl-1.1.1c             |       h7b6447c_1         3.8 MB
    cryptography-2.7           |   py37h1ba5d50_0         608 KB
    ca-certificates-2019.5.15  |                0         133 KB
    six-1.12.0                 |           py37_0          22 KB
    setuptools-41.0.1          |           py37_0         648 KB
    pip-19.1.1                 |           py37_0         1.8 MB
    zlib-1.2.11                |       h7b6447c_3         120 KB
    requests-2.22.0            |           py37_0          89 KB
    certifi-2019.6.16          |           py37_1         156 KB
    libedit-3.1.20181209       |       hc058e9b_0         188 KB
    wheel-0.33.4               |           py37_0          39 KB
    python-3.7.3               |       h0371630_0        36.7 MB
    sqlite-3.29.0              |       h7b6447c_0         1.9 MB
    ------------------------------------------------------------
                                           Total:        58.2 MB

The following NEW packages will be INSTALLED:

    _libgcc_mutex:   0.1-main               

The following packages will be UPDATED:

    ca-certificates: 2018.03.07-0            --> 2019.5.15-0            
    certifi:         2018.8.24-py37_1        --> 2019.6.16-py37_1       
    cffi:            1.11.5-py37he75722e_1   --> 1.12.3-py37h2e261b9_0  
    chardet:         3.0.4-py37_1            --> 3.0.4-py37_1003        
    conda:           4.5.11-py37_0           --> 4.6.14-py37_0          
    cryptography:    2.3.1-py37hc365091_0    --> 2.7-py37h1ba5d50_0     
    idna:            2.7-py37_0              --> 2.8-py37_0             
    libedit:         3.1.20170329-h6b74fdf_2 --> 3.1.20181209-hc058e9b_0
    libgcc-ng:       8.2.0-hdf63c60_1        --> 9.1.0-hdf63c60_0       
    ncurses:         6.1-hf484d3e_0          --> 6.1-he6710b0_1         
    openssl:         1.0.2p-h14c3975_0       --> 1.1.1c-h7b6447c_1      
    pip:             10.0.1-py37_0           --> 19.1.1-py37_0          
    pycparser:       2.18-py37_1             --> 2.19-py37_0            
    pyopenssl:       18.0.0-py37_0           --> 19.0.0-py37_0          
    pysocks:         1.6.8-py37_0            --> 1.7.0-py37_0           
    python:          3.7.0-hc3d631a_0        --> 3.7.3-h0371630_0       
    requests:        2.19.1-py37_0           --> 2.22.0-py37_0          
    setuptools:      40.2.0-py37_0           --> 41.0.1-py37_0          
    six:             1.11.0-py37_1           --> 1.12.0-py37_0          
    sqlite:          3.24.0-h84994c4_0       --> 3.29.0-h7b6447c_0      
    urllib3:         1.23-py37_0             --> 1.24.2-py37_0          
    wheel:           0.31.1-py37_0           --> 0.33.4-py37_0          
    zlib:            1.2.11-ha838bed_2       --> 1.2.11-h7b6447c_3      

Proceed ([y]/n)? y


Downloading and Extracting Packages
pyopenssl-19.0.0     | 82 KB     | ###################################### | 100% 
_libgcc_mutex-0.1    | 3 KB      | ###################################### | 100% 
ncurses-6.1          | 958 KB    | ###################################### | 100% 
idna-2.8             | 101 KB    | ###################################### | 100% 
chardet-3.0.4        | 173 KB    | ###################################### | 100% 
cffi-1.12.3          | 222 KB    | ###################################### | 100% 
conda-4.6.14         | 2.1 MB    | ###################################### | 100% 
pysocks-1.7.0        | 29 KB     | ###################################### | 100% 
urllib3-1.24.2       | 153 KB    | ###################################### | 100% 
libgcc-ng-9.1.0      | 8.1 MB    | ###################################### | 100% 
pycparser-2.19       | 172 KB    | ###################################### | 100% 
openssl-1.1.1c       | 3.8 MB    | ###################################### | 100% 
cryptography-2.7     | 608 KB    | ###################################### | 100% 
ca-certificates-2019 | 133 KB    | ###################################### | 100% 
six-1.12.0           | 22 KB     | ###################################### | 100% 
setuptools-41.0.1    | 648 KB    | ###################################### | 100% 
pip-19.1.1           | 1.8 MB    | ###################################### | 100% 
zlib-1.2.11          | 120 KB    | ###################################### | 100% 
requests-2.22.0      | 89 KB     | ###################################### | 100% 
certifi-2019.6.16    | 156 KB    | ###################################### | 100% 
libedit-3.1.20181209 | 188 KB    | ###################################### | 100% 
wheel-0.33.4         | 39 KB     | ###################################### | 100% 
python-3.7.3         | 36.7 MB   | ###################################### | 100% 
sqlite-3.29.0        | 1.9 MB    | ###################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

とうまく動いていた頃のバージョンにアップしてやる。
で、cudatoolkitをインストールするとcondaの最新版が4.7.10だからアップデートを自動でしやがって結果インストールに失敗する。

どうせいっちゅうの。

anacondaおよびcondaのバージョンアップで改善するのを待つか・・・


今できる別の方法を考えてみよう。
まず

$ pyenv install anaconda3-4.3.1
$ pyenv global anaconda3-4.3.1 

python 3.6のanacondaを入れる。
次にcudnnをNVIDIAのサイトからダウンロードしてきて

$ tar zxf cudnn-10.1-linux-x64-v7.6.2.24.tgz
$ sudo cp -a cuda/include/* /usr/local/cuda/include/
$ sudo cp -a cuda/lib64/* /usr/local/cuda/lib64/
$ sudo ldconfig

とcudnnの必要なファイルを手動でインストール
必要かはちょっとわからないが

$ sudo ln -s /usr/local/cuda/include/crt/math_functions.hpp /usr/local/cuda/include/math_functions.hpp
$ sudo ln -s /usr/lib64/libcublas.so.10.1.0.105 /usr/local/cuda-10.1/lib64/libcublas.so.10.1.0.105
$ sudo ln -s /usr/local/cuda-10.1/lib64/libcublas.so.10.1.0.105 /usr/local/cuda-10.1/lib64/libcublas.so.10.1
$ sudo ln -s /usr/local/cuda-10.1/lib64/libcublas.so.10.1 /usr/local/cuda-10.1/lib64/libcublas.so
$ sudo ln -s /usr/local/cuda-10.1/targets/x86_64-linux/lib/libcusolver.so.10.1.105 /usr/local/cuda-10.1/lib64/libcusolver.so.10.1
$ sudo ln -s /usr/local/cuda-10.1/targets/x86_64-linux/lib/libcurand.so.10.1.105 /usr/local/cuda-10.1/lib64/libcurand.so.10.1
$ sudo ln -s /usr/local/cuda-10.1/targets/x86_64-linux/lib/libcufft.so.10.1.105 /usr/local/cuda-10.1/lib64/libcufft.so.10.1

とリンクを張っておいて

$ conda install tensorflow-gpu

とtensorflowをインストールする。
この状態で

$ python
Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 12:22:00) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from tensorflow.python.client import device_lib
>>> device_lib.list_local_devices()
2019-08-04 15:00:29.442682: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2019-08-04 15:00:29.442754: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2019-08-04 15:00:29.612838: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:893] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-04 15:00:29.613333: I tensorflow/core/common_runtime/gpu/gpu_device.cc:955] Found device 0 with properties: 
name: GeForce GT 710
major: 3 minor: 5 memoryClockRate (GHz) 0.954
pciBusID 0000:04:00.0
Total memory: 980.94MiB
Free memory: 958.69MiB
2019-08-04 15:00:29.613394: I tensorflow/core/common_runtime/gpu/gpu_device.cc:976] DMA: 0 
2019-08-04 15:00:29.613416: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 0:   Y 
2019-08-04 15:00:29.613452: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GT 710, pci bus id: 0000:04:00.0)
[name: "/cpu:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 10112316233564255216
, name: "/gpu:0"
device_type: "GPU"
memory_limit: 769327104
locality {
  bus_id: 1
}
incarnation: 15150163897772535855
physical_device_desc: "device: 0, name: GeForce GT 710, pci bus id: 0000:04:00.0"
]
>>> 

とテストすると、ここまでは問題なくインストールできたことがわかる。
問題はこのあとのkerasでcondaでインストールするとまたcondaのバージョンアップで固まり、サンプルプログラムでは

$ python mnist_cnn.py 
Using TensorFlow backend.
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
11493376/11490434 [==============================] - 4s 0us/step
x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
Traceback (most recent call last):
  File "mnist_cnn.py", line 57, in <module>
    model.add(Dense(num_classes, activation='softmax'))
  File "/home/kuro/.pyenv/versions/anaconda3-4.3.1/lib/python3.6/site-packages/keras/engine/sequential.py", line 181, in add
    output_tensor = layer(self.outputs[0])
  File "/home/kuro/.pyenv/versions/anaconda3-4.3.1/lib/python3.6/site-packages/keras/engine/base_layer.py", line 457, in __call__
    output = self.call(inputs, **kwargs)
  File "/home/kuro/.pyenv/versions/anaconda3-4.3.1/lib/python3.6/site-packages/keras/layers/core.py", line 883, in call
    output = self.activation(output)
  File "/home/kuro/.pyenv/versions/anaconda3-4.3.1/lib/python3.6/site-packages/keras/activations.py", line 31, in softmax
    return K.softmax(x)
  File "/home/kuro/.pyenv/versions/anaconda3-4.3.1/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 3231, in softmax
    return tf.nn.softmax(x, axis=axis)
TypeError: softmax() got an unexpected keyword argument 'axis'

こんなエラーが出て止まってしまう。
これはなんだ?
なおpipでインストールしても同じエラーが出るので、pythonモジュールに足りないものがあるのかもしれない。

結局

$ conda install keras=2.0.8

と最新版の2.2.4よりちょっと古いバージョンをインストールしてみたところ、とりあえずエラー出さずに完走できることがわかった。
しばらくはこれで様子を見るか。

なお、anaconda環境でpipを使うと最悪環境が壊れてしまうこともあるらしく、あまりおすすめできないとのこと。
なのでcondaでどうにかする方策を基本としたが、condaがバージョンアップしておかしくなっている間は困るのね。