Skip to content

Instantly share code, notes, and snippets.

@seraphy
Last active July 27, 2019 02:29
Show Gist options
  • Save seraphy/1f4c2155ba6d80f026d320def765d908 to your computer and use it in GitHub Desktop.
Save seraphy/1f4c2155ba6d80f026d320def765d908 to your computer and use it in GitHub Desktop.
Tensorflow(python3.6)で一次関数を学習させ一次関数を近似させる例
numpy
matplotlib
pandas
tensorflow
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import tensorflow as tf\nimport numpy as np\nimport time\nimport random\nimport math\nimport matplotlib.pyplot as plt\n\nprint(tf.__version__)\n",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "1.12.2\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "start = time.time()\n",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x_data = np.random.rand(100).astype(\"float32\")\ny_data = 0.1 * x_data + 0.3",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "w = tf.Variable(tf.random_uniform([1], -1.0, 1.0))\nb = tf.Variable(tf.zeros([1]))\ny = w * x_data + b\n",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "loss = tf.reduce_mean(tf.square(y_data - y))\noptimizer = tf.train.GradientDescentOptimizer(0.5)\ntrain = optimizer.minimize(loss)\n",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "init = tf.global_variables_initializer()\nsess = tf.Session()\nsess.run(init)\n",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for step in range(1001):\n sess.run(train)\n if step % 100 == 0:\n print(f\"{step} {sess.run(w)}, {sess.run(b)}\")",
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": "0 [-0.03927329], [0.4752938]\n100 [0.09979425], [0.3001]\n200 [0.09999977], [0.30000013]\n300 [0.0999999], [0.30000007]\n400 [0.0999999], [0.30000007]\n500 [0.0999999], [0.30000007]\n600 [0.0999999], [0.30000007]\n700 [0.0999999], [0.30000007]\n800 [0.0999999], [0.30000007]\n900 [0.0999999], [0.30000007]\n1000 [0.0999999], [0.30000007]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "plot_x = []\nplot_y = []\nfor i in range(100):\n result = sess.run(w) * i + sess.run(b)\n plot_x.append(i)\n plot_y.append(result)\n print(f\"{i} {result}\")",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "0 [0.30000007]\n1 [0.39999998]\n2 [0.49999988]\n3 [0.5999998]\n4 [0.6999997]\n5 [0.7999996]\n6 [0.8999995]\n7 [0.9999994]\n8 [1.0999993]\n9 [1.1999992]\n10 [1.2999991]\n11 [1.399999]\n12 [1.4999989]\n13 [1.5999988]\n14 [1.6999987]\n15 [1.7999986]\n16 [1.8999985]\n17 [1.9999985]\n18 [2.0999985]\n19 [2.1999984]\n20 [2.2999983]\n21 [2.3999982]\n22 [2.499998]\n23 [2.599998]\n24 [2.699998]\n25 [2.7999978]\n26 [2.8999977]\n27 [2.9999976]\n28 [3.0999975]\n29 [3.1999974]\n30 [3.2999973]\n31 [3.3999972]\n32 [3.4999971]\n33 [3.599997]\n34 [3.699997]\n35 [3.7999969]\n36 [3.8999968]\n37 [3.9999967]\n38 [4.0999966]\n39 [4.1999965]\n40 [4.2999964]\n41 [4.3999963]\n42 [4.499996]\n43 [4.599996]\n44 [4.699996]\n45 [4.799996]\n46 [4.899996]\n47 [4.9999957]\n48 [5.0999956]\n49 [5.1999955]\n50 [5.2999954]\n51 [5.3999953]\n52 [5.499995]\n53 [5.599995]\n54 [5.699995]\n55 [5.799995]\n56 [5.899995]\n57 [5.9999948]\n58 [6.0999947]\n59 [6.1999946]\n60 [6.2999945]\n61 [6.3999944]\n62 [6.4999943]\n63 [6.599994]\n64 [6.699994]\n65 [6.799994]\n66 [6.899994]\n67 [6.999994]\n68 [7.0999937]\n69 [7.1999936]\n70 [7.2999935]\n71 [7.3999934]\n72 [7.4999933]\n73 [7.599993]\n74 [7.699993]\n75 [7.799993]\n76 [7.899993]\n77 [7.999993]\n78 [8.099993]\n79 [8.199992]\n80 [8.299993]\n81 [8.399993]\n82 [8.499992]\n83 [8.599992]\n84 [8.699992]\n85 [8.799993]\n86 [8.899992]\n87 [8.999991]\n88 [9.099992]\n89 [9.199992]\n90 [9.299992]\n91 [9.399991]\n92 [9.499991]\n93 [9.599992]\n94 [9.699991]\n95 [9.799991]\n96 [9.899991]\n97 [9.999991]\n98 [10.099991]\n99 [10.19999]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sess.close()",
"execution_count": 9,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "plt.scatter(plot_x, plot_y)\nplt.show()",
"execution_count": 10,
"outputs": [
{
"output_type": "display_data",
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvqOYd8AAAEElJREFUeJzt3X+MZWV9x/H3dwfURSULZbXuwnQhIbTERiGThkpjCNiASMQ0NsXUllqa+adVsBYLbRrSP5raYCw0MSYTQLAS/IEbJKYRCUpMm3brLhBBVgrVCruzumvaUWOmAXa//ePeoZdhZ+6Pc3+c85z3K9nM3DNn9jwnz+4nn3nuc+9EZiJJar4tsx6AJGk8DHRJKoSBLkmFMNAlqRAGuiQVwkCXpEIY6JJUCANdkgphoEtSIU6Y5sVOO+203LVr1zQvKUmNt2/fvh9n5vZ+50010Hft2sXevXuneUlJaryI+MEg57nkIkmFMNAlqRAGuiQVwkCX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JuhX4amb+MvAWOvdf7FxHxE7gQ8BCZr6ZzosRr6LMub4TuGzdsY3m9p3A2d0/i8Cnqly49oFOz9v0ZubzwNrb9BYlMw9l5iPdz39G5z/4Tjr3elf3tLuA98xmhJMREacD7wJu6z4O4GLg3u4pJd7zycDbgdsBMvP5zFyh8Lmm88r0rRFxAnAScIgC5zozvwn897rDG83tlcBnsuPfgG0R8aZRr92EQG/d2/RGxC7gPGAP8MbMPASd0AfeMLuRTcQtwEeBY93HvwCsZOaL3cclzvdZwBHg092lptsi4rUUPNeZeRD4OPAsnSD/CbCP8ud6zUZzO9Z8a0KgD/Q2vaWIiNcBXwKuy8yfzno8kxQRVwCHM3Nf7+HjnFrafJ8AnA98KjPPA35OQcsrx9NdM74SOBPYAbyWznLDeqXNdT9j/ffehEAf6G16SxARJ9IJ87szc3f38I/WfgTrfjw8q/FNwIXAuyPiv+gspV1Mp7Fv6/5YDmXO9wHgQGbu6T6+l07AlzzX7wC+n5lHMvMFYDfwNsqf6zUbze1Y860Jgd6Kt+ntrh3fDuzPzE/0fOl+4Oru51cDX5722CYlM2/MzNMzcxedef16Zv4u8A3gvd3TirpngMz8IfBcRJzTPXQJnV/dWOxc01lquSAiTur+W1+756LnusdGc3s/8Pvd3S4XAD9ZW5oZSWbW/g9wOfAfwH8Cfznr8UzoHn+Dzo9a3wYe6/65nM6a8kPA092Pp856rBO6/4uAr3Q/Pwv4d+AZ4IvAq2c9vgnc71uBvd35vg84pfS5Bv4a+C7wBPCPwKtLnGvgHjrPE7xAp4Ffs9Hc0lly+WQ32x6nswto5Gv7SlFJKkQTllwkSQMw0CWpEAa6JBXCQJekQhjoklQIA12SCmGgS1IhDHRJKsT/ASsz1gQHRunKAAAAAElFTkSuQmCC\n",
"text/plain": "<Figure size 432x288 with 1 Axes>"
},
"metadata": {
"needs_background": "light"
}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "timer = time.time() - start\nprint(f\"time:{timer}sec\")",
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": "time:6.7154457569122314sec\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python36",
"display_name": "Python 3.6",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"name": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6",
"file_extension": ".py",
"codemirror_mode": {
"version": 3,
"name": "ipython"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment