吴恩达Deeplearning第二课代码bug修正大全

解决维度不匹配问题


第一周

Initialization

错误:

plt.title("Model with Zeros initialization")
axes = plt.gca()
axes.set_xlim([-1.5,1.5])
axes.set_ylim([-1.5,1.5])
plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

错误发生在这里,报错如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda2/envs/py3/lib/python3.6/site-packages/matplotlib/colors.py in to_rgba(c, alpha)
    131     try:
--> 132         rgba = _colors_full_map.cache[c, alpha]
    133     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)
~/anaconda2/envs/py3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   4049                 # must be acceptable as PathCollection facecolors
-> 4050                 colors = mcolors.to_rgba_array(c)
   4051             except ValueError:

~/anaconda2/envs/py3/lib/python3.6/site-packages/matplotlib/colors.py in to_rgba_array(c, alpha)
    232     for i, cc in enumerate(c):
--> 233         result[i] = to_rgba(cc, alpha)
    234     return result

~/anaconda2/envs/py3/lib/python3.6/site-packages/matplotlib/colors.py in to_rgba(c, alpha)
    133     except (KeyError, TypeError):  # Not in cache, or unhashable.
--> 134         rgba = _to_rgba_no_colorcycle(c, alpha)
    135         try:

~/anaconda2/envs/py3/lib/python3.6/site-packages/matplotlib/colors.py in _to_rgba_no_colorcycle(c, alpha)
    188     if len(c) not in [3, 4]:
--> 189         raise ValueError("RGBA sequence should have length 3 or 4")
    190     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-287696ebcb66> in <module>()
      3 axes.set_xlim([-1.5,1.5])
      4 axes.set_ylim([-1.5,1.5])
----> 5 plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

~/文档/DL课后作业/代码作业/第二课第一周编程作业/assignment1/init_utils.py in plot_decision_boundary(model, X, y)
    215     plt.ylabel('x2')
    216     plt.xlabel('x1')
--> 217     plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
    218     plt.show()
    219 

~/anaconda2/envs/py3/lib/python3.6/site-packages/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, hold, data, **kwargs)
   3355                          vmin=vmin, vmax=vmax, alpha=alpha,
   3356                          linewidths=linewidths, verts=verts,
-> 3357                          edgecolors=edgecolors, data=data, **kwargs)
   3358     finally:
   3359         ax._hold = washold

~/anaconda2/envs/py3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1708                     warnings.warn(msg % (label_namer, func.__name__),
   1709                                   RuntimeWarning, stacklevel=2)
-> 1710             return func(ax, *args, **kwargs)
   1711         pre_doc = inner.__doc__
   1712         if pre_doc is None:

~/anaconda2/envs/py3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   4053                 msg = ("c of shape {0} not acceptable as a color sequence "
   4054                        "for x with size {1}, y with size {2}")
-> 4055                 raise ValueError(msg.format(c.shape, x.size, y.size))
   4056         else:
   4057             colors = None  # use cmap, norm after collection is created

ValueError: c of shape (1, 300) not acceptable as a color sequence for x with size 300, y with size 300

解决方案:
1.找到下载的课程代码文件夹第二课第一周
2.找到init_utils.py 文件
3.找到plot_decision_boundary() 函数
4.修改plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
plt.scatter(X[0, :], X[1, :], c=y.reshape(X[0,:].shape), cmap=plt.cm.Spectral)
5.记得在Ipython notebook 中点击Kernel->Restart & Run All 重启内核,重新导包,否则无效


Regularization

错误:

train_X, train_Y, test_X, test_Y = load_2D_dataset()

问题发生在load_2D_dataset() 函数上,同样是矩阵维度不匹配
报错信息主要看这一条:

~/文档/DL课后作业/代码作业/第二课第一周编程作业/assignment1/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

解决方案 :
1.找到下载的课程代码文件夹第二课第一周
2.找到reg_utils.py 文件
3.找到load_2D_dataset() 函数
4.修改plt.scatter(train_X[0, :], train_X[1, :], c=train_Y, s=40, cmap=plt.cm.Spectral);
plt.scatter(train_X[0, :], train_X[1, :], c=np.squeeze(train_Y), s=40, cmap=plt.cm.Spectral);
5.记得在Ipython notebook 中点击Kernel->Restart & Run All 重启内核,重新导包,否则无效


错误:

plt.title("Model without regularization")
axes = plt.gca()
axes.set_xlim([-0.75,0.40])
axes.set_ylim([-0.75,0.65])
plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

报错重点在这:

~/文档/DL课后作业/代码作业/第二课第一周编程作业/assignment1/reg_utils.py in plot_decision_boundary(model, X, y)
    322     plt.ylabel('x2')
    323     plt.xlabel('x1')
--> 324     plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
    325     plt.show()
    326 

解决方案 :
1.找到下载的课程代码文件夹第二课第一周
2.找到reg_utils.py 文件
3.找到plot_decision_boundary(model, X, y) 函数
4.修改plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
plt.scatter(X[0, :], X[1, :], c=y.reshape(X[0,:].shape), cmap=plt.cm.Spectral)
5.记得在Ipython notebook 中点击Kernel->Restart & Run All 重启内核,重新导包,否则无效


第二周

错误:
出现了同样的问题

# train 3-layer model
layers_dims = [train_X.shape[0], 5, 2, 1]
parameters = model(train_X, train_Y, layers_dims, optimizer = "gd")

# Predict
predictions = predict(train_X, train_Y, parameters)

# Plot decision boundary
plt.title("Model with Gradient Descent optimization")
axes = plt.gca()
axes.set_xlim([-1.5,2.5])
axes.set_ylim([-1,1.5])
plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

这里报错重点是:

~/文档/DL课后作业/代码作业/第二课第二周编程作业/assignment2/opt_utils.py in plot_decision_boundary(model, X, y)
    230     plt.ylabel('x2')
    231     plt.xlabel('x1')
--> 232     plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
    233     plt.show()
    234 

解决方案:
1.找到下载的课程代码文件夹第二课第二周
2.找到opt_utils.py 文件
3.找到plot_decision_boundary(model, X, y) 函数
4.修改plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
plt.scatter(X[0, :], X[1, :], c=y.reshape(X[0,:].shape), cmap=plt.cm.Spectral)
5.记得在Ipython notebook 中点击Kernel->Restart & Run All 重启内核,重新导包,否则无效

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页