Deep learning by Andrew NG在Course2的作业挺多坑的,可能是当时他布置作业时的环境比较旧,而现在搭建的Anaconda环境相对较新,有些方法已经不适用而造成的的。
先完成Regularizaion的作业,运行
\COURSE 2 Improving Deep Neural Networks Hyperparameter tuning, Regularization and Optimization\week5\Regularization\Regularization.ipynb
第一坑:
首先踩到的第一个坑是如下提示:
train_X, train_Y, test_X, test_Y = load_2D_dataset()
TypeError Traceback (most recent call last)
d:\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba(c, alpha)
173 try:
--> 174 rgba = _colors_full_map.cache[c, alpha]
175 except (KeyError, TypeError): # Not in cache, or unhashable.
TypeError: unhashable type: 'numpy.ndarray'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
d:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
4231 try: # Then is 'c' acceptable as PathCollection facecolors?
-> 4232 colors = mcolors.to_rgba_array(c)
4233 n_elem = colors.shape[0]
d:\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba_array(c, alpha)
274 for i, cc in enumerate(c):
--> 275 result[i] = to_rgba(cc, alpha)
276 return result
d:\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba(c, alpha)
175 except (KeyError, TypeError): # Not in cache, or unhashable.
--> 176 rgba = _to_rgba_no_colorcycle(c, alpha)
177 try:
d:\Anaconda3\lib\site-packages\matplotlib\colors.py in _to_rgba_no_colorcycle(c, alpha)
230 if len(c) not in [3, 4]:
--> 231 raise ValueError("RGBA sequence should have length 3 or 4")
232 if len(c) == 3 and alpha is None:
ValueError: RGBA sequence should have length 3 or 4
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-5-a9a84d38b990> in <module>
----> 1 train_X, train_Y, test_X, test_Y = load_2D_dataset()
~\Desktop\COURSE 2 Improving Deep Neural Networks Hyperparameter tuning, Regularization and Optimization\week5\Regularization\reg_utils.py in load_2D_dataset()
332 test_Y = data['yval'].T
333
--> 334 plt.scatter(train_X[0, :], train_X[1, :], c=train_Y, s=40, cmap=plt.cm.Spectral);
335
336 return train_X, train_Y, test_X, test_Y
d:\Anaconda3\lib\site-packages\matplotlib\pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, data, **kwargs)
2860 vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,
2861 verts=verts, edgecolors=edgecolors, **({"data": data} if data
-> 2862 is not None else {}), **kwargs)
2863 sci(__ret)
2864 return __ret
d:\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1808 "the Matplotlib list!)" % (label_namer, func.__name__),
1809 RuntimeWarning, stacklevel=2)
-> 1810 return func(ax, *args, **kwargs)
1811
1812 inner.__doc__ = _add_data_doc(inner.__doc__,
d:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
4243 "acceptable for use with 'x' with size {xs}, "
4244 "'y' with size {ys}."
-> 4245 .format(nc=n_elem, xs=x.size, ys=y.size)
4246 )
4247 # Both the mapping *and* the RGBA conversion failed: pretty
ValueError: 'c' argument has 1 elements, which is not acceptable for use with 'x' with size 211, 'y' with size 211.
出现这种问题的原因是plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)这个函数太旧了,
解决方案如下即可
按提示找到reg_utils.py文件就在作业的同一个目录。
在顶部import如下
import operator
from functools import reduce
然后分别在
plot_decision_boundary()和load_2D_dataset()找到plt.scatter方法,替换成
plt.scatter(X[0, :], X[1, :], c=reduce(operator.add, y), s=40, cmap=plt.cm.Spectral)
第二坑:
TypeError Traceback (most recent call last)
d:\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba(c, alpha)
173 try:
--> 174 rgba = _colors_full_map.cache[c, alpha]
175 except (KeyError, TypeError): # Not in cache, or unhashable.
TypeError: unhashable type: 'numpy.ndarray'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
d:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
4231 try: # Then is 'c' acceptable as PathCollection facecolors?
-> 4232 colors = mcolors.to_rgba_array(c)
4233 n_elem = colors.shape[0]
d:\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba_array(c, alpha)
274 for i, cc in enumerate(c):
--> 275 result[i] = to_rgba(cc, alpha)
276 return result
d:\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba(c, alpha)
175 except (KeyError, TypeError): # Not in cache, or unhashable.
--> 176 rgba = _to_rgba_no_colorcycle(c, alpha)
177 try:
d:\Anaconda3\lib\site-packages\matplotlib\colors.py in _to_rgba_no_colorcycle(c, alpha)
230 if len(c) not in [3, 4]:
--> 231 raise ValueError("RGBA sequence should have length 3 or 4")
232 if len(c) == 3 and alpha is None:
ValueError: RGBA sequence should have length 3 or 4
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-7-a9a84d38b990> in <module>
----> 1 train_X, train_Y, test_X, test_Y = load_2D_dataset()
~\Desktop\COURSE 2 Improving Deep Neural Networks Hyperparameter tuning, Regularization and Optimization\week5\Regularization\reg_utils.py in load_2D_dataset()
332 train_X = data['X'].T
333 train_Y = data['y'].T
--> 334 test_X = data['Xval'].T
335 test_Y = data['yval'].T
336
d:\Anaconda3\lib\site-packages\matplotlib\pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, data, **kwargs)
2860 vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,
2861 verts=verts, edgecolors=edgecolors, **({"data": data} if data
-> 2862 is not None else {}), **kwargs)
2863 sci(__ret)
2864 return __ret
d:\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1808 "the Matplotlib list!)" % (label_namer, func.__name__),
1809 RuntimeWarning, stacklevel=2)
-> 1810 return func(ax, *args, **kwargs)
1811
1812 inner.__doc__ = _add_data_doc(inner.__doc__,
d:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
4243 "acceptable for use with 'x' with size {xs}, "
4244 "'y' with size {ys}."
-> 4245 .format(nc=n_elem, xs=x.size, ys=y.size)
4246 )
4247 # Both the mapping *and* the RGBA conversion failed: pretty
ValueError: 'c' argument has 1 elements, which is not acceptable for use with 'x' with size 211, 'y' with size 211.
==========================================
Traceback (most recent call last):
File "d:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-1-0b76c4151612>", line 4, in <module>
from reg_utils import sigmoid, relu, plot_decision_boundary, initialize_parameters, load_2D_dataset, predict_dec
File "C:\Users\34657\Desktop\COURSE 2 Improving Deep Neural Networks Hyperparameter tuning, Regularization and Optimization\week5\Regularization\reg_utils.py", line 328
plt.scatter(X[0, :], X[1, :], c=reduce(operator.add, Y), s=40, cmap=plt.cm.Spectral)
^
TabError: inconsistent use of tabs and spaces in indentation
分析
提示空格问题??然而所有打开来看又很正常啊!
其实很多表面的正常,背后都隐藏着不正常,你看到的是隔开了,但实际上它可能是/t而不是我们普遍认为的/space
参考这位网友的方案https://blog.csdn.net/qq_41096996/article/details/85947560
将上面的真空格替换错误行的假空格就OK了