TensorFlow成功安装
首先声明本次安装的是基于Python 3.7 tensorflow-cpu的安装,其他版本安装参考网上其他资料。
以前使用的python3.6的,安装过程没遇到什么错误。这次是在python3.7上安装的,遇到了不同的问题,故记录下安装过程中的问题及解决办法,希望对大家有所帮助。我是在windows上安装的,个人觉得用anaconda进行安装非常方便。anaconda安装后有一个类似windows命令窗口的Anaconda Prompt,如图:
打开上述窗口后执行
1.pip install tensorflow
出现了这个问题 wisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.,
解决方法 :pip install PyHamcrest
同时还会出现 : ERROR:Cannot uninstall 'wrap’问题
解决方案: pip install -U --ignore-installed wrapt enum34 simplejson netaddr
参考:ERROR:Cannot uninstall ‘wrap’
同时还会出现 : tensorboard 1.14.0 has requirement setuptools>=41.0.0, but you’ll have setuptools 40.2.0 which is incompatible.问题
解决方案: pip install setuptools==41.0.0
2.这个时候看起来已经成功安装,但是还会出现问题。
当我们导入包时: import tensorflow as tf
又出现问题: ModuleNotFoundError: No module named ‘numpy.core._multiarray_umath’ 错误
解决方案:升级numpy pip install --upgrade numpy
参考:更新numpy版本
这个时候基本就安装成功了。
补充 :
后面会遇到在引入skimage 包时报错,错误提示from numpy.lib.arraypad import _validate_lengths,找不到_validate_lengths函数。
找到:Anaconda3/envs/your dirb/python3.7/site-packages/numpyb/arraypad.py的954行,添加下面两个函数保存,重新加载即可消除错误。参考:解决方法来源
def _normalize_shape(ndarray, shape, cast_to_int=True):
"""
Private function which does some checks and normalizes the possibly
much simpler representations of 'pad_width', 'stat_length',
'constant_values', 'end_values'.
Parameters
----------
narray : ndarray
Input ndarray
shape : {sequence, array_like, float, int}, optional
The width of padding (pad_width), the number of elements on the
edge of the narray used for statistics (stat_length), the constant
value(s) to use when filling padded regions (constant_values), or the
endpoint target(s) for linear ramps (end_values).
((before_1, after_1), ... (before_N, after_N)) unique number of
elements for each axis where `N` is rank of `narray`.
((before, after),) yields same before and after constants for each
axis.
(constant,) or val is a shortcut for before = after = constant for
all axes.
cast_to_int : bool, optional
Controls if values in ``shape`` will be rounded and cast to int
before being returned.
Returns
-------
normalized_shape : tuple of tuples
val => ((val, val), (val, val), ...)
[[val1, val2], [val3, val4], ...] => ((val1, val2), (val3, val4), ...)
((val1, val2), (val3, val4), ...) => no change
[[val1, val2], ] => ((val1, val2), (val1, val2), ...)
((val1, val2), ) => ((val1, val2), (val1, val2), ...)
[[val , ], ] => ((val, val), (val, val), ...)
((val , ), ) => ((val, val), (val, val), ...)
"""
ndims = ndarray.ndim
# Shortcut shape=None
if shape is None:
return ((None, None), ) * ndims
# Convert any input `info` to a NumPy array
shape_arr = np.asarray(shape)
try:
shape_arr = np.broadcast_to(shape_arr, (ndims, 2))
except ValueError:
fmt = "Unable to create correctly shaped tuple from %s"
raise ValueError(fmt % (shape,))
# Cast if necessary
if cast_to_int is True:
shape_arr = np.round(shape_arr).astype(int)
# Convert list of lists to tuple of tuples
return tuple(tuple(axis) for axis in shape_arr.tolist())
def _validate_lengths(narray, number_elements):
"""
Private function which does some checks and reformats pad_width and
stat_length using _normalize_shape.
Parameters
----------
narray : ndarray
Input ndarray
number_elements : {sequence, int}, optional
The width of padding (pad_width) or the number of elements on the edge
of the narray used for statistics (stat_length).
((before_1, after_1), ... (before_N, after_N)) unique number of
elements for each axis.
((before, after),) yields same before and after constants for each
axis.
(constant,) or int is a shortcut for before = after = constant for all
axes.
Returns
-------
_validate_lengths : tuple of tuples
int => ((int, int), (int, int), ...)
[[int1, int2], [int3, int4], ...] => ((int1, int2), (int3, int4), ...)
((int1, int2), (int3, int4), ...) => no change
[[int1, int2], ] => ((int1, int2), (int1, int2), ...)
((int1, int2), ) => ((int1, int2), (int1, int2), ...)
[[int , ], ] => ((int, int), (int, int), ...)
((int , ), ) => ((int, int), (int, int), ...)
"""
normshp = _normalize_shape(narray, number_elements)
for i in normshp:
chk = [1 if x is None else x for x in i]
chk = [1 if x >= 0 else -1 for x in chk]
if (chk[0] < 0) or (chk[1] < 0):
fmt = "%s cannot contain negative values."
raise ValueError(fmt % (number_elements,))
return normshp