Created
April 16, 2020 18:54
-
-
Save romeokienzler/f7df5f7f7c3c22458bef8f38f27d3c23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "!pip install tensorflow==2.2.0rc1" | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "import tensorflow as tf\nimport pandas as pd\ntf.__version__" | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "# The code was removed by Watson Studio for sharing." | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "import types\nimport pandas as pd\nfrom botocore.client import Config\nimport ibm_boto3\n\ndef __iter__(self): return 0\n\n\nclient = ibm_boto3.client(service_name='s3',\n ibm_api_key_id=credentials_1['IBM_API_KEY_ID'],\n ibm_auth_endpoint=credentials_1['IBM_AUTH_ENDPOINT'],\n config=Config(signature_version='oauth'),\n endpoint_url=credentials_1['ENDPOINT'])\nbucket=credentials_1['BUCKET']" | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "client.download_file(Bucket=bucket,Key='cases_features.csv',Filename='cases_features.csv')" | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "df = pd.read_csv('cases_features.csv')\ndf = df[df['countriesAndTerritories']=='United_States_of_America']\nwindow_size = 10" | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "import tensorflow as tf\nimport numpy as np\n\nnp.random.seed(seed=42)\n\[email protected]\ndef model(x, beta, gamma):\n return x*(gamma*((beta/gamma)-1))\n \n\n\na = tf.Variable(np.random.randn()*10, trainable=True, dtype=tf.float64)\nb = tf.Variable(np.random.randn()*10, trainable=True, dtype=tf.float64)\nc = tf.Variable(np.random.randn()*10, trainable=True, dtype=tf.float64)\nd = tf.Variable(np.random.randn()*10, trainable=True, dtype=tf.float64)\n\nx = tf.constant(df['cases_'+str(window_size)+'_day_average'], dtype=tf.float64)\ny = tf.constant(df.cases, dtype=tf.float64)\n\n\n\nvariables = [a,b]\noptimizer = tf.optimizers.Adam(0.01)\n\nloss_object = tf.keras.losses.MeanAbsoluteError()\n\nlosses = []\nepochs = 10000\n\nwith tf.GradientTape(persistent=True) as tape:\n for i in range(epochs):\n y_pred = model(x,a,b)\n loss = loss_object(y,y_pred)\n losses.append(loss.numpy())\n grads = tape.gradient(loss, variables)\n optimizer.apply_gradients(zip(grads, variables))" | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "print(losses[0])\nprint(losses[-1])" | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "import seaborn as sns\nsns.lineplot(x=range(epochs), y=losses)" | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "a/b" | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "predictions = []\ndef recursive_model(i,x,predictions):\n if i <= len(df):\n x = model(x,a,b)\n predictions.append(x)\n i = i+1\n recursive_model(i,x,predictions)\nrecursive_model(1,100,predictions)" | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": "%matplotlib inline\nimport matplotlib.pyplot as plt\nindex = np.array(range(len(df)))\nplt.plot(index, df['percentage_died'], label = \"reality\")\nplt.plot(index, predictions, label = \"prediction\")\nplt.legend()\nplt.show()" | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.6", | |
"language": "python", | |
"name": "python3" | |
}, | |
"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.6.9" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment