{ "cells": [ { "cell_type": "markdown", "source": [ "# Assignment 4\n", "\n", "\n", "- **Zhilong Li**\n", "- **USC ID:** 3884-8441-02\n", "- **USC email:** zhilongl@usc.edu" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "from tensorflow import keras\n", "import tensorflow as tf\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 ()", "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-02T07:00:14.729167Z", "start_time": "2021-10-02T07:00:14.724918Z" } } }, { "cell_type": "code", "execution_count": 3, "source": [ "def get_model(input_dim, output_dim, width, depth, regularization_param,\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", " ----------\n", " input_dim : int\n", " output_dim : int\n", " width : int\n", " depth : int\n", " regularization_param : float\n", " Number of hidden layers + output layer\n", " act_func : str, optional\n", " Which activation function this model will be using, by default 'tanh'\n", " output_act_func : bool, optional\n", " Should the output layer use an activation function, by default True\n", " random_seed : int\n", " Seed to initialize bias and weights of the network\n", "\n", " Returns\n", " -------\n", " keras.Sequential\n", " The constructed model based on the input arguments\n", " \"\"\"\n", " initializer = keras.initializers.RandomNormal()\n", " regularizer = keras.regularizers.l2(regularization_param)\n", "\n", " if act_func == 'sin':\n", " act_func = tf.math.sin\n", "\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", " all_layers.append(keras.layers.Dense(\n", " output_dim, kernel_initializer=initializer,\n", " activation=act_func if output_act_func else None,\n", " bias_initializer=initializer,\n", " kernel_regularizer=regularizer,\n", " 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, "source": [ "tf.keras.backend.clear_session()\n", "model = get_model(1, 1, 20, 5, 1e-7, act_func='sin', output_act_func=False)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "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: )\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": [ "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": [ "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 ()", "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, "source": [], "outputs": [], "metadata": {} } ], "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3.8.12 64-bit ('tf': conda)" }, "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.8.12" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false }, "interpreter": { "hash": "859cadc33cf9c93d9354f622d0bfdc75f7d75fc23a9b22a18dfc646072acf83e" } }, "nbformat": 4, "nbformat_minor": 2 }