Sfoglia il codice sorgente

Finish A3 basic part

Zhilong Li 4 anni fa
parent
commit
a9878f35d1

+ 0 - 8
Assignment2/.idea/.gitignore

@@ -1,8 +0,0 @@
-# Default ignored files
-/shelf/
-/workspace.xml
-# Datasource local storage ignored files
-/dataSources/
-/dataSources.local.xml
-# Editor-based HTTP Client requests
-/httpRequests/

+ 0 - 8
Assignment2/.idea/Assignment2.iml

@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<module type="PYTHON_MODULE" version="4">
-  <component name="NewModuleRootManager">
-    <content url="file://$MODULE_DIR$" />
-    <orderEntry type="jdk" jdkName="Python 3.8 (tf)" jdkType="Python SDK" />
-    <orderEntry type="sourceFolder" forTests="false" />
-  </component>
-</module>

+ 0 - 6
Assignment2/.idea/inspectionProfiles/profiles_settings.xml

@@ -1,6 +0,0 @@
-<component name="InspectionProjectProfileManager">
-  <settings>
-    <option name="USE_PROJECT_PROFILE" value="false" />
-    <version value="1.0" />
-  </settings>
-</component>

+ 0 - 4
Assignment2/.idea/misc.xml

@@ -1,4 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (tf)" project-jdk-type="Python SDK" />
-</project>

+ 0 - 8
Assignment2/.idea/modules.xml

@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="ProjectModuleManager">
-    <modules>
-      <module fileurl="file://$PROJECT_DIR$/.idea/Assignment2.iml" filepath="$PROJECT_DIR$/.idea/Assignment2.iml" />
-    </modules>
-  </component>
-</project>

+ 1 - 4
Assignment2/Zhilong_Assignment2.ipynb

@@ -327,7 +327,6 @@
    "cell_type": "code",
    "execution_count": 26,
    "metadata": {
-    "collapsed": false,
     "jupyter": {
      "outputs_hidden": false
     },
@@ -348,7 +347,6 @@
    "cell_type": "code",
    "execution_count": 3,
    "metadata": {
-    "collapsed": false,
     "jupyter": {
      "outputs_hidden": false
     },
@@ -366,7 +364,6 @@
    "cell_type": "code",
    "execution_count": 4,
    "metadata": {
-    "collapsed": false,
     "jupyter": {
      "outputs_hidden": false
     },
@@ -576,7 +573,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.8.12"
+   "version": "3.8.11"
   }
  },
  "nbformat": 4,

+ 187 - 63
Assignment3/A3_Zhilong_Li.ipynb

@@ -2,7 +2,6 @@
  "cells": [
   {
    "cell_type": "markdown",
-   "metadata": {},
    "source": [
     "# Assignment 4\n",
     "\n",
@@ -10,65 +9,70 @@
     "- **Zhilong Li**\n",
     "- **USC ID:** 3884-8441-02\n",
     "- **USC email:** zhilongl@usc.edu"
-   ]
+   ],
+   "metadata": {}
   },
   {
    "cell_type": "code",
-   "execution_count": 1,
-   "metadata": {
-    "ExecuteTime": {
-     "end_time": "2021-10-02T03:23:56.486153Z",
-     "start_time": "2021-10-02T03:23:54.993987Z"
-    }
-   },
-   "outputs": [
-    {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "Init Plugin\n",
-      "Init Graph Optimizer\n",
-      "Init Kernel\n"
-     ]
-    }
-   ],
+   "execution_count": null,
    "source": [
     "from tensorflow import keras\n",
     "import tensorflow as tf\n",
-    "import numpy as np\n",
     "import matplotlib.pyplot as plt\n",
+    "import numpy as np\n",
+    "\n",
     "import pickle\n",
     "plt.style.use('ggplot')"
-   ]
+   ],
+   "outputs": [
+    {
+     "output_type": "error",
+     "ename": "Error",
+     "evalue": "Kernel is dead",
+     "traceback": [
+      "Error: Kernel is dead",
+      "at f._sendKernelShellControl (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/node_modules/@jupyterlab/services.js:3:428116)",
+      "at f.sendShellMessage (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/node_modules/@jupyterlab/services.js:3:427885)",
+      "at f.requestExecute (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/node_modules/@jupyterlab/services.js:3:430437)",
+      "at _.requestExecute (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:32:18027)",
+      "at w.executeCodeCell (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:52:301076)",
+      "at w.execute (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:52:300703)",
+      "at w.start (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:52:296367)",
+      "at runMicrotasks (<anonymous>)",
+      "at processTicksAndRejections (internal/process/task_queues.js:93:5)",
+      "at async t.CellExecutionQueue.executeQueuedCells (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:52:311160)",
+      "at async t.CellExecutionQueue.start (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:52:310700)"
+     ]
+    }
+   ],
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2021-10-02T07:00:13.073742Z",
+     "start_time": "2021-10-02T07:00:12.086679Z"
+    }
+   }
   },
   {
    "cell_type": "code",
    "execution_count": 2,
+   "source": [
+    "global_random_seed = 3884  # np.random.randint(0, 99999)\n",
+    "np.random.seed(global_random_seed)"
+   ],
+   "outputs": [],
    "metadata": {
     "ExecuteTime": {
-     "end_time": "2021-10-02T03:23:58.323626Z",
-     "start_time": "2021-10-02T03:23:58.316529Z"
+     "end_time": "2021-10-02T07:00:14.729167Z",
+     "start_time": "2021-10-02T07:00:14.724918Z"
     }
-   },
-   "outputs": [],
-   "source": [
-    "global_random_seed = 3884 # np.random.randint(0, 99999)\n",
-    "np.random.seed(global_random_seed)"
-   ]
+   }
   },
   {
    "cell_type": "code",
    "execution_count": 3,
-   "metadata": {
-    "ExecuteTime": {
-     "end_time": "2021-10-02T03:24:01.030972Z",
-     "start_time": "2021-10-02T03:24:01.016971Z"
-    }
-   },
-   "outputs": [],
    "source": [
     "def get_model(input_dim, output_dim, width, depth, regularization_param,\n",
-    "                act_func='tanh', output_act_func=False, random_seed=None):\n",
+    "              act_func='tanh', output_act_func=False, random_seed=None):\n",
     "    \"\"\"Construct a sequential model based on the input configuration\n",
     "\n",
     "    Parameters\n",
@@ -91,7 +95,7 @@
     "    keras.Sequential\n",
     "        The constructed model based on the input arguments\n",
     "    \"\"\"\n",
-    "    initializer = keras.initializers.RandomNormal(seed=random_seed)\n",
+    "    initializer = keras.initializers.RandomNormal()\n",
     "    regularizer = keras.regularizers.l2(regularization_param)\n",
     "\n",
     "    if act_func == 'sin':\n",
@@ -99,10 +103,10 @@
     "\n",
     "    all_layers = [keras.layers.InputLayer(input_shape=[input_dim])]\n",
     "    all_layers += [keras.layers.Dense(width, activation=act_func,\n",
-    "                                         kernel_initializer=initializer,\n",
-    "                                         bias_initializer=initializer,\n",
-    "                                         kernel_regularizer=regularizer,\n",
-    "                                         bias_regularizer=regularizer) for _ in range(depth-1)]\n",
+    "                                      kernel_initializer=initializer,\n",
+    "                                      bias_initializer=initializer,\n",
+    "                                      kernel_regularizer=regularizer,\n",
+    "                                      bias_regularizer=regularizer) for _ in range(depth-1)]\n",
     "    all_layers.append(keras.layers.Dense(\n",
     "        output_dim, kernel_initializer=initializer,\n",
     "        activation=act_func if output_act_func else None,\n",
@@ -111,52 +115,169 @@
     "        bias_regularizer=regularizer))\n",
     "    m = keras.Sequential(all_layers)\n",
     "    return m"
-   ]
+   ],
+   "outputs": [],
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2021-10-02T07:00:20.353060Z",
+     "start_time": "2021-10-02T07:00:20.342013Z"
+    }
+   }
   },
   {
    "cell_type": "code",
    "execution_count": 4,
-   "metadata": {
-    "ExecuteTime": {
-     "end_time": "2021-10-02T03:24:06.384304Z",
-     "start_time": "2021-10-02T03:24:06.306974Z"
-    }
-   },
+   "source": [
+    "tf.keras.backend.clear_session()\n",
+    "model = get_model(1, 1, 20, 5, 1e-7, act_func='sin', output_act_func=False)"
+   ],
    "outputs": [
     {
-     "name": "stdout",
      "output_type": "stream",
+     "name": "stdout",
      "text": [
-      "Metal device set to: Apple M1\n"
+      "Metal device set to: Apple M1\n",
+      "\n",
+      "systemMemory: 16.00 GB\n",
+      "maxCacheSize: 5.33 GB\n",
+      "\n"
      ]
     },
     {
+     "output_type": "stream",
      "name": "stderr",
+     "text": [
+      "2021-10-02 21:13:31.476496: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.\n",
+      "2021-10-02 21:13:31.476623: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)\n"
+     ]
+    }
+   ],
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2021-10-02T08:06:56.053459Z",
+     "start_time": "2021-10-02T08:06:56.002144Z"
+    }
+   }
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "source": [
+    "n_train = 100\n",
+    "x_train = tf.reshape(tf.linspace(0.0, 1.0, n_train), [100, 1])\n",
+    "optimizer = keras.optimizers.Adam(learning_rate=1e-2)\n",
+    "Pe = 10\n",
+    "Da = 10\n",
+    "lambda_b = 1000\n",
+    "with tf.device('/cpu:0'):\n",
+    "    for ep in range(3001):\n",
+    "        with tf.GradientTape() as tape_0:\n",
+    "            with tf.GradientTape() as tape_1:\n",
+    "                tape_1.watch(x_train)\n",
+    "                with tf.GradientTape() as tape_2:\n",
+    "                    tape_2.watch(x_train)\n",
+    "                    u = model(x_train)\n",
+    "                du_dx = tape_2.gradient(u, x_train)\n",
+    "            d2u_dx2 = tape_1.gradient(du_dx, x_train)\n",
+    "            int_loss = sum(map(lambda d2, d, u: (d2-Pe*d+Da*u*(1-u))**2,\n",
+    "                               d2u_dx2, du_dx, u))/n_train\n",
+    "            boundary_loss = lambda_b*(u[0]**2+(u[-1]-1)**2)\n",
+    "            loss = int_loss + boundary_loss + sum(model.losses)\n",
+    "        grads = tape_0.gradient(loss, model.trainable_variables)\n",
+    "        optimizer.apply_gradients(zip(grads, model.trainable_variables))\n",
+    "        if not ep % 100 or ep == 1000:\n",
+    "            print(ep, loss)\n"
+   ],
+   "outputs": [
+    {
      "output_type": "stream",
+     "name": "stdout",
      "text": [
-      "2021-10-01 20:24:06.336225: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.\n",
-      "2021-10-01 20:24:06.336513: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)\n"
+      "0 tf.Tensor([1039.3241], shape=(1,), dtype=float32)\n",
+      "100 tf.Tensor([58.22947], shape=(1,), dtype=float32)\n",
+      "200 tf.Tensor([56.328785], shape=(1,), dtype=float32)\n",
+      "300 tf.Tensor([55.604458], shape=(1,), dtype=float32)\n",
+      "400 tf.Tensor([57.53366], shape=(1,), dtype=float32)\n",
+      "500 tf.Tensor([49.33706], shape=(1,), dtype=float32)\n",
+      "600 tf.Tensor([61.896923], shape=(1,), dtype=float32)\n",
+      "700 tf.Tensor([48.654636], shape=(1,), dtype=float32)\n",
+      "800 tf.Tensor([79.09517], shape=(1,), dtype=float32)\n",
+      "900 tf.Tensor([82.4559], shape=(1,), dtype=float32)\n",
+      "1000 tf.Tensor([34.646717], shape=(1,), dtype=float32)\n",
+      "1100 tf.Tensor([7.691236], shape=(1,), dtype=float32)\n",
+      "1200 tf.Tensor([2.3371177], shape=(1,), dtype=float32)\n",
+      "1300 tf.Tensor([6.0998716], shape=(1,), dtype=float32)\n",
+      "1400 tf.Tensor([0.35021833], shape=(1,), dtype=float32)\n",
+      "1500 tf.Tensor([1.1403896], shape=(1,), dtype=float32)\n",
+      "1600 tf.Tensor([0.11293], shape=(1,), dtype=float32)\n",
+      "1700 tf.Tensor([2.166767], shape=(1,), dtype=float32)\n",
+      "1800 tf.Tensor([0.0864344], shape=(1,), dtype=float32)\n",
+      "1900 tf.Tensor([2.6307728], shape=(1,), dtype=float32)\n",
+      "2000 tf.Tensor([0.977543], shape=(1,), dtype=float32)\n",
+      "2100 tf.Tensor([0.2197722], shape=(1,), dtype=float32)\n",
+      "2200 tf.Tensor([3.6889656], shape=(1,), dtype=float32)\n",
+      "2300 tf.Tensor([0.08489634], shape=(1,), dtype=float32)\n",
+      "2400 tf.Tensor([0.5404036], shape=(1,), dtype=float32)\n",
+      "2500 tf.Tensor([0.10080672], shape=(1,), dtype=float32)\n",
+      "2600 tf.Tensor([0.17384219], shape=(1,), dtype=float32)\n",
+      "2700 tf.Tensor([1.1919072], shape=(1,), dtype=float32)\n",
+      "2800 tf.Tensor([0.7122941], shape=(1,), dtype=float32)\n",
+      "2900 tf.Tensor([1.3751904], shape=(1,), dtype=float32)\n",
+      "3000 tf.Tensor([0.14365542], shape=(1,), dtype=float32)\n"
      ]
     }
    ],
