深度學習Course 2踩坑記

? Deep learning by Andrew NG在Course2的作業(yè)挺多坑的,可能是當時他布置作業(yè)時的環(huán)境比較舊,而現(xiàn)在搭建的Anaconda環(huán)境相對較新,有些方法已經不適用而造成的的。

先完成Regularizaion的作業(yè),運行

\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.


出現(xiàn)這種問題的原因是plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)這個函數(shù)太舊了,

解決方案如下即可

按提示找到reg_utils.py文件就在作業(yè)的同一個目錄。

在頂部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了

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容