Guest User

Untitled

a guest
Jun 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stdout",
  10. "output_type": "stream",
  11. "text": [
  12. "1.8.0\n"
  13. ]
  14. }
  15. ],
  16. "source": [
  17. "import numpy as np\n",
  18. "import tensorflow as tf\n",
  19. "\n",
  20. "print(tf.__version__)"
  21. ]
  22. },
  23. {
  24. "cell_type": "code",
  25. "execution_count": 2,
  26. "metadata": {},
  27. "outputs": [],
  28. "source": [
  29. "# Describe TF Graph: standard normalization of inputs and ouputs (z-score) + linear regression\n",
  30. "\n",
  31. "batch_size = 100\n",
  32. "\n",
  33. "a = tf.get_variable(\"a\", [1], dtype=tf.float32)\n",
  34. "b = tf.get_variable(\"b\", [1], dtype=tf.float32)\n",
  35. "\n",
  36. "x = tf.placeholder(tf.float32, shape=[batch_size])\n",
  37. "x_mean = tf.placeholder(tf.float32, shape=[1])\n",
  38. "x_std = tf.placeholder(tf.float32, shape=[1])\n",
  39. "x_norm = (x - x_mean) / x_std\n",
  40. "\n",
  41. "y = a * x_norm + b\n",
  42. "\n",
  43. "y_true = tf.placeholder(tf.float32, shape=[batch_size])\n",
  44. "y_mean = tf.placeholder(tf.float32, shape=[1])\n",
  45. "y_std = tf.placeholder(tf.float32, shape=[1])\n",
  46. "y_true_norm = (y_true - y_mean) / y_std\n",
  47. "\n",
  48. "loss = tf.losses.mean_squared_error(labels=y_true_norm, predictions=y)\n",
  49. "\n",
  50. "optimizer = tf.train.GradientDescentOptimizer(0.01)\n",
  51. "train = optimizer.minimize(loss)\n",
  52. "\n",
  53. "# note: this part of the graph will be used only at inference time\n",
  54. "y_denorm = y * y_std + y_mean\n",
  55. "\n",
  56. "# write Graph to TensorBoard\n",
  57. "writer = tf.summary.FileWriter('.')\n",
  58. "writer.add_graph(tf.get_default_graph())"
  59. ]
  60. },
  61. {
  62. "cell_type": "code",
  63. "execution_count": 3,
  64. "metadata": {},
  65. "outputs": [
  66. {
  67. "name": "stdout",
  68. "output_type": "stream",
  69. "text": [
  70. "Loss: 3.5527137e-13\n"
  71. ]
  72. },
  73. {
  74. "data": {
  75. "text/plain": [
  76. "array([ 0.9941406, 2.9941406, 4.9941406, 6.9941406, 8.994141 ,\n",
  77. " 10.994141 , 12.993164 , 14.993164 , 16.993164 , 18.99414 ,\n",
  78. " 20.99414 , 22.99414 , 24.99414 , 26.99414 , 28.99414 ,\n",
  79. " 30.99414 , 32.993164 , 34.993164 , 36.993164 , 38.99414 ,\n",
  80. " 40.99414 , 42.99414 , 44.99414 , 46.99414 , 48.99414 ,\n",
  81. " 50.99414 , 52.993164 , 54.993164 , 56.993164 , 58.99414 ,\n",
  82. " 60.99414 , 62.99414 , 64.99414 , 66.99414 , 68.99414 ,\n",
  83. " 70.99414 , 72.993164 , 74.993164 , 76.993164 , 78.99414 ,\n",
  84. " 80.99414 , 82.99414 , 84.99414 , 86.99414 , 88.99414 ,\n",
  85. " 90.99414 , 92.993164 , 94.993164 , 96.99414 , 98.99414 ,\n",
  86. " 100.99414 , 102.99414 , 104.99414 , 106.99414 , 108.99414 ,\n",
  87. " 110.99414 , 112.993164 , 114.993164 , 116.99414 , 118.99414 ,\n",
  88. " 120.99414 , 122.99414 , 124.99414 , 126.99414 , 128.99414 ,\n",
  89. " 130.99414 , 132.99316 , 134.99316 , 136.99414 , 138.99414 ,\n",
  90. " 140.99414 , 142.99414 , 144.99414 , 146.99414 , 148.99414 ,\n",
  91. " 150.99316 , 152.99316 , 154.99316 , 156.99414 , 158.99414 ,\n",
  92. " 160.99414 , 162.99414 , 164.99414 , 166.99414 , 168.99414 ,\n",
  93. " 170.99316 , 172.99316 , 174.99316 , 176.99414 , 178.99414 ,\n",
  94. " 180.99414 , 182.99414 , 184.99414 , 186.99414 , 188.99414 ,\n",
  95. " 190.99316 , 192.99316 , 194.99316 , 196.99414 , 198.99414 ],\n",
  96. " dtype=float32)"
  97. ]
  98. },
  99. "execution_count": 3,
  100. "metadata": {},
  101. "output_type": "execute_result"
  102. }
  103. ],
  104. "source": [
  105. "# Data\n",
  106. "\n",
  107. "x_values = np.arange(10000)\n",
  108. "y_true_values = x_values * 2 + 1\n",
  109. "\n",
  110. "epoch = 100\n",
  111. "\n",
  112. "training_data_moments = {\n",
  113. " x_mean:[np.mean(x_values)],\n",
  114. " x_std:[np.std(x_values)],\n",
  115. " y_mean:[np.mean(y_true_values)],\n",
  116. " y_std:[np.std(y_true_values)]\n",
  117. "}\n",
  118. "\n",
  119. "# Run training\n",
  120. "sess = tf.Session()\n",
  121. "\n",
  122. "init = tf.global_variables_initializer()\n",
  123. "sess.run(init)\n",
  124. "\n",
  125. "for _ in range(epoch):\n",
  126. " for i in range(0, len(x_values), batch_size):\n",
  127. " x_batch = x_values[i:i+batch_size]\n",
  128. " y_batch = y_true_values[i:i+batch_size]\n",
  129. " _, current_loss = sess.run((train, loss), feed_dict={x:x_batch, y_true:y_batch, **training_data_moments})\n",
  130. " \n",
  131. "print(\"Loss: \" + str(current_loss))\n",
  132. " \n",
  133. "# Inference (note: x normalization and y denormalization has been serialized into TF Graph)\n",
  134. "y_test = sess.run(y_denorm, feed_dict={x:x_values[0:batch_size], **training_data_moments})\n",
  135. "\n",
  136. "y_test"
  137. ]
  138. }
  139. ],
  140. "metadata": {
  141. "kernelspec": {
  142. "display_name": "Python 3",
  143. "language": "python",
  144. "name": "python3"
  145. },
  146. "language_info": {
  147. "codemirror_mode": {
  148. "name": "ipython",
  149. "version": 3
  150. },
  151. "file_extension": ".py",
  152. "mimetype": "text/x-python",
  153. "name": "python",
  154. "nbconvert_exporter": "python",
  155. "pygments_lexer": "ipython3",
  156. "version": "3.6.3"
  157. }
  158. },
  159. "nbformat": 4,
  160. "nbformat_minor": 2
  161. }
Add Comment
Please, Sign In to add comment