+   "metadata": {
+    "ExecuteTime": {
+     "start_time": "2021-10-02T08:07:24.274Z"
+    }
+   }
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
    "source": [
-    "tf.keras.backend.clear_session()\n",
-    "model = get_model(1,1,20,5,1.0e-7,act_func='sin',output_act_func=False)"
-   ]
+    "res = np.load(f'ref/diffusion-advection-reaction_pe_{Pe}_da_{Da}.npy')\n",
+    "plt.figure(figsize=[10,6])\n",
+    "plt.plot(res[0],res[1],'r')\n",
+    "pred = model.predict(x_train)\n",
+    "plt.plot(x_train, pred, 'b--',linewidth=2)"
+   ],
+   "outputs": [
+    {
+     "output_type": "error",
+     "ename": "Error",
+     "evalue": "Kernel is dead",
+     "traceback": [
+      "Error: Kernel is dead",
+      "at f._sendKernelShellControl (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/node_modules/@jupyterlab/services.js:3:428116)",
+      "at f.sendShellMessage (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/node_modules/@jupyterlab/services.js:3:427885)",
+      "at f.requestExecute (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/node_modules/@jupyterlab/services.js:3:430437)",
+      "at _.requestExecute (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:32:18027)",
+      "at w.executeCodeCell (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:52:301076)",
+      "at w.execute (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:52:300703)",
+      "at w.start (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:52:296367)",
+      "at runMicrotasks (<anonymous>)",
+      "at processTicksAndRejections (internal/process/task_queues.js:93:5)",
+      "at async t.CellExecutionQueue.executeQueuedCells (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:52:311160)",
+      "at async t.CellExecutionQueue.start (/Users/zhilong/.vscode/extensions/ms-toolsai.jupyter-2021.8.2041215044/out/client/extension.js:52:310700)"
+     ]
+    }
+   ],
+   "metadata": {}
   },
   {
    "cell_type": "code",
    "execution_count": null,
-   "metadata": {},
+   "source": [],
    "outputs": [],
-   "source": []
+   "metadata": {}
   }
  ],
  "metadata": {
   "kernelspec": {
-   "display_name": "Python 3 (ipykernel)",
-   "language": "python",
-   "name": "python3"
+   "name": "python3",
+   "display_name": "Python 3.8.12 64-bit ('tf': conda)"
   },
   "language_info": {
    "codemirror_mode": {
@@ -198,8 +319,11 @@
     "_Feature"
    ],
    "window_display": false
+  },
+  "interpreter": {
+   "hash": "859cadc33cf9c93d9354f622d0bfdc75f7d75fc23a9b22a18dfc646072acf83e"
   }
  },
  "nbformat": 4,
  "nbformat_minor": 2
-}
+}

File diff suppressed because it is too large
+ 79 - 79
Assignment3/Custom_training_demo.ipynb


File diff suppressed because it is too large
+ 4 - 2
Assignment3/Gradients_in_TF2.ipynb


BIN
Assignment3/ref/diffusion-advection-reaction_pe_0.1_da_0.1.npy


BIN
Assignment3/ref/diffusion-advection-reaction_pe_0.1_da_10.npy


BIN
Assignment3/ref/diffusion-advection-reaction_pe_10_da_0.1.npy


BIN
Assignment3/ref/diffusion-advection-reaction_pe_10_da_10.npy


Some files were not shown because too many files changed in this diff