{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Concise Implementation of Linear Regression" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:59:54.049026Z", "start_time": "2019-07-03T21:59:52.500967Z" }, "attributes": { "classes": [], "id": "", "n": "2" } }, "outputs": [], "source": [ "import d2l\n", "from mxnet import autograd, np, npx, gluon\n", "npx.set_np()\n", "\n", "true_w = np.array([2, -3.4])\n", "true_b = 4.2\n", "features, labels = d2l.synthetic_data(true_w, true_b, 1000)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Reading Data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:59:54.120474Z", "start_time": "2019-07-03T21:59:54.051605Z" }, "attributes": { "classes": [], "id": "", "n": "3" }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X =\n", "[[ 0.4015098 1.4096868 ]\n", " [ 0.65820086 -1.4260322 ]\n", " [ 0.00153129 -0.14330608]\n", " [-0.843129 0.6070013 ]\n", " [ 1.5080738 -0.27229312]\n", " [-0.01436996 0.50522786]\n", " [-0.2513225 -0.7733599 ]\n", " [-0.4892422 0.82852226]\n", " [ 0.19469471 0.26424283]\n", " [ 0.8269238 1.0562588 ]]y =\n", "[ 0.21267602 10.392891 4.7093544 0.45944032 8.140177 2.475876\n", " 6.332503 0.4059622 3.69898 2.2603266 ]\n" ] } ], "source": [ "def load_array(data_arrays, batch_size, is_train=True):\n", " dataset = gluon.data.ArrayDataset(*data_arrays)\n", " return gluon.data.DataLoader(dataset, batch_size, shuffle=is_train)\n", " \n", "batch_size = 10\n", "data_iter = load_array((features, labels), batch_size)\n", "for X, y in data_iter:\n", " print('X =\\n%sy =\\n%s' %(X, y))\n", " break" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Define the Model and initialize Model Parameters" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:59:54.129952Z", "start_time": "2019-07-03T21:59:54.124685Z" }, "attributes": { "classes": [], "id": "", "n": "5" }, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "from mxnet.gluon import nn\n", "from mxnet import init\n", "\n", "net = nn.Sequential()\n", "net.add(nn.Dense(1))\n", "net.initialize(init.Normal(sigma=0.01))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Define the loss function and optimization algorithm" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:59:54.135619Z", "start_time": "2019-07-03T21:59:54.132150Z" }, "attributes": { "classes": [], "id": "", "n": "8" } }, "outputs": [], "source": [ "from mxnet import gluon\n", "\n", "loss = gluon.loss.L2Loss() \n", "trainer = gluon.Trainer(net.collect_params(),\n", " 'sgd', {'learning_rate': 0.03})" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Training" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:59:56.549677Z", "start_time": "2019-07-03T21:59:54.137373Z" }, "attributes": { "classes": [], "id": "", "n": "10" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "epoch 1, loss: 0.040749\n", "epoch 2, loss: 0.000152\n", "epoch 3, loss: 0.000051\n", "Error in estimating w [[ 0.00024056 -0.00077081]]\n", "Error in estimating b [0.00041628]\n" ] } ], "source": [ "for epoch in range(1, 4):\n", " for X, y in data_iter:\n", " with autograd.record():\n", " l = loss(net(X), y)\n", " l.backward()\n", " trainer.step(batch_size)\n", " l = loss(net(features), labels)\n", " print('epoch %d, loss: %f' % (epoch, l.mean()))\n", " \n", "w = net[0].weight.data()\n", "print('Error in estimating w', true_w.reshape(w.shape) - w)\n", "b = net[0].bias.data()\n", "print('Error in estimating b', true_b - b) " ] } ], "metadata": { "celltoolbar": "Slideshow", "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.7.1" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }