├── .github └── workflows │ ├── proof-html.yml │ └── auto-assign.yml ├── README.md └── Credit_card_fraud ├── creditcardfraud.ipynb └── GAN_171103.py /.github/workflows/proof-html.yml: -------------------------------------------------------------------------------- 1 | name: Proof HTML 2 | on: 3 | push: 4 | workflow_dispatch: 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: anishathalye/proof-html@v1.1.0 10 | with: 11 | directory: ./ 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to your organization's Credit_card_monitoring_and_check repository 2 | This code repository (or "repo") is designed to demonstrate the best GitHub has to offer with the least amount of noise. 3 | 4 | The repo includes an Credit_card_fraud and Credit_monitoring two GitHub Actions workflows. 5 | 6 | Please download the dataset from [Credit Card Fraud](https://www.kaggle.com/datasets/mlg-ulb/creditcardfraud) and [Credit Monitoring](https://www.kaggle.com/competitions/GiveMeSomeCredit/data) 7 | 8 | -------------------------------------------------------------------------------- /.github/workflows/auto-assign.yml: -------------------------------------------------------------------------------- 1 | name: Auto Assign 2 | on: 3 | issues: 4 | types: [opened] 5 | pull_request: 6 | types: [opened] 7 | jobs: 8 | run: 9 | runs-on: ubuntu-latest 10 | permissions: 11 | issues: write 12 | pull-requests: write 13 | steps: 14 | - name: 'Auto-assign issue' 15 | uses: pozil/auto-assign-issue@v1 16 | with: 17 | repo-token: ${{ secrets.GITHUB_TOKEN }} 18 | assignees: michael-yc 19 | numOfAssignee: 1 20 | -------------------------------------------------------------------------------- /Credit_card_fraud/creditcardfraud.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "#importing packages\n", 10 | "%matplotlib inline\n", 11 | "import scipy.stats as stats\n", 12 | "import numpy as np\n", 13 | "import pandas as pd\n", 14 | "import matplotlib.pyplot as plt\n", 15 | "import seaborn as sns\n", 16 | "plt.style.use('ggplot')" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "ename": "FileNotFoundError", 26 | "evalue": "[Errno 2] File b'creditcard.csv' does not exist: b'creditcard.csv'", 27 | "output_type": "error", 28 | "traceback": [ 29 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 30 | "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 31 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_csv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'creditcard.csv'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 32 | "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py\u001b[0m in \u001b[0;36mparser_f\u001b[0;34m(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)\u001b[0m\n\u001b[1;32m 700\u001b[0m skip_blank_lines=skip_blank_lines)\n\u001b[1;32m 701\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 702\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_read\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilepath_or_buffer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 703\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 704\u001b[0m \u001b[0mparser_f\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__name__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 33 | "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py\u001b[0m in \u001b[0;36m_read\u001b[0;34m(filepath_or_buffer, kwds)\u001b[0m\n\u001b[1;32m 427\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 428\u001b[0m \u001b[0;31m# Create the parser.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 429\u001b[0;31m \u001b[0mparser\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mTextFileReader\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilepath_or_buffer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 430\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 431\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mchunksize\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0miterator\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 34 | "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, f, engine, **kwds)\u001b[0m\n\u001b[1;32m 893\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moptions\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'has_index_names'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mkwds\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'has_index_names'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 894\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 895\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_engine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mengine\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 896\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 897\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 35 | "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py\u001b[0m in \u001b[0;36m_make_engine\u001b[0;34m(self, engine)\u001b[0m\n\u001b[1;32m 1120\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_make_engine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mengine\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'c'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1121\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mengine\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'c'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1122\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mCParserWrapper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moptions\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1123\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1124\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mengine\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'python'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 36 | "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, src, **kwds)\u001b[0m\n\u001b[1;32m 1851\u001b[0m \u001b[0mkwds\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'usecols'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0musecols\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1852\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1853\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_reader\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mparsers\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTextReader\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msrc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1854\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munnamed_cols\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_reader\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munnamed_cols\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1855\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 37 | "\u001b[0;32mpandas/_libs/parsers.pyx\u001b[0m in \u001b[0;36mpandas._libs.parsers.TextReader.__cinit__\u001b[0;34m()\u001b[0m\n", 38 | "\u001b[0;32mpandas/_libs/parsers.pyx\u001b[0m in \u001b[0;36mpandas._libs.parsers.TextReader._setup_parser_source\u001b[0;34m()\u001b[0m\n", 39 | "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] File b'creditcard.csv' does not exist: b'creditcard.csv'" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "df = pd.read_csv('creditcard.csv')" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "#shape\n", 54 | "print('The data frame has {} rows and {} columns.'.format(df.shape[0], df.shape[1]))" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "#peek at data\n", 64 | "df.sample(6)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "#info\n", 74 | "df.info()" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "#numerical summary -> only non-anonymized columns of interest\n", 84 | "pd.set_option('precision', 3)\n", 85 | "df.loc[:, ['Time', 'Amount']].describe()" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "plt.figure(figsize=(10,8))\n", 95 | "plt.title('Distribution of The Monetary Value Feature')\n", 96 | "sns.distplot(df.Amount)\n", 97 | "\n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "#visualizations of time and amount\n", 107 | "plt.figure(figsize=(10,8))\n", 108 | "plt.title('Distribution of The Time Feature')\n", 109 | "sns.distplot(df.Time)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "Around 88 dollars is the mean of all credit card transactions in this data set. The biggest transaction had a monetary value of around 25,691 dollars." 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "#fraud vs. normal transactions \n", 126 | "counts = df.Class.value_counts()\n", 127 | "normal = counts[0]\n", 128 | "fraudulent = counts[1]\n", 129 | "perc_normal = (normal/(normal+fraudulent))*100\n", 130 | "perc_fraudulent = (fraudulent/(normal+fraudulent))*100\n", 131 | "print('There were {} non-fraudulent transactions ({:.3f}%) and {} fraudulent transactions ({:.3f}%).'.format(normal, perc_normal, fraudulent, perc_fraudulent))" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "plt.figure(figsize=(8,6))\n", 141 | "sns.barplot(x=counts.index, y=counts)\n", 142 | "plt.title('Count of Non-Fraudulent vs. Fraudulent Transactions')\n", 143 | "plt.ylabel('Count')\n", 144 | "plt.xlabel('Class (0:Non-Fraudulent, 1:Fraudulent)')" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": null, 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "corr = df.corr()\n", 154 | "corr" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "#heatmap\n", 164 | "corr = df.corr()\n", 165 | "plt.figure(figsize=(12,10))\n", 166 | "heat = sns.heatmap(data=corr)\n", 167 | "plt.title('Heatmap of Correlation')" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "#skewness\n", 177 | "skew_ = df.skew()\n", 178 | "skew_" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "**Scaling Amount and Time**" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": null, 191 | "metadata": {}, 192 | "outputs": [], 193 | "source": [ 194 | "from sklearn.preprocessing import StandardScaler\n", 195 | "scaler = StandardScaler()\n", 196 | "scaler2 = StandardScaler()\n", 197 | "#scaling time\n", 198 | "scaled_time = scaler.fit_transform(df[['Time']])\n", 199 | "flat_list1 = [item for sublist in scaled_time.tolist() for item in sublist]\n", 200 | "scaled_time = pd.Series(flat_list1)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": null, 206 | "metadata": {}, 207 | "outputs": [], 208 | "source": [ 209 | "#scaling the amount column\n", 210 | "scaled_amount = scaler2.fit_transform(df[['Amount']])\n", 211 | "flat_list2 = [item for sublist in scaled_amount.tolist() for item in sublist]\n", 212 | "scaled_amount = pd.Series(flat_list2)" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": null, 218 | "metadata": {}, 219 | "outputs": [], 220 | "source": [ 221 | "#concatenating newly created columns w original df\n", 222 | "df = pd.concat([df, scaled_amount.rename('scaled_amount'), scaled_time.rename('scaled_time')], axis=1)\n", 223 | "df.sample(5)" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "metadata": {}, 230 | "outputs": [], 231 | "source": [ 232 | "#dropping old amount and time columns\n", 233 | "df.drop(['Amount', 'Time'], axis=1, inplace=True)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "**Splitting Data into Train and Test**" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": null, 246 | "metadata": {}, 247 | "outputs": [], 248 | "source": [ 249 | "#manual train test split using numpy's random.rand\n", 250 | "mask = np.random.rand(len(df)) < 0.9\n", 251 | "train = df[mask]\n", 252 | "test = df[~mask]\n", 253 | "print('The Train Shape: {}\\nThe Test Shape: {}'.format(train.shape, test.shape))" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [ 262 | "train.reset_index(drop=True, inplace=True)\n", 263 | "test.reset_index(drop=True, inplace=True)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "**Creating a subsample data set with balanced class distributions**" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [ 279 | "#how many random samples from normal transactions do we need?\n", 280 | "amount_of_frauds = train.Class.value_counts()[1]\n", 281 | "print('There are {} fraudulent transactions in the train data.'.format(amount_of_frauds))" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": null, 287 | "metadata": {}, 288 | "outputs": [], 289 | "source": [ 290 | "#randomly selecting 442 random non-fraudulent transactions\n", 291 | "non_fraud = train[train['Class'] == 0]\n", 292 | "fraud = train[train['Class'] == 1]" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "metadata": {}, 299 | "outputs": [], 300 | "source": [ 301 | "selected = non_fraud.sample(no_of_frauds)\n", 302 | "selected.head()" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": null, 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [ 311 | "#concatenating both into a subsample data set with equal class distribution\n", 312 | "selected.reset_index(drop=True, inplace=True)\n", 313 | "fraud.reset_index(drop=True, inplace=True)" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": null, 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [ 322 | "subsample = pd.concat([selected, fraud])\n", 323 | "len(subsample)" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": null, 329 | "metadata": {}, 330 | "outputs": [], 331 | "source": [ 332 | "#shuffling our data set\n", 333 | "subsample = subsample.sample(frac=1).reset_index(drop=True)\n", 334 | "subsample.head(10)" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": null, 340 | "metadata": {}, 341 | "outputs": [], 342 | "source": [ 343 | "new_counts = subsample.Class.value_counts()\n", 344 | "plt.figure(figsize=(8,6))\n", 345 | "sns.barplot(x=new_counts.index, y=new_counts)\n", 346 | "plt.title('Count of Fraudulent vs. Non-Fraudulent Transactions In Sub-Sample')\n", 347 | "plt.ylabel('Count')\n", 348 | "plt.xlabel('Class (0:Non-Fraudulent, 1:Fraudulent)')" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": null, 354 | "metadata": {}, 355 | "outputs": [], 356 | "source": [ 357 | "#taking a look at correlations once more\n", 358 | "corr = subsample.corr()\n", 359 | "corr = corr[['Class']]\n", 360 | "corr" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": null, 366 | "metadata": {}, 367 | "outputs": [], 368 | "source": [ 369 | "#negative correlations smaller than -0.5\n", 370 | "corr[corr.Class < -0.5]" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": null, 376 | "metadata": {}, 377 | "outputs": [], 378 | "source": [ 379 | "#positive correlations greater than 0.5\n", 380 | "corr[corr.Class > 0.5]" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "metadata": {}, 387 | "outputs": [], 388 | "source": [ 389 | "#visualizing the features w high negative correlation\n", 390 | "f, axes = plt.subplots(nrows=2, ncols=4, figsize=(26,16))\n", 391 | "\n", 392 | "f.suptitle('Features With High Negative Correlation', size=35)\n", 393 | "sns.boxplot(x=\"Class\", y=\"V3\", data=subsample, ax=axes[0,0])\n", 394 | "sns.boxplot(x=\"Class\", y=\"V9\", data=subsample, ax=axes[0,1])\n", 395 | "sns.boxplot(x=\"Class\", y=\"V10\", data=subsample, ax=axes[0,2])\n", 396 | "sns.boxplot(x=\"Class\", y=\"V12\", data=subsample, ax=axes[0,3])\n", 397 | "sns.boxplot(x=\"Class\", y=\"V14\", data=subsample, ax=axes[1,0])\n", 398 | "sns.boxplot(x=\"Class\", y=\"V16\", data=subsample, ax=axes[1,1])\n", 399 | "sns.boxplot(x=\"Class\", y=\"V17\", data=subsample, ax=axes[1,2])\n", 400 | "f.delaxes(axes[1,3])" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": null, 406 | "metadata": {}, 407 | "outputs": [], 408 | "source": [ 409 | "#visualizing the features w high positive correlation\n", 410 | "f, axes = plt.subplots(nrows=1, ncols=2, figsize=(18,9))\n", 411 | "\n", 412 | "f.suptitle('Features With High Positive Correlation', size=20)\n", 413 | "sns.boxplot(x=\"Class\", y=\"V4\", data=subsample, ax=axes[0])\n", 414 | "sns.boxplot(x=\"Class\", y=\"V11\", data=subsample, ax=axes[1])" 415 | ] 416 | }, 417 | { 418 | "cell_type": "markdown", 419 | "metadata": {}, 420 | "source": [ 421 | "**Extreme Outlier Removal**" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": null, 427 | "metadata": {}, 428 | "outputs": [], 429 | "source": [ 430 | "#Only removing extreme outliers\n", 431 | "Q1 = subsample.quantile(0.25)\n", 432 | "Q3 = subsample.quantile(0.75)\n", 433 | "IQR = Q3 - Q1\n", 434 | "\n", 435 | "df2 = subsample[~((subsample < (Q1 - 2.5 * IQR)) |(subsample > (Q3 + 2.5 * IQR))).any(axis=1)]" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": null, 441 | "metadata": {}, 442 | "outputs": [], 443 | "source": [ 444 | "transactions_after = len(df2)\n", 445 | "transactions_before = len(subsample)\n", 446 | "transactions_difference = len(subsample) - len(df2)\n", 447 | "print('We reduced our data size from {} transactions by {} transactions to {} transactions.'.format(transactions_before, transactions_difference, transactions_after))" 448 | ] 449 | }, 450 | { 451 | "cell_type": "markdown", 452 | "metadata": {}, 453 | "source": [ 454 | "**Dimensionality Reduction**" 455 | ] 456 | }, 457 | { 458 | "cell_type": "code", 459 | "execution_count": null, 460 | "metadata": {}, 461 | "outputs": [], 462 | "source": [ 463 | "from sklearn.manifold import TSNE\n", 464 | "\n", 465 | "X = df2.drop('Class', axis=1)\n", 466 | "y = df2['Class']" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": null, 472 | "metadata": {}, 473 | "outputs": [], 474 | "source": [ 475 | "#t-SNE\n", 476 | "X_reduced_tsne = TSNE(n_components=2, random_state=42).fit_transform(X.values)" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": null, 482 | "metadata": {}, 483 | "outputs": [], 484 | "source": [ 485 | "# t-SNE scatter plot\n", 486 | "import matplotlib.patches as mpatches\n", 487 | "\n", 488 | "f, ax = plt.subplots(figsize=(24,16))\n", 489 | "\n", 490 | "\n", 491 | "blue_patch = mpatches.Patch(color='#0A0AFF', label='No Fraud')\n", 492 | "red_patch = mpatches.Patch(color='#AF0000', label='Fraud')\n", 493 | "\n", 494 | "ax.scatter(X_reduced_tsne[:,0], X_reduced_tsne[:,1], c=(y == 0), cmap='coolwarm', label='No Fraud', linewidths=2)\n", 495 | "ax.scatter(X_reduced_tsne[:,0], X_reduced_tsne[:,1], c=(y == 1), cmap='coolwarm', label='Fraud', linewidths=2)\n", 496 | "ax.set_title('t-SNE', fontsize=14)\n", 497 | "\n", 498 | "ax.grid(True)\n", 499 | "\n", 500 | "ax.legend(handles=[blue_patch, red_patch])\n" 501 | ] 502 | }, 503 | { 504 | "cell_type": "markdown", 505 | "metadata": {}, 506 | "source": [ 507 | "**Classification Algorithms**" 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": null, 513 | "metadata": {}, 514 | "outputs": [], 515 | "source": [ 516 | "def warn(*args, **kwargs):\n", 517 | " pass\n", 518 | "import warnings\n", 519 | "warnings.warn = warn" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": null, 525 | "metadata": {}, 526 | "outputs": [], 527 | "source": [ 528 | "# train test split\n", 529 | "from sklearn.model_selection import train_test_split\n", 530 | "\n", 531 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": null, 537 | "metadata": {}, 538 | "outputs": [], 539 | "source": [ 540 | "X_train = X_train.values\n", 541 | "X_validation = X_test.values\n", 542 | "y_train = y_train.values\n", 543 | "y_validation = y_test.values" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": null, 549 | "metadata": {}, 550 | "outputs": [], 551 | "source": [ 552 | "print('X_shapes:\\n', 'X_train:', 'X_validation:\\n', X_train.shape, X_validation.shape, '\\n')\n", 553 | "print('Y_shapes:\\n', 'Y_train:', 'Y_validation:\\n', y_train.shape, y_validation.shape)" 554 | ] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": null, 559 | "metadata": {}, 560 | "outputs": [], 561 | "source": [ 562 | "from sklearn.model_selection import KFold\n", 563 | "from sklearn.model_selection import cross_val_score\n", 564 | "from sklearn.metrics import roc_auc_score\n", 565 | "from sklearn.metrics import classification_report\n", 566 | "from sklearn.metrics import confusion_matrix\n", 567 | "from sklearn.linear_model import LogisticRegression\n", 568 | "from sklearn.discriminant_analysis import LinearDiscriminantAnalysis\n", 569 | "from sklearn.neighbors import KNeighborsClassifier\n", 570 | "from sklearn.tree import DecisionTreeClassifier\n", 571 | "from sklearn.svm import SVC\n", 572 | "from xgboost import XGBClassifier\n", 573 | "from sklearn.ensemble import RandomForestClassifier\n" 574 | ] 575 | }, 576 | { 577 | "cell_type": "code", 578 | "execution_count": null, 579 | "metadata": {}, 580 | "outputs": [], 581 | "source": [ 582 | "##Spot-Checking Algorithms\n", 583 | "\n", 584 | "models = []\n", 585 | "\n", 586 | "models.append(('LR', LogisticRegression()))\n", 587 | "models.append(('LDA', LinearDiscriminantAnalysis()))\n", 588 | "models.append(('KNN', KNeighborsClassifier()))\n", 589 | "models.append(('CART', DecisionTreeClassifier()))\n", 590 | "models.append(('SVM', SVC()))\n", 591 | "models.append(('XGB', XGBClassifier()))\n", 592 | "models.append(('RF', RandomForestClassifier()))\n", 593 | "\n", 594 | "#testing models\n", 595 | "\n", 596 | "results = []\n", 597 | "names = []\n", 598 | "\n", 599 | "for name, model in models:\n", 600 | " kfold = KFold(n_splits=10, random_state=42)\n", 601 | " cv_results = cross_val_score(model, X_train, y_train, cv=kfold, scoring='roc_auc')\n", 602 | " results.append(cv_results)\n", 603 | " names.append(name)\n", 604 | " msg = '%s: %f (%f)' % (name, cv_results.mean(), cv_results.std())\n", 605 | " print(msg)" 606 | ] 607 | }, 608 | { 609 | "cell_type": "code", 610 | "execution_count": null, 611 | "metadata": {}, 612 | "outputs": [], 613 | "source": [ 614 | "#Compare Algorithms\n", 615 | "\n", 616 | "fig = plt.figure(figsize=(12,10))\n", 617 | "plt.title('Comparison of Classification Algorithms')\n", 618 | "plt.xlabel('Algorithm')\n", 619 | "plt.ylabel('ROC-AUC Score')\n", 620 | "plt.boxplot(results)\n", 621 | "ax = fig.add_subplot(111)\n", 622 | "ax.set_xticklabels(names)\n", 623 | "plt.show()" 624 | ] 625 | }, 626 | { 627 | "cell_type": "code", 628 | "execution_count": null, 629 | "metadata": {}, 630 | "outputs": [], 631 | "source": [ 632 | "#visualizing RF\n", 633 | "model = RandomForestClassifier(n_estimators=10)\n", 634 | "\n", 635 | "# Train\n", 636 | "model.fit(X_train, y_train)\n", 637 | "# Extract single tree\n", 638 | "estimator = model.estimators_[5]\n", 639 | "\n", 640 | "from sklearn.tree import export_graphviz\n", 641 | "# Export as dot file\n", 642 | "export_graphviz(estimator, out_file='tree.dot', \n", 643 | " feature_names = X.columns.tolist(),\n", 644 | " class_names = ['0',' 1'],\n", 645 | " rounded = True, proportion = False, \n", 646 | " precision = 2, filled = True)\n", 647 | "\n", 648 | "# Convert to png using system command (requires Graphviz)\n", 649 | "from subprocess import call\n", 650 | "call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])\n", 651 | "\n", 652 | "# Display in jupyter notebook\n", 653 | "from IPython.display import Image\n", 654 | "Image(filename = 'tree.png')" 655 | ] 656 | }, 657 | { 658 | "cell_type": "code", 659 | "execution_count": null, 660 | "metadata": {}, 661 | "outputs": [], 662 | "source": [] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": null, 667 | "metadata": {}, 668 | "outputs": [], 669 | "source": [] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": null, 674 | "metadata": {}, 675 | "outputs": [], 676 | "source": [] 677 | }, 678 | { 679 | "cell_type": "code", 680 | "execution_count": null, 681 | "metadata": {}, 682 | "outputs": [], 683 | "source": [] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": null, 688 | "metadata": {}, 689 | "outputs": [], 690 | "source": [] 691 | }, 692 | { 693 | "cell_type": "code", 694 | "execution_count": null, 695 | "metadata": {}, 696 | "outputs": [], 697 | "source": [] 698 | }, 699 | { 700 | "cell_type": "code", 701 | "execution_count": null, 702 | "metadata": {}, 703 | "outputs": [], 704 | "source": [] 705 | } 706 | ], 707 | "metadata": { 708 | "kernelspec": { 709 | "display_name": "Python 3", 710 | "language": "python", 711 | "name": "python3" 712 | }, 713 | "language_info": { 714 | "codemirror_mode": { 715 | "name": "ipython", 716 | "version": 3 717 | }, 718 | "file_extension": ".py", 719 | "mimetype": "text/x-python", 720 | "name": "python", 721 | "nbconvert_exporter": "python", 722 | "pygments_lexer": "ipython3", 723 | "version": "3.7.1" 724 | } 725 | }, 726 | "nbformat": 4, 727 | "nbformat_minor": 2 728 | } 729 | -------------------------------------------------------------------------------- /Credit_card_fraud/GAN_171103.py: -------------------------------------------------------------------------------- 1 | 2 | import numpy as np 3 | import pandas as pd 4 | import matplotlib.pyplot as plt 5 | plt.style.use('ggplot') 6 | 7 | import xgboost as xgb 8 | 9 | import pickle 10 | import gc 11 | import os 12 | import sys 13 | 14 | from keras import applications 15 | from keras import backend as K 16 | from keras import layers 17 | from keras import models 18 | from keras import optimizers 19 | import tensorflow as tf 20 | 21 | 22 | def BaseMetrics(y_pred,y_true): 23 | TP = np.sum( (y_pred == 1) & (y_true == 1) ) 24 | TN = np.sum( (y_pred == 0) & (y_true == 0) ) 25 | FP = np.sum( (y_pred == 1) & (y_true == 0) ) 26 | FN = np.sum( (y_pred == 0) & (y_true == 1) ) 27 | return TP, TN, FP, FN 28 | 29 | 30 | 31 | def SimpleAccuracy(y_pred,y_true): 32 | TP, TN, FP, FN = BaseMetrics(y_pred,y_true) 33 | ACC = ( TP + TN ) / ( TP + TN + FP + FN ) 34 | return ACC 35 | 36 | def SimpleMetrics(y_pred,y_true): 37 | TP, TN, FP, FN = BaseMetrics(y_pred,y_true) 38 | ACC = ( TP + TN ) / ( TP + TN + FP + FN ) 39 | 40 | # Reporting 41 | from IPython.display import display 42 | print( 'Confusion Matrix') 43 | display( pd.DataFrame( [[TN,FP],[FN,TP]], columns=['Pred 0','Pred 1'], index=['True 0', 'True 1'] ) ) 44 | print( 'Accuracy : {}'.format( ACC )) 45 | 46 | 47 | def get_data_batch(train, batch_size, seed=0): 48 | # # for random sampling - some samples will have excessively low or high sampling, but easy to implement 49 | # np.random.seed(seed) 50 | # x = train.loc[ np.random.choice(train.index, batch_size) ].values 51 | 52 | # iterating through shuffled indices, so every sample gets covered evenly 53 | start_i = (batch_size * seed) % len(train) 54 | stop_i = start_i + batch_size 55 | shuffle_seed = (batch_size * seed) // len(train) 56 | np.random.seed(shuffle_seed) 57 | train_ix = np.random.choice( list(train.index), replace=False, size=len(train) ) # wasteful to shuffle every time 58 | train_ix = list(train_ix) + list(train_ix) # duplicate to cover ranges past the end of the set 59 | x = train.loc[ train_ix[ start_i: stop_i ] ].values 60 | 61 | return np.reshape(x, (batch_size, -1) ) 62 | 63 | 64 | 65 | def CheckAccuracy( x, g_z, data_cols, label_cols=[], seed=0, with_class=False, data_dim=2 ): 66 | 67 | # Slightly slower code to create dataframes to feed into the xgboost dmatrix formats 68 | 69 | 70 | dtrain = np.vstack( [ x[:int(len(x)/2)], g_z[:int(len(g_z)/2)] ] ) # Use half of each real and generated set for training 71 | dlabels = np.hstack( [ np.zeros(int(len(x)/2)), np.ones(int(len(g_z)/2)) ] ) # synthetic labels 72 | dtest = np.vstack( [ x[int(len(x)/2):], g_z[int(len(g_z)/2):] ] ) # Use the other half of each set for testing 73 | y_true = dlabels # Labels for test samples will be the same as the labels for training samples, assuming even batch sizes 74 | 75 | dtrain = xgb.DMatrix(dtrain, dlabels, feature_names=data_cols+label_cols) 76 | dtest = xgb.DMatrix(dtest, feature_names=data_cols+label_cols) 77 | 78 | xgb_params = { 79 | # 'tree_method': 'hist', # for faster evaluation 80 | 'max_depth': 4, # for faster evaluation 81 | 'objective': 'binary:logistic', 82 | 'random_state': 0, 83 | 'eval_metric': 'auc', # allows for balanced or unbalanced classes 84 | } 85 | xgb_test = xgb.train(xgb_params, dtrain, num_boost_round=10) # limit to ten rounds for faster evaluation 86 | 87 | y_pred = np.round(xgb_test.predict(dtest)) 88 | 89 | 90 | return SimpleAccuracy(y_pred, y_true) # assumes balanced real and generated datasets 91 | 92 | def PlotData( x, g_z, data_cols, label_cols=[], seed=0, with_class=False, data_dim=2, save=False, prefix='' ): 93 | 94 | real_samples = pd.DataFrame(x, columns=data_cols+label_cols) 95 | gen_samples = pd.DataFrame(g_z, columns=data_cols+label_cols) 96 | 97 | f, axarr = plt.subplots(1, 2, figsize=(6,2) ) 98 | if with_class: 99 | axarr[0].scatter( real_samples[data_cols[0]], real_samples[data_cols[1]], c=real_samples[label_cols[0]]/2 ) #, cmap='plasma' ) 100 | axarr[1].scatter( gen_samples[ data_cols[0]], gen_samples[ data_cols[1]], c=gen_samples[label_cols[0]]/2 ) #, cmap='plasma' ) 101 | 102 | 103 | else: 104 | axarr[0].scatter( real_samples[data_cols[0]], real_samples[data_cols[1]]) #, cmap='plasma' ) 105 | axarr[1].scatter( gen_samples[data_cols[0]], gen_samples[data_cols[1]]) #, cmap='plasma' ) 106 | axarr[0].set_title('real') 107 | axarr[1].set_title('generated') 108 | axarr[0].set_ylabel(data_cols[1]) # Only add y label to left plot 109 | for a in axarr: a.set_xlabel(data_cols[0]) # Add x label to both plots 110 | axarr[1].set_xlim(axarr[0].get_xlim()), axarr[1].set_ylim(axarr[0].get_ylim()) # Use axes ranges from real data for generated data 111 | 112 | if save: 113 | plt.save( prefix + '.xgb_check.png' ) 114 | 115 | plt.show() 116 | 117 | 118 | 119 | #### The Functions to define the layers of the networks used in the 'define_models' functions below 120 | 121 | def generator_network(x, data_dim, base_n_count): 122 | x = layers.Dense(base_n_count, activation='relu')(x) 123 | x = layers.Dense(base_n_count*2, activation='relu')(x) 124 | x = layers.Dense(base_n_count*4, activation='relu')(x) 125 | x = layers.Dense(data_dim)(x) 126 | return x 127 | 128 | def generator_network_w_label(x, labels, data_dim, label_dim, base_n_count): 129 | x = layers.concatenate([x,labels]) 130 | x = layers.Dense(base_n_count*1, activation='relu')(x) # 1 131 | x = layers.Dense(base_n_count*2, activation='relu')(x) # 2 132 | x = layers.Dense(base_n_count*4, activation='relu')(x) 133 | # x = layers.Dense(base_n_count*4, activation='relu')(x) # extra 134 | # x = layers.Dense(base_n_count*4, activation='relu')(x) # extra 135 | x = layers.Dense(data_dim)(x) 136 | x = layers.concatenate([x,labels]) 137 | return x 138 | 139 | def discriminator_network(x, data_dim, base_n_count): 140 | x = layers.Dense(base_n_count*4, activation='relu')(x) 141 | # x = layers.Dropout(0.1)(x) 142 | x = layers.Dense(base_n_count*2, activation='relu')(x) 143 | # x = layers.Dropout(0.1)(x) 144 | x = layers.Dense(base_n_count, activation='relu')(x) 145 | x = layers.Dense(1, activation='sigmoid')(x) 146 | # x = layers.Dense(1)(x) 147 | return x 148 | 149 | def critic_network(x, data_dim, base_n_count): 150 | x = layers.Dense(base_n_count*4, activation='relu')(x) 151 | # x = layers.Dropout(0.1)(x) 152 | x = layers.Dense(base_n_count*2, activation='relu')(x) # 2 153 | # x = layers.Dropout(0.1)(x) 154 | x = layers.Dense(base_n_count*1, activation='relu')(x) # 1 155 | # x = layers.Dense(base_n_count*4, activation='relu')(x) # extra 156 | # x = layers.Dense(base_n_count*4, activation='relu')(x) # extra 157 | # x = layers.Dense(1, activation='sigmoid')(x) 158 | x = layers.Dense(1)(x) 159 | return x 160 | 161 | 162 | 163 | #### The functions to define the keras network models 164 | 165 | def define_models_GAN(rand_dim, data_dim, base_n_count, type=None): 166 | generator_input_tensor = layers.Input(shape=(rand_dim, )) 167 | generated_image_tensor = generator_network(generator_input_tensor, data_dim, base_n_count) 168 | 169 | generated_or_real_image_tensor = layers.Input(shape=(data_dim,)) 170 | 171 | if type == 'Wasserstein': 172 | discriminator_output = critic_network(generated_or_real_image_tensor, data_dim, base_n_count) 173 | else: 174 | discriminator_output = discriminator_network(generated_or_real_image_tensor, data_dim, base_n_count) 175 | 176 | generator_model = models.Model(inputs=[generator_input_tensor], outputs=[generated_image_tensor], name='generator') 177 | discriminator_model = models.Model(inputs=[generated_or_real_image_tensor], 178 | outputs=[discriminator_output], 179 | name='discriminator') 180 | 181 | combined_output = discriminator_model(generator_model(generator_input_tensor)) 182 | combined_model = models.Model(inputs=[generator_input_tensor], outputs=[combined_output], name='combined') 183 | 184 | return generator_model, discriminator_model, combined_model 185 | 186 | def define_models_CGAN(rand_dim, data_dim, label_dim, base_n_count, type=None): 187 | generator_input_tensor = layers.Input(shape=(rand_dim, )) 188 | labels_tensor = layers.Input(shape=(label_dim,)) # updated for class 189 | generated_image_tensor = generator_network_w_label(generator_input_tensor, labels_tensor, data_dim, label_dim, base_n_count) # updated for class 190 | 191 | generated_or_real_image_tensor = layers.Input(shape=(data_dim + label_dim,)) # updated for class 192 | 193 | if type == 'Wasserstein': 194 | discriminator_output = critic_network(generated_or_real_image_tensor, data_dim + label_dim, base_n_count) # updated for class 195 | else: 196 | discriminator_output = discriminator_network(generated_or_real_image_tensor, data_dim + label_dim, base_n_count) # updated for class 197 | 198 | generator_model = models.Model(inputs=[generator_input_tensor, labels_tensor], outputs=[generated_image_tensor], name='generator') # updated for class 199 | discriminator_model = models.Model(inputs=[generated_or_real_image_tensor], 200 | outputs=[discriminator_output], 201 | name='discriminator') 202 | 203 | combined_output = discriminator_model(generator_model([generator_input_tensor, labels_tensor])) # updated for class 204 | combined_model = models.Model(inputs=[generator_input_tensor, labels_tensor], outputs=[combined_output], name='combined') # updated for class 205 | 206 | return generator_model, discriminator_model, combined_model 207 | 208 | 209 | 210 | #### The functions specific to the WGAN architecture 211 | #### Train discrimnator step is separated out to facilitate pre-training of the discriminator by itself 212 | 213 | def em_loss(y_coefficients, y_pred): 214 | # define earth mover distance (wasserstein loss) 215 | # literally the weighted average of the critic network output 216 | # this is defined separately so it can be fed as a loss function to the optimizer in the WGANs 217 | return tf.reduce_mean(tf.multiply(y_coefficients, y_pred)) 218 | 219 | def train_discriminator_step(model_components, seed=0): 220 | 221 | [ cache_prefix, with_class, starting_step, 222 | train, data_cols, data_dim, 223 | label_cols, label_dim, 224 | generator_model, discriminator_model, combined_model, 225 | rand_dim, nb_steps, batch_size, 226 | k_d, k_g, critic_pre_train_steps, log_interval, learning_rate, base_n_count, 227 | data_dir, generator_model_path, discriminator_model_path, 228 | 229 | sess, _z, _x, _labels, _g_z, epsilon, x_hat, gradients, _gradient_penalty, 230 | _disc_loss_generated, _disc_loss_real, _disc_loss, disc_optimizer, 231 | show, 232 | combined_loss, disc_loss_generated, disc_loss_real, xgb_losses 233 | ] = model_components 234 | 235 | if with_class: 236 | d_l_g, d_l_r, _ = sess.run([_disc_loss_generated, _disc_loss_real, disc_optimizer], feed_dict={ 237 | _z: np.random.normal(size=(batch_size, rand_dim)), 238 | _x: get_data_batch(train, batch_size, seed=seed), 239 | _labels: get_data_batch(train, batch_size, seed=seed)[:,-label_dim:], # .reshape(-1,label_dim), # updated for class 240 | epsilon: np.random.uniform(low=0.0, high=1.0, size=(batch_size, 1)) 241 | }) 242 | else: 243 | d_l_g, d_l_r, _ = sess.run([_disc_loss_generated, _disc_loss_real, disc_optimizer], feed_dict={ 244 | _z: np.random.normal(size=(batch_size, rand_dim)), 245 | _x: get_data_batch(train, batch_size, seed=seed), 246 | epsilon: np.random.uniform(low=0.0, high=1.0, size=(batch_size, 1)) 247 | }) 248 | return d_l_g, d_l_r 249 | 250 | def training_steps_WGAN(model_components): 251 | 252 | [ cache_prefix, with_class, starting_step, 253 | train, data_cols, data_dim, 254 | label_cols, label_dim, 255 | generator_model, discriminator_model, combined_model, 256 | rand_dim, nb_steps, batch_size, 257 | k_d, k_g, critic_pre_train_steps, log_interval, learning_rate, base_n_count, 258 | data_dir, generator_model_path, discriminator_model_path, 259 | 260 | sess, _z, _x, _labels, _g_z, epsilon, x_hat, gradients, _gradient_penalty, 261 | _disc_loss_generated, _disc_loss_real, _disc_loss, disc_optimizer, 262 | show, 263 | combined_loss, disc_loss_generated, disc_loss_real, xgb_losses 264 | ] = model_components 265 | 266 | for i in range(starting_step, starting_step+nb_steps): 267 | K.set_learning_phase(1) # 1 = train 268 | 269 | # train the discriminator 270 | for j in range(k_d): 271 | d_l_g, d_l_r = train_discriminator_step(model_components, seed=i+j) 272 | disc_loss_generated.append(d_l_g) 273 | disc_loss_real.append(d_l_r) 274 | 275 | # train the generator 276 | for j in range(k_g): 277 | np.random.seed(i+j) 278 | z = np.random.normal(size=(batch_size, rand_dim)) 279 | if with_class: 280 | labels = get_data_batch(train, batch_size, seed=i+j)[:,-label_dim:] # updated for class 281 | loss = combined_model.train_on_batch([z, labels], [-np.ones(batch_size)]) # updated for class 282 | else: 283 | loss = combined_model.train_on_batch(z, [-np.ones(batch_size)]) 284 | combined_loss.append(loss) 285 | 286 | # To determine xgb loss each step, after training generator and discriminator 287 | if not i % 10: # 2x faster than testing each step... 288 | K.set_learning_phase(0) # 0 = test 289 | test_size = 492 # test using all of the actual fraud data 290 | x = get_data_batch(train, test_size, seed=i) 291 | z = np.random.normal(size=(test_size, rand_dim)) 292 | if with_class: 293 | labels = x[:,-label_dim:] 294 | g_z = generator_model.predict([z, labels]) 295 | else: 296 | g_z = generator_model.predict(z) 297 | xgb_loss = CheckAccuracy( x, g_z, data_cols, label_cols, seed=0, with_class=with_class, data_dim=data_dim ) 298 | xgb_losses = np.append(xgb_losses, xgb_loss) 299 | 300 | if not i % log_interval: 301 | print('Step: {} of {}.'.format(i, starting_step + nb_steps)) 302 | 303 | 304 | if show: 305 | PlotData( x, g_z, data_cols, label_cols, seed=0, with_class=with_class, data_dim=data_dim, 306 | save=False, prefix= data_dir + cache_prefix + '_' + str(i) ) 307 | 308 | # save model checkpoints 309 | model_checkpoint_base_name = data_dir + cache_prefix + '_{}_model_weights_step_{}.h5' 310 | generator_model.save_weights(model_checkpoint_base_name.format('generator', i)) 311 | discriminator_model.save_weights(model_checkpoint_base_name.format('discriminator', i)) 312 | pickle.dump([combined_loss, disc_loss_generated, disc_loss_real, xgb_losses], 313 | open( data_dir + cache_prefix + '_losses_step_{}.pkl'.format(i) ,'wb')) 314 | 315 | return [combined_loss, disc_loss_generated, disc_loss_real, xgb_losses] 316 | 317 | def adversarial_training_WGAN(arguments, train, data_cols, label_cols=[], seed=0, starting_step=0): 318 | 319 | [rand_dim, nb_steps, batch_size, 320 | k_d, k_g, critic_pre_train_steps, log_interval, learning_rate, base_n_count, 321 | data_dir, generator_model_path, discriminator_model_path, loss_pickle_path, show ] = arguments 322 | 323 | np.random.seed(seed) # set random seed 324 | 325 | data_dim = len(data_cols) 326 | print('data_dim: ', data_dim) 327 | print('data_cols: ', data_cols) 328 | 329 | label_dim = 0 330 | with_class = False 331 | if len(label_cols) > 0: 332 | with_class = True 333 | label_dim = len(label_cols) 334 | print('label_dim: ', label_dim) 335 | print('label_cols: ', label_cols) 336 | 337 | # define network models 338 | 339 | K.set_learning_phase(1) # 1 = train 340 | 341 | if with_class: 342 | cache_prefix = 'WCGAN' 343 | generator_model, discriminator_model, combined_model = define_models_CGAN(rand_dim, data_dim, label_dim, base_n_count, type='Wasserstein') 344 | else: 345 | cache_prefix = 'WGAN' 346 | generator_model, discriminator_model, combined_model = define_models_GAN(rand_dim, data_dim, base_n_count, type='Wasserstein') 347 | 348 | 349 | # construct computation graph for calculating the gradient penalty (improved WGAN) and training the discriminator 350 | 351 | _z = tf.placeholder(tf.float32, shape=(batch_size, rand_dim)) 352 | 353 | _labels = None 354 | if with_class: 355 | _x = tf.placeholder(tf.float32, shape=(batch_size, data_dim + label_dim)) 356 | _labels = tf.placeholder(tf.float32, shape=(batch_size, label_dim)) # updated for class 357 | _g_z = generator_model(inputs=[_z, _labels]) # updated for class 358 | else: 359 | _x = tf.placeholder(tf.float32, shape=(batch_size, data_dim)) 360 | _g_z = generator_model(_z) 361 | 362 | epsilon = tf.placeholder(tf.float32, shape=(batch_size, 1)) 363 | 364 | x_hat = epsilon * _x + (1.0 - epsilon) * _g_z 365 | gradients = tf.gradients(discriminator_model(x_hat), [x_hat]) 366 | _gradient_penalty = 10.0 * tf.square(tf.norm(gradients[0], ord=2) - 1.0) 367 | 368 | # calculate discriminator's loss 369 | _disc_loss_generated = em_loss(tf.ones(batch_size), discriminator_model(_g_z)) 370 | _disc_loss_real = em_loss(tf.ones(batch_size), discriminator_model(_x)) 371 | _disc_loss = _disc_loss_generated - _disc_loss_real + _gradient_penalty 372 | 373 | # update f by taking an SGD step on mini-batch loss LD(f) 374 | disc_optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5, beta2=0.9).minimize( 375 | _disc_loss, var_list=discriminator_model.trainable_weights) 376 | 377 | sess = K.get_session() 378 | 379 | # compile models 380 | 381 | adam = optimizers.Adam(lr=learning_rate, beta_1=0.5, beta_2=0.9) 382 | 383 | discriminator_model.trainable = False 384 | combined_model.compile(optimizer=adam, loss=[em_loss]) 385 | 386 | combined_loss, disc_loss_generated, disc_loss_real, xgb_losses = [], [], [], [] 387 | 388 | model_components = [ cache_prefix, with_class, starting_step, 389 | train, data_cols, data_dim, 390 | label_cols, label_dim, 391 | generator_model, discriminator_model, combined_model, 392 | rand_dim, nb_steps, batch_size, 393 | k_d, k_g, critic_pre_train_steps, log_interval, learning_rate, base_n_count, 394 | data_dir, generator_model_path, discriminator_model_path, 395 | 396 | sess, _z, _x, _labels, _g_z, epsilon, x_hat, gradients, _gradient_penalty, 397 | _disc_loss_generated, _disc_loss_real, _disc_loss, disc_optimizer, 398 | show, 399 | combined_loss, disc_loss_generated, disc_loss_real, xgb_losses 400 | ] 401 | 402 | if show: 403 | print(generator_model.summary()) 404 | print(discriminator_model.summary()) 405 | print(combined_model.summary()) 406 | 407 | if loss_pickle_path: 408 | print('Loading loss pickles') 409 | [combined_loss, disc_loss_generated, disc_loss_real, xgb_losses] = pickle.load(open(loss_pickle_path,'rb')) 410 | if generator_model_path: 411 | print('Loading generator model') 412 | generator_model.load_weights(generator_model_path) #, by_name=True) 413 | if discriminator_model_path: 414 | print('Loading discriminator model') 415 | discriminator_model.load_weights(discriminator_model_path) #, by_name=True) 416 | else: 417 | print('pre-training the critic...') 418 | K.set_learning_phase(1) # 1 = train 419 | for i in range(critic_pre_train_steps): 420 | if i%20==0: 421 | print('Step: {} of {} critic pre-training.'.format(i, critic_pre_train_steps)) 422 | loss = train_discriminator_step(model_components, seed=i) 423 | print('Last batch of critic pre-training disc_loss: {}.'.format(loss)) 424 | 425 | model_components = [ cache_prefix, with_class, starting_step, 426 | train, data_cols, data_dim, 427 | label_cols, label_dim, 428 | generator_model, discriminator_model, combined_model, 429 | rand_dim, nb_steps, batch_size, 430 | k_d, k_g, critic_pre_train_steps, log_interval, learning_rate, base_n_count, 431 | data_dir, generator_model_path, discriminator_model_path, 432 | 433 | sess, _z, _x, _labels, _g_z, epsilon, x_hat, gradients, _gradient_penalty, 434 | _disc_loss_generated, _disc_loss_real, _disc_loss, disc_optimizer, 435 | show, 436 | combined_loss, disc_loss_generated, disc_loss_real, xgb_losses 437 | ] 438 | 439 | [combined_loss, disc_loss_generated, disc_loss_real, xgb_losses] = training_steps_WGAN(model_components) 440 | 441 | 442 | 443 | #### The functions specific to the vanilla GAN architecture 444 | 445 | def training_steps_GAN(model_components): 446 | 447 | [ cache_prefix, with_class, starting_step, 448 | train, data_cols, data_dim, 449 | label_cols, label_dim, 450 | generator_model, discriminator_model, combined_model, 451 | rand_dim, nb_steps, batch_size, 452 | k_d, k_g, critic_pre_train_steps, log_interval, learning_rate, base_n_count, 453 | data_dir, generator_model_path, discriminator_model_path, show, 454 | combined_loss, disc_loss_generated, disc_loss_real, xgb_losses ] = model_components 455 | 456 | for i in range(starting_step, starting_step+nb_steps): 457 | K.set_learning_phase(1) # 1 = train 458 | 459 | # train the discriminator 460 | for j in range(k_d): 461 | np.random.seed(i+j) 462 | z = np.random.normal(size=(batch_size, rand_dim)) 463 | x = get_data_batch(train, batch_size, seed=i+j) 464 | 465 | if with_class: 466 | labels = x[:,-label_dim:] 467 | g_z = generator_model.predict([z, labels]) 468 | else: 469 | g_z = generator_model.predict(z) 470 | # x = np.vstack([x,g_z]) # code to train the discriminator on real and generated data at the same time, but you have to run network again for separate losses 471 | # classes = np.hstack([np.zeros(batch_size),np.ones(batch_size)]) 472 | # d_l_r = discriminator_model.train_on_batch(x, classes) 473 | 474 | d_l_r = discriminator_model.train_on_batch(x, np.random.uniform(low=0.999, high=1.0, size=batch_size)) # 0.7, 1.2 # GANs need noise to prevent loss going to zero 475 | d_l_g = discriminator_model.train_on_batch(g_z, np.random.uniform(low=0.0, high=0.001, size=batch_size)) # 0.0, 0.3 # GANs need noise to prevent loss going to zero 476 | # d_l_r = discriminator_model.train_on_batch(x, np.ones(batch_size)) # without noise 477 | # d_l_g = discriminator_model.train_on_batch(g_z, np.zeros(batch_size)) # without noise 478 | disc_loss_real.append(d_l_r) 479 | disc_loss_generated.append(d_l_g) 480 | 481 | # train the generator 482 | for j in range(k_g): 483 | np.random.seed(i+j) 484 | z = np.random.normal(size=(batch_size, rand_dim)) 485 | if with_class: 486 | # loss = combined_model.train_on_batch([z, labels], np.ones(batch_size)) # without noise 487 | loss = combined_model.train_on_batch([z, labels], np.random.uniform(low=0.999, high=1.0, size=batch_size)) # 0.7, 1.2 # GANs need noise to prevent loss going to zero 488 | else: 489 | # loss = combined_model.train_on_batch(z, np.ones(batch_size)) # without noise 490 | loss = combined_model.train_on_batch(z, np.random.uniform(low=0.999, high=1.0, size=batch_size)) # 0.7, 1.2 # GANs need noise to prevent loss going to zero 491 | combined_loss.append(loss) 492 | 493 | # Determine xgb loss each step, after training generator and discriminator 494 | if not i % 10: # 2x faster than testing each step... 495 | K.set_learning_phase(0) # 0 = test 496 | test_size = 492 # test using all of the actual fraud data 497 | x = get_data_batch(train, test_size, seed=i) 498 | z = np.random.normal(size=(test_size, rand_dim)) 499 | if with_class: 500 | labels = x[:,-label_dim:] 501 | g_z = generator_model.predict([z, labels]) 502 | else: 503 | g_z = generator_model.predict(z) 504 | xgb_loss = CheckAccuracy( x, g_z, data_cols, label_cols, seed=0, with_class=with_class, data_dim=data_dim ) 505 | xgb_losses = np.append(xgb_losses, xgb_loss) 506 | 507 | # Saving weights and plotting images 508 | if not i % log_interval: 509 | print('Step: {} of {}.'.format(i, starting_step + nb_steps)) 510 | K.set_learning_phase(0) # 0 = test 511 | 512 | # loss summaries 513 | print( 'Losses: G, D Gen, D Real, Xgb: {:.4f}, {:.4f}, {:.4f}, {:.4f}'.format(combined_loss[-1], disc_loss_generated[-1], disc_loss_real[-1], xgb_losses[-1]) ) 514 | print( 'D Real - D Gen: {:.4f}'.format(disc_loss_real[-1]-disc_loss_generated[-1]) ) 515 | # print('Generator model loss: {}.'.format(combined_loss[-1])) 516 | # print('Discriminator model loss gen: {}.'.format(disc_loss_generated[-1])) 517 | # print('Discriminator model loss real: {}.'.format(disc_loss_real[-1])) 518 | # print('xgboost accuracy: {}'.format(xgb_losses[-1]) ) 519 | 520 | if show: 521 | PlotData( x, g_z, data_cols, label_cols, seed=0, with_class=with_class, data_dim=data_dim, 522 | save=False, prefix= data_dir + cache_prefix + '_' + str(i) ) 523 | 524 | # save model checkpoints 525 | model_checkpoint_base_name = data_dir + cache_prefix + '_{}_model_weights_step_{}.h5' 526 | generator_model.save_weights(model_checkpoint_base_name.format('generator', i)) 527 | discriminator_model.save_weights(model_checkpoint_base_name.format('discriminator', i)) 528 | pickle.dump([combined_loss, disc_loss_generated, disc_loss_real, xgb_losses], 529 | open( data_dir + cache_prefix + '_losses_step_{}.pkl'.format(i) ,'wb')) 530 | 531 | return [combined_loss, disc_loss_generated, disc_loss_real, xgb_losses] 532 | 533 | def adversarial_training_GAN(arguments, train, data_cols, label_cols=[], seed=0, starting_step=0): 534 | 535 | [rand_dim, nb_steps, batch_size, 536 | k_d, k_g, critic_pre_train_steps, log_interval, learning_rate, base_n_count, 537 | data_dir, generator_model_path, discriminator_model_path, loss_pickle_path, show ] = arguments 538 | 539 | np.random.seed(seed) # set random seed 540 | 541 | data_dim = len(data_cols) 542 | print('data_dim: ', data_dim) 543 | print('data_cols: ', data_cols) 544 | 545 | label_dim = 0 546 | with_class = False 547 | if len(label_cols) > 0: 548 | with_class = True 549 | label_dim = len(label_cols) 550 | print('label_dim: ', label_dim) 551 | print('label_cols: ', label_cols) 552 | 553 | # define network models 554 | 555 | K.set_learning_phase(1) # 1 = train 556 | 557 | if with_class: 558 | cache_prefix = 'CGAN' 559 | generator_model, discriminator_model, combined_model = define_models_CGAN(rand_dim, data_dim, label_dim, base_n_count) 560 | else: 561 | cache_prefix = 'GAN' 562 | generator_model, discriminator_model, combined_model = define_models_GAN(rand_dim, data_dim, base_n_count) 563 | 564 | # compile models 565 | 566 | adam = optimizers.Adam(lr=learning_rate, beta_1=0.5, beta_2=0.9) 567 | 568 | generator_model.compile(optimizer=adam, loss='binary_crossentropy') 569 | discriminator_model.compile(optimizer=adam, loss='binary_crossentropy') 570 | discriminator_model.trainable = False 571 | combined_model.compile(optimizer=adam, loss='binary_crossentropy') 572 | 573 | if show: 574 | print(generator_model.summary()) 575 | print(discriminator_model.summary()) 576 | print(combined_model.summary()) 577 | 578 | combined_loss, disc_loss_generated, disc_loss_real, xgb_losses = [], [], [], [] 579 | 580 | if loss_pickle_path: 581 | print('Loading loss pickles') 582 | [combined_loss, disc_loss_generated, disc_loss_real, xgb_losses] = pickle.load(open(loss_pickle_path,'rb')) 583 | if generator_model_path: 584 | print('Loading generator model') 585 | generator_model.load_weights(generator_model_path, by_name=True) 586 | if discriminator_model_path: 587 | print('Loading discriminator model') 588 | discriminator_model.load_weights(discriminator_model_path, by_name=True) 589 | 590 | model_components = [ cache_prefix, with_class, starting_step, 591 | train, data_cols, data_dim, 592 | label_cols, label_dim, 593 | generator_model, discriminator_model, combined_model, 594 | rand_dim, nb_steps, batch_size, 595 | k_d, k_g, critic_pre_train_steps, log_interval, learning_rate, base_n_count, 596 | data_dir, generator_model_path, discriminator_model_path, show, 597 | combined_loss, disc_loss_generated, disc_loss_real, xgb_losses ] 598 | 599 | [combined_loss, disc_loss_generated, disc_loss_real, xgb_losses] = training_steps_GAN(model_components) 600 | 601 | 602 | 603 | #### Functions specific to the DRAGAN architecture 604 | #### Note the DRAGAN is implemented in tensorflow without Keras libraries 605 | #### https://github.com/kodalinaveen3/DRAGAN 606 | 607 | def sample_z(m, n): # updated to normal distribution 608 | # return np.random.uniform(-1., 1., size=[m, n]) 609 | return np.random.normal(size=[m, n]) 610 | 611 | def xavier_init(size): # updated to uniform distribution using standard xavier formulation 612 | # in_dim = size[0] 613 | # xavier_stddev = 1. / tf.sqrt(in_dim / 2.) 614 | # return tf.random_normal(shape=size, stddev=xavier_stddev, seed=global_seed) 615 | xavier_range = tf.sqrt( 6 / ( size[0] + size[1] ) ) 616 | return tf.random_uniform(shape=size, minval=-xavier_range, maxval=xavier_range) 617 | 618 | def get_perturbed_batch(minibatch): 619 | return minibatch + 0.5 * minibatch.std() * np.random.random(minibatch.shape) 620 | 621 | 622 | def G(z, G_W, G_b): # The Generator Network 623 | # for i in range(len(G_layer_dims)-2): 624 | for i in range(len(G_W)-1): 625 | z = tf.nn.relu(tf.matmul(z, G_W[i]) + G_b[i]) 626 | # print(i,G_W[i],z) 627 | return tf.matmul(z, G_W[-1]) + G_b[-1] 628 | 629 | def D(x, D_W, D_b): # The Discriminator Network 630 | # for i in range(len(D_layer_dims)-2): 631 | for i in range(len(D_W)-1): 632 | x = tf.nn.relu(tf.matmul(x, D_W[i]) + D_b[i]) 633 | return tf.nn.sigmoid(tf.matmul(x, D_W[-1]) + D_b[-1]) 634 | 635 | 636 | def define_DRAGAN_network( X_dim=2, h_dim=128, z_dim=2, lambda0=10, learning_rate=1e-4, mb_size=128, seed=0 ): 637 | 638 | X = tf.placeholder(tf.float32, shape=[None, X_dim], name='X' ) 639 | X_p = tf.placeholder(tf.float32, shape=[None, X_dim], name='X_p' ) 640 | z = tf.placeholder(tf.float32, shape=[None, z_dim], name='z' ) 641 | 642 | D_layer_dims = [X_dim, h_dim*4, h_dim*2, h_dim, 1 ] 643 | D_W, D_b = [], [] 644 | for i in range(len(D_layer_dims)-1): 645 | D_W.append( tf.Variable( xavier_init([D_layer_dims[i], D_layer_dims[i+1]] ), name='D_W'+str(i) ) ) 646 | # D_W.append( tf.Variable( initializer=tf.contrib.layers.xavier_initializer(seed=global_seed) ) # working towards using tf's own xavier initializer 647 | D_b.append( tf.Variable( tf.zeros(shape=[D_layer_dims[i+1]]), name='D_b'+str(i) ) ) 648 | theta_D = D_W + D_b 649 | 650 | G_layer_dims = [z_dim, h_dim, h_dim*2, h_dim*4, X_dim ] 651 | G_W, G_b = [], [] 652 | for i in range(len(G_layer_dims)-1): 653 | G_W.append( tf.Variable( xavier_init([G_layer_dims[i], G_layer_dims[i+1]] ), name='G_W'+str(i) ) ) 654 | G_b.append( tf.Variable( tf.zeros(shape=[G_layer_dims[i+1]]), name='g_b'+str(i) ) ) 655 | theta_G = G_W + G_b 656 | # print( theta_D + theta_G ) 657 | 658 | G_sample = G(z, G_W, G_b) 659 | D_real = D(X, D_W, D_b) 660 | D_fake = D(G_sample, D_W, D_b) 661 | D_real_perturbed = D(X_p, D_W, D_b) 662 | 663 | # D_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_real, labels=tf.ones_like(D_real))) 664 | # D_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_fake, labels=tf.zeros_like(D_fake))) 665 | # disc_cost = D_loss_real + D_loss_fake 666 | # gen_cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_fake, labels=tf.ones_like(D_fake))) 667 | D_loss_real = tf.reduce_mean(tf.log( D_real )) 668 | D_loss_fake = tf.reduce_mean(tf.log( 1 - D_fake )) 669 | disc_cost = - D_loss_real - D_loss_fake 670 | gen_cost = D_loss_fake 671 | 672 | #Gradient penalty 673 | alpha = tf.random_uniform( 674 | shape=[mb_size,1], 675 | minval=0., 676 | maxval=1.) # do not set seed 677 | 678 | differences = X_p - X 679 | interpolates = X + (alpha*differences) 680 | gradients = tf.gradients(D(interpolates, D_W, D_b), [interpolates])[0] 681 | # slopes = tf.sqrt(tf.reduce_sum(tf.square(gradients), reduction_indices=[1])) 682 | # gradient_penalty = tf.reduce_mean((slopes-1.)**2) 683 | gradient_penalty = tf.square(tf.norm(gradients, ord=2) - 1.0 ) # corrected? 684 | 685 | disc_cost += lambda0 * gradient_penalty / mb_size # corrected? 686 | 687 | G_solver = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5, beta2=0.9).minimize(gen_cost, var_list=theta_G) 688 | D_solver = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5, beta2=0.9).minimize(disc_cost, var_list=theta_D) 689 | 690 | return [ D_solver, disc_cost, D_loss_real, D_loss_fake, 691 | X, X_p, z, 692 | G_solver, gen_cost, G_sample ] 693 | 694 | 695 | 696 | 697 | # End of function list --------------------------------------------------------------------------------