AI-Brain/AIBrain.ipynb

224 lines
4.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import YouTubeVideo\n",
"YouTubeVideo('cNxadbrN_aI')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"\n",
"tf.config.list_physical_devices('GPU')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fashion_mnist = tf.keras.datasets.fashion_mnist\n",
"(train_images, train_labels), (valid_images, valid_labels) = fashion_mnist.load_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"# The question number to study with. Feel free to change up to 59999.\n",
"data_idx = 42\n",
"\n",
"plt.figure()\n",
"plt.imshow(train_images[data_idx], cmap='gray')\n",
"plt.colorbar()\n",
"plt.grid(False)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_labels[data_idx]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"# The question number to quiz with. Feel free to change up to 9999.\n",
"data_idx = 6174\n",
"\n",
"plt.figure()\n",
"plt.imshow(valid_images[data_idx], cmap='gray')\n",
"plt.colorbar()\n",
"plt.grid(False)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"valid_labels[data_idx]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 28 lists with 28 values each\n",
"valid_images[data_idx]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"number_of_classes = train_labels.max() + 1\n",
"number_of_classes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = tf.keras.Sequential([\n",
" tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
" tf.keras.layers.Dense(number_of_classes)\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image_height = 28\n",
"image_width = 28\n",
"\n",
"number_of_weights = image_height * image_width * number_of_classes\n",
"number_of_weights"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tf.keras.utils.plot_model(model, show_shapes=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.compile(optimizer='adam',\n",
" loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"history = model.fit(\n",
" train_images,\n",
" train_labels,\n",
" epochs=5,\n",
" verbose=True,\n",
" validation_data=(valid_images, valid_labels)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.predict(train_images[0:10])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_idx = 8675 # The question number to study with. Feel free to change up to 59999.\n",
"\n",
"plt.figure()\n",
"plt.imshow(train_images[data_idx], cmap='gray')\n",
"plt.colorbar()\n",
"plt.grid(False)\n",
"plt.show()\n",
"\n",
"x_values = range(number_of_classes)\n",
"plt.figure()\n",
"plt.bar(x_values, model.predict(train_images[data_idx:data_idx+1]).flatten())\n",
"plt.xticks(range(10))\n",
"plt.show()\n",
"\n",
"print(\"correct answer:\", train_labels[data_idx])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}