|
|
@@ -70,7 +70,7 @@ |
|
|
|
"\n", |
|
|
|
"这样,假设超平面S的总的误分类点集合为M,那么所有误分类点到S的距离之和为\n", |
|
|
|
"$$\n", |
|
|
|
"-\\frac{1}{||w||} \\sum_{x_i \\in M} y_i (w \\cdot x_i + b)\n", |
|
|
|
"-\\frac{1}{||w||} \\sum_{x_i \\in M} |w \\cdot x_i + b|\n", |
|
|
|
"$$\n", |
|
|
|
"不考虑1/||w||,就得到了感知机学习的损失函数。\n", |
|
|
|
"\n", |
|
|
@@ -156,10 +156,11 @@ |
|
|
|
"name": "stdout", |
|
|
|
"output_type": "stream", |
|
|
|
"text": [ |
|
|
|
"update weight and bias: 1.0 3.0 0.5\n", |
|
|
|
"update weight and bias: -2.5 1.5 0.0\n", |
|
|
|
"w = [-2.5, 1.5]\n", |
|
|
|
"b = 0.0\n", |
|
|
|
"update weight and bias: 0.5 1.5 0.5\n", |
|
|
|
"update weight and bias: -3.0 0.0 0.0\n", |
|
|
|
"update weight and bias: -2.0 3.0 0.5\n", |
|
|
|
"w = [-2.0, 3.0]\n", |
|
|
|
"b = 0.5\n", |
|
|
|
"ground_truth: [1, 1, 1, 1, -1, -1, -1, -1]\n", |
|
|
|
"predicted: [1, 1, 1, 1, -1, -1, -1, -1]\n" |
|
|
|
] |
|
|
@@ -175,16 +176,17 @@ |
|
|
|
" else: return -1\n", |
|
|
|
" \n", |
|
|
|
"def perceptron_train(train_data, eta=0.5, n_iter=100):\n", |
|
|
|
" weight = [0, 0] # 权重\n", |
|
|
|
" bias = 0 # 偏置量\n", |
|
|
|
" weight = [0, 0] # 权重\n", |
|
|
|
" bias = 0 # 偏置量\n", |
|
|
|
" learning_rate = eta # 学习速率\n", |
|
|
|
"\n", |
|
|
|
" train_num = n_iter # 迭代次数\n", |
|
|
|
"\n", |
|
|
|
" for i in range(train_num):\n", |
|
|
|
" #FIXME: the random chose sample is to slow\n", |
|
|
|
" # need improve to cover all samples\n", |
|
|
|
" train = random.choice(train_data)\n", |
|
|
|
" x1, x2, y = train\n", |
|
|
|
" \n", |
|
|
|
" predict = sign(weight[0] * x1 + weight[1] * x2 + bias) # 输出\n", |
|
|
|
" #print(\"train data: x: (%2d, %2d) y: %2d ==> predict: %2d\" % (x1, x2, y, predict))\n", |
|
|
|
" \n", |
|
|
|