## Cynhard85 / MachineLearningTutorial

liuxinyang 提交于 2018-07-06 16:43 . modify names
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# 准备数据"
]
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"import numpy as np\n",
"import pandas as pd\n",
"import datetime"
]
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.figure.Figure at 0x22a54e79f28>"
]
},
"output_type": "display_data"
}
],
"source": [
"np.random.seed(42)\n",
"\n",
"m = 100\n",
"w = np.array([3, 4]).reshape([-1, 1])\n",
"x = np.linspace(-10, 10, m, [-1, 1])\n",
"X = np.c_[np.ones([m, 1]), x]\n",
"y = X.dot(w) + np.random.normal(0, 5, m).reshape([-1, 1])\n",
"\n",
"plt.scatter(x, y, s=10)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"outputs": [],
"source": [
"df = pd.DataFrame(data=list(zip(x, y.flatten())), columns=['x', 'y'])\n",
"df.to_csv(\"{}.csv\".format(datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')), index=False)"
]
},
{
"cell_type": "markdown",
"source": [
"# 批量梯度下降"
]
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2.56385135]\n",
" [4.03448317]]\n"
]
}
],
"source": [
"    np.random.seed(42)\n",
"    w = 10 * np.random.randn(2, 1) + 10\n",
"    ws = []\n",
"    mses = []\n",
"\n",
"    # train\n",
"    for epoch in range(epoches):\n",
"        mse = np.mean((X.dot(w) - y)**2)\n",
"        mses.append(mse)\n",
"        ws.append(w.flatten())\n",
"        gradients = X.T.dot(X.dot(w) - y)\n",
"        w -= eta * gradients\n",
"        \n",
"    mse = np.mean((X.dot(w) - y)**2)\n",
"    mses.append(mse)  # last mse\n",
"    ws.append(w.flatten())  # we need the last w\n",
"    print(w)\n",
"\n",
"    # save data\n",
"    if save:\n",
"        df = pd.DataFrame(data=ws, columns=['w0', 'w1'])\n",
"        df.to_csv(\"{}.csv\".format(datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')), index=False)\n",
"        df = pd.DataFrame(data=mses, columns=['mse'])\n",
"        df.to_csv(\"{}.csv\".format(datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')), index=False)\n",
"\n",
]
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2.48076741]\n",
" [4.03448317]]\n"
]
}
],
"source": [
"w = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)\n",
"print(w)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.figure.Figure at 0x22a569e2ac8>"
]
},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(x, y, s=10)\n",
"\n",
"xs = np.array([-10, 10])\n",
"Xs = np.c_[np.ones([2,1]), xs]\n",
"ys = Xs.dot(w)\n",
"plt.plot(xs, ys, 'r', linewidth=2)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"source": [
"## 学习率过小"
]
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[13.77884944]\n",
" [ 4.18644247]]\n"
]
}
],
"source": [
]
},
{
"cell_type": "markdown",
"source": [
"## 学习率过大"
]
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[  2.50642535]\n",
" [244.65957398]]\n"
]
}
],
"source": [
]
},
{
"cell_type": "markdown",
"source": [
"# 随机梯度下降"
]
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2.28077622]\n",
" [3.77353814]]\n"
]
}
],
"source": [
"    np.random.seed(42)\n",
"    w = 10 * np.random.randn(2, 1) + 10\n",
"    ws = []\n",
"    mses = []\n",
"    \n",
"    for epoch in range(epoches):\n",
"        for sample in range(m):\n",
"            mse = np.mean((X.dot(w) - y)**2)\n",
"            mses.append([epoch*m+sample, mse])\n",
"            ws.append(w.flatten())\n",
"            \n",
"            index = np.random.randint(m)\n",
"            xi = X[index:index+1]\n",
"            yi = y[index:index+1]\n",
"            gradients = xi.T.dot(xi.dot(w) - yi)\n",
"            w -= eta * gradients\n",
"\n",
"    mse = np.mean((X.dot(w) - y)**2)\n",
"    mses.append([epoch*m+sample, mse])  # last mse\n",
"    ws.append(w.flatten())  # we need the last w\n",
"    print(w)\n",
"            \n",
"    # save data\n",
"    if save:\n",
"        df = pd.DataFrame(data=ws, columns=['w0', 'w1'])\n",
"        df.to_csv(\"{}.csv\".format(datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')), index=False)\n",
"        df = pd.DataFrame(data=mses, columns=['iteration', 'mse'])\n",
"        df.to_csv(\"{}.csv\".format(datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')), index=False)\n",
"    \n",
]
},
{
"cell_type": "markdown",
"source": [
"# 小批量梯度下降"
]
},
{
"cell_type": "code",
"execution_count": 17,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2.7526632 ]\n",
" [4.00159136]]\n"
]
}
],
"source": [
"    np.random.seed(42)\n",
"    w = 10 * np.random.randn(2, 1) + 10\n",
"    ws = []\n",
"    mses = []\n",
"    batches = m // batch_size\n",
"    for epoch in range(epoches):\n",
"        shuffled_indices = np.random.permutation(m)\n",
"        X_suffled = X[shuffled_indices]\n",
"        y_suffled = y[shuffled_indices]\n",
"        for batch in range(batches):\n",
"            mse = np.mean((X.dot(w) - y)**2)\n",
"            mses.append([epoch*batches+batch, mse])\n",
"            ws.append(w.flatten())\n",
"            \n",
"            X_batch = X_suffled[batch:batch*batch_size]\n",
"            y_batch = y_suffled[batch:batch*batch_size]\n",
"            gradients = X_batch.T.dot(X_batch.dot(w) - y_batch)\n",
"            w -= eta * gradients\n",
"\n",
"    mse = np.mean((X.dot(w) - y)**2)\n",
"    mses.append([epoch*batches+batch, mse])  # last mse\n",
"    ws.append(w.flatten())  # we need the last w\n",
"    print(w)\n",
"    \n",
"    # save data\n",
"    if save:\n",
"        df = pd.DataFrame(data=ws, columns=['w0', 'w1'])\n",
"        df.to_csv(\"{}.csv\".format(datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')), index=False)\n",
"        df = pd.DataFrame(data=mses, columns=['iteration', 'mse'])\n",
"        df.to_csv(\"{}.csv\".format(datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')), index=False)\n",
"        \n",
]
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": []
}
],
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
},
"toc": {
"base_numbering": 1,
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}