瀏覽代碼

some works

Zhilong Li 4 年之前
父節點
當前提交
1b1b9edbdc
共有 3 個文件被更改,包括 194 次插入5 次删除
  1. 3 0
      .gitignore
  2. 191 5
      Assignment3/A3_Zhilong_Li.ipynb
  3. 二進制
      Assignment3/Assignment03.pdf

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+.DS_Store
+.ipynb_checkpoints/
+.idea/

+ 191 - 5
Assignment3/A3_Zhilong_Li.ipynb

@@ -1,19 +1,205 @@
 {
  "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Assignment 4\n",
+    "\n",
+    "\n",
+    "- **Zhilong Li**\n",
+    "- **USC ID:** 3884-8441-02\n",
+    "- **USC email:** zhilongl@usc.edu"
+   ]
+  },
+  {
+   "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"
+     ]
+    }
+   ],
+   "source": [
+    "from tensorflow import keras\n",
+    "import tensorflow as tf\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "import pickle\n",
+    "plt.style.use('ggplot')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2021-10-02T03:23:58.323626Z",
+     "start_time": "2021-10-02T03:23:58.316529Z"
+    }
+   },
+   "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",
+    "    \"\"\"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(seed=random_seed)\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"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {
+    "ExecuteTime": {
+     "end_time": "2021-10-02T03:24:06.384304Z",
+     "start_time": "2021-10-02T03:24:06.306974Z"
+    }
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Metal device set to: Apple M1\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "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"
+     ]
+    }
+   ],
+   "source": [
+    "tf.keras.backend.clear_session()\n",
+    "model = get_model(1,1,20,5,1.0e-7,act_func='sin',output_act_func=False)"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
-   "source": [],
+   "metadata": {},
    "outputs": [],
-   "metadata": {}
+   "source": []
   }
  ],
  "metadata": {
-  "orig_nbformat": 4,
+  "kernelspec": {
+   "display_name": "Python 3 (ipykernel)",
+   "language": "python",
+   "name": "python3"
+  },
   "language_info": {
-   "name": "python"
+   "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
   }
  },
  "nbformat": 4,
  "nbformat_minor": 2
-}
+}

二進制
Assignment3/Assignment03.pdf