complete Lab24
This commit is contained in:
parent
4021991ec3
commit
f453a4d74f
BIN
Lab2/assets/fashion-mnist.zip
Normal file
BIN
Lab2/assets/fashion-mnist.zip
Normal file
Binary file not shown.
302
Lab2/examples/Lab24-iris.ipynb
Normal file
302
Lab2/examples/Lab24-iris.ipynb
Normal file
File diff suppressed because one or more lines are too long
770
Lab2/examples/Lab24-mnist.ipynb
Normal file
770
Lab2/examples/Lab24-mnist.ipynb
Normal file
File diff suppressed because one or more lines are too long
127
Lab2/examples/Lab24-xor.ipynb
Normal file
127
Lab2/examples/Lab24-xor.ipynb
Normal file
@ -0,0 +1,127 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "ab7d7dbd-f2e0-4384-8c55-d4aeee74dd7c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"The tensorboard extension is already loaded. To reload it, use:\n",
|
||||
" %reload_ext tensorboard\n",
|
||||
"WARNING:tensorflow:5 out of the last 5 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7fe3e8248550> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n",
|
||||
"1/1 [==============================] - 0s 33ms/step\n",
|
||||
"[[0.03452728]\n",
|
||||
" [0.9867295 ]\n",
|
||||
" [0.9883936 ]\n",
|
||||
" [0.01205833]]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Reusing TensorBoard on port 6008 (pid 911540), started 0:22:54 ago. (Use '!kill 911540' to kill it.)"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"\n",
|
||||
" <iframe id=\"tensorboard-frame-e941beefef1b33a0\" width=\"100%\" height=\"800\" frameborder=\"0\">\n",
|
||||
" </iframe>\n",
|
||||
" <script>\n",
|
||||
" (function() {\n",
|
||||
" const frame = document.getElementById(\"tensorboard-frame-e941beefef1b33a0\");\n",
|
||||
" const url = new URL(\"/\", window.location);\n",
|
||||
" const port = 6008;\n",
|
||||
" if (port) {\n",
|
||||
" url.port = port;\n",
|
||||
" }\n",
|
||||
" frame.src = url;\n",
|
||||
" })();\n",
|
||||
" </script>\n",
|
||||
" "
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%load_ext tensorboard\n",
|
||||
"\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np \n",
|
||||
"import datetime, os\n",
|
||||
"\n",
|
||||
"tf.config.experimental.set_visible_devices([], \"GPU\") \n",
|
||||
"\n",
|
||||
"X = np.array([[0,0],[0,1],[1,0],[1,1]])\n",
|
||||
"y = np.array([[0],[1],[1],[0]])\n",
|
||||
" \n",
|
||||
"model = tf.keras.models.Sequential([\n",
|
||||
" tf.keras.layers.Dense(8, activation='relu', name='layers_dense_1'),\n",
|
||||
" tf.keras.layers.Dense(8, activation='relu', name='layers_dense_2'),\n",
|
||||
" tf.keras.layers.Dense(1, activation='sigmoid', name='layers_dense_3')\n",
|
||||
"])\n",
|
||||
" \n",
|
||||
"loss_fn = tf.keras.losses.binary_crossentropy\n",
|
||||
"\n",
|
||||
"simple = True\n",
|
||||
"\n",
|
||||
"if simple == True:\n",
|
||||
" model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n",
|
||||
" \n",
|
||||
" logdir = os.path.join(\"logs\", datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n",
|
||||
" tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)\n",
|
||||
" model.fit(X, y, batch_size=4, epochs=1000, verbose=0, callbacks = [tensorboard_callback])\n",
|
||||
"else:\n",
|
||||
" for i in range(100):\n",
|
||||
" with tf.GradientTape() as tape:\n",
|
||||
" # Forward pass.\n",
|
||||
" predictions = model(X)\n",
|
||||
" # Compute the loss value for this batch.\n",
|
||||
" loss_value = loss_fn(y, predictions)\n",
|
||||
"\n",
|
||||
" # Get gradients of loss wrt the weights.\n",
|
||||
" gradients = tape.gradient(loss_value, model.trainable_weights)\n",
|
||||
" # Update the weights of the model.\n",
|
||||
" optimizer.apply_gradients(zip(gradients, model.trainable_weights))\n",
|
||||
"\n",
|
||||
"print(model.predict(X))\n",
|
||||
"\n",
|
||||
"%tensorboard --logdir logs"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.8.16"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
30
Lab2/examples/datasets/iris_test.csv
Normal file
30
Lab2/examples/datasets/iris_test.csv
Normal file
@ -0,0 +1,30 @@
|
||||
5.9,3,4.2,1.5,1
|
||||
6.9,3.1,5.4,2.1,2
|
||||
5.1,3.3,1.7,0.5,0
|
||||
6,3.4,4.5,1.6,1
|
||||
5.5,2.5,4,1.3,1
|
||||
6.2,2.9,4.3,1.3,1
|
||||
5.5,4.2,1.4,0.2,0
|
||||
6.3,2.8,5.1,1.5,2
|
||||
5.6,3,4.1,1.3,1
|
||||
6.7,2.5,5.8,1.8,2
|
||||
7.1,3,5.9,2.1,2
|
||||
4.3,3,1.1,0.1,0
|
||||
5.6,2.8,4.9,2,2
|
||||
5.5,2.3,4,1.3,1
|
||||
6,2.2,4,1,1
|
||||
5.1,3.5,1.4,0.2,0
|
||||
5.7,2.6,3.5,1,1
|
||||
4.8,3.4,1.9,0.2,0
|
||||
5.1,3.4,1.5,0.2,0
|
||||
5.7,2.5,5,2,2
|
||||
5.4,3.4,1.7,0.2,0
|
||||
5.6,3,4.5,1.5,1
|
||||
6.3,2.9,5.6,1.8,2
|
||||
6.3,2.5,4.9,1.5,1
|
||||
5.8,2.7,3.9,1.2,1
|
||||
6.1,3,4.6,1.4,1
|
||||
5.2,4.1,1.5,0.1,0
|
||||
6.7,3.1,4.7,1.5,1
|
||||
6.7,3.3,5.7,2.5,2
|
||||
6.4,2.9,4.3,1.3,1
|
|
120
Lab2/examples/datasets/iris_train.csv
Normal file
120
Lab2/examples/datasets/iris_train.csv
Normal file
@ -0,0 +1,120 @@
|
||||
6.4,2.8,5.6,2.2,2
|
||||
5,2.3,3.3,1,1
|
||||
4.9,2.5,4.5,1.7,2
|
||||
4.9,3.1,1.5,0.1,0
|
||||
5.7,3.8,1.7,0.3,0
|
||||
4.4,3.2,1.3,0.2,0
|
||||
5.4,3.4,1.5,0.4,0
|
||||
6.9,3.1,5.1,2.3,2
|
||||
6.7,3.1,4.4,1.4,1
|
||||
5.1,3.7,1.5,0.4,0
|
||||
5.2,2.7,3.9,1.4,1
|
||||
6.9,3.1,4.9,1.5,1
|
||||
5.8,4,1.2,0.2,0
|
||||
5.4,3.9,1.7,0.4,0
|
||||
7.7,3.8,6.7,2.2,2
|
||||
6.3,3.3,4.7,1.6,1
|
||||
6.8,3.2,5.9,2.3,2
|
||||
7.6,3,6.6,2.1,2
|
||||
6.4,3.2,5.3,2.3,2
|
||||
5.7,4.4,1.5,0.4,0
|
||||
6.7,3.3,5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2.5,2
|
||||
5,3.5,1.6,0.6,0
|
||||
5.5,2.6,4.4,1.2,1
|
||||
5.7,3,4.2,1.2,1
|
||||
4.4,2.9,1.4,0.2,0
|
||||
4.8,3,1.4,0.1,0
|
||||
5.5,2.4,3.7,1,1
|
|
1433
Lab2/main.ipynb
Normal file
1433
Lab2/main.ipynb
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user