多层感知器,可视化Python中的决策边界(2D)

时间:2015-10-03 10:03:41

标签: python numpy neural-network visualization perceptron

我已经为二进制分类编写了多层感知。据我所知,一个隐藏层可以仅用线作为决策边界(每个隐藏神经元一行)来表示。这很有效,并且可以在训练后使用得到的权重轻松绘制。

然而,随着更多图层的添加,我不确定使用什么方法,可视化部分很少在教科书中处理。我想知道,有没有一种直接的方法将权重矩阵从不同的层转换为这个非线性决策边界(假设是2D输入)?

非常感谢,

1 个答案:

答案 0 :(得分:3)

绘制决策边界(对于线性或非线性分类器)的一种方法是对均匀网格中的点进行采样并将它们提供给分类器。 Asumming val some = Option(List(3,4,5)) val none = Option(List()) // seeking None, rather than Some(List()) here 是您的数据,您可以按如下方式创建统一的点网格:

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  if CurPageID = wpSelectProgramGroup then
  begin
    if DirExists(ExpandConstant('{group}')) then { if the directory is not empty }
    begin   
      { I have no idea how to get the info if the user has selected }
      { the create start menu folder option or not }
      if { USER DID NOT SELECT THE 'Don't create a Start menu folder' OPTION } then
      begin
        MsgBox('Directory already exists. Please choose a different one.',
               mbConfirmation, MB_OK);
        Result := False;
      end
        else
      begin
        Result := True;
      end; 
    end
      else
    begin
      Result := True;
    end; 
  end { wpSelectProgramGroup }
    else
  begin
    Result := True;
  end; 
end;

然后,您将这些坐标提供给感知器以捕获他们的预测:

X

假设h = .02 # step size in the mesh x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) 是您的感知器,Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) 会从统一采样点创建要素,将它们提供给分类器并在clf预测中捕获。

最后,将决策边界绘制为等高线图(使用matplotlib):

np.c_

并且可选地,还绘制您的数据点:

Z

Fully working example,示例的信用转到scikit-learn(顺便说一下,这是一个很好的机器学习库,实现了完全正常工作的Perceptron)。