安装tensorflow-gpu记录

前言

Mac用户不要直接本机装!!!ubuntu装比较省事。

安装conda

有图形安装和命令行安装,我这里选择命令行安装

下载地址

官网(国外网站,下载较慢)

https://www.anaconda.com/products/individual

国内镜像

https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/

安装命令

1
bash ~/Downloads/Anaconda3-2019.07-MacOSX-x86_64.sh

安装tensorflow-gpu

下面记录一下安装tensorflow-gpu过程中遇到的问题

1
conda install tensorflow-gpu

报错

1
2
3
4
5
6
7
8
9
10
11
12
13
Solving environment: failed with current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): failed

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/main/osx-64/repodata.json>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

If your current network has https://www.anaconda.com blocked, please file
a support request with your network engineering team.

ConnectionError(ReadTimeoutError("HTTPSConnectionPool(host='repo.anaconda.com', port=443): Read timed out."))

解决办法:换源

这里选择清华的,执行如下命令

1
2
3
4
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

找到.condarc文件,如果不知道在哪可以通过如下命令查看

1
conda --help

查看.condarc文件,注意channels这里除了我们添加的清华源还有个-defaults

1
conda config --show-sources

执行

1
conda search tensorflow-gpu

依然报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Loading channels: failed

# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

Traceback (most recent call last):
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 234, in _load
repodata_fn=self.repodata_fn)
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 584, in fetch_repodata_remote_request
raise Response304ContentUnchanged()
conda.core.subdir_data.Response304ContentUnchanged

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/exceptions.py", line 1062, in __call__
return func(*args, **kwargs)
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/cli/main.py", line 84, in _main
exit_code = do_call(args, p)
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/cli/conda_argparse.py", line 82, in do_call
exit_code = getattr(module, func_name)(args, parser)
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/cli/main_search.py", line 73, in execute
matches = sorted(SubdirData.query_all(spec, channel_urls, subdirs),
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 91, in query_all
result = tuple(concat(executor.map(subdir_query, channel_urls)))
File "/Users/glarcy/anaconda3/lib/python3.7/concurrent/futures/_base.py", line 586, in result_iterator
yield fs.pop().result()
File "/Users/glarcy/anaconda3/lib/python3.7/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/Users/glarcy/anaconda3/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/Users/glarcy/anaconda3/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 87, in <lambda>
package_ref_or_match_spec))
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 96, in query
self.load()
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 160, in load
_internal_state = self._load()
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 249, in _load
mod_etag_headers.get('_mod'))
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 297, in _read_local_repdata
_internal_state = self._process_raw_repodata_str(raw_repodata_str)
File "/Users/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 335, in _process_raw_repodata_str
json_obj = json.loads(raw_repodata_str or '{}')
File "/Users/glarcy/anaconda3/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/Users/glarcy/anaconda3/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/glarcy/anaconda3/lib/python3.7/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 142249 column 16 (char 3604299)

解决办法:删除.condarc文件的-defaults

删除以后需执行以下命令,清除cache

1
conda clean -i

不报错了,但是发现我的源下面并没有tensorflow-gpu的相关版本包

后来,又发现mac目前仅支持python2.7、3.3-3.6的tensorflow-gpu版本

https://tensorflow.google.cn/install/source

而我的版本为3.7,于是

1
2
3
4
5
6
7
创建tensorflow-gpu运行环境
conda create -n tensorflow python=3.6
激活
conda activate tensorflow
# conda deactivate
安装tensorflow-gpu
pip install tensorflow-gpu

安装完之后发现导入tensorflow报错,查了一下好像是cuda,cudnn版本问题,但是在mac要安装这两个太麻烦了,我弃坑了转ubuntu了,ubuntu就不用这么多步骤

Ubuntu 安装

安装

1
bash Anaconda3-2019.07-Linux-x86_64.sh

安装tensorflow-gpu

1
conda install tensorflow-gpu

查看安装列表

1
conda list

出现错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(tensorflow) glarcy@glarcy-virtual-machine:~$ conda list

# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

Traceback (most recent call last):
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/exceptions.py", line 1062, in __call__
return func(*args, **kwargs)
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/cli/main.py", line 84, in _main
exit_code = do_call(args, p)
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/cli/conda_argparse.py", line 82, in do_call
exit_code = getattr(module, func_name)(args, parser)
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/cli/main_list.py", line 142, in execute
show_channel_urls=context.show_channel_urls)
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/cli/main_list.py", line 80, in print_packages
show_channel_urls=show_channel_urls)
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/cli/main_list.py", line 45, in list_packages
installed = sorted(PrefixData(prefix, pip_interop_enabled=True).iter_records(),
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/prefix_data.py", line 130, in iter_records
return itervalues(self._prefix_records)
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/prefix_data.py", line 159, in _prefix_records
return self.__prefix_records or self.load() or self.__prefix_records
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/common/io.py", line 88, in decorated
return f(*args, **kwds)
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/prefix_data.py", line 72, in load
self._load_site_packages()
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/core/prefix_data.py", line 274, in _load_site_packages
python_record = read_python_record(self.prefix_path, af, python_pkg_record.version)
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/gateways/disk/read.py", line 257, in read_python_record
paths_tups = pydist.get_paths()
File "/home/glarcy/anaconda3/lib/python3.7/site-packages/conda/common/pkg_formats/python.py", line 268, in get_paths
records = sorted(concatv(records, ((pf, None, None) for pf in missing_pyc_files)))
TypeError: '<' not supported between instances of 'str' and 'NoneType'

解决办法:更新conda

1
conda update  --all

查看conda 环境信息

1
conda info --envs

If you’d prefer that conda’s base environment not be activated on startup, set the auto_activate_base parameter to false:

1
conda config --set auto_activate_base false

import tensorflow错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/glarcy/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])

原因numpy版本太高

解决办法:将numpy换成1.16.0版本

1
pip install numpy==1.16.0