Skip to content

Instantly share code, notes, and snippets.

@taka-wang
Last active March 23, 2020 10:24
Show Gist options
  • Save taka-wang/bd9b22db366333a153a6d1340e8dda4c to your computer and use it in GitHub Desktop.
Save taka-wang/bd9b22db366333a153a6d1340e8dda4c to your computer and use it in GitHub Desktop.
ai basic1 snippets
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"\n",
"mnist = tf.keras.datasets.mnist\n",
"(train_images, train_labels), (test_images, test_labels) = mnist.load_data()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"network = tf.keras.models.Sequential()\n",
"network.add(tf.keras.layers.Dense(512, activation='relu', input_shape=(28 * 28,)))\n",
"network.add(tf.keras.layers.Dense(10, activation='softmax'))\n",
"network.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"train_images = train_images.reshape((60000, 28 * 28))\n",
"train_images = train_images.astype('float32') / 255\n",
"\n",
"test_images = test_images.reshape((10000, 28 * 28))\n",
"test_images = test_images / 255.0\n",
"\n",
"train_labels = tf.keras.utils.to_categorical(train_labels)\n",
"test_labels = tf.keras.utils.to_categorical(test_labels)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 60000 samples\n",
"Epoch 1/5\n",
"60000/60000 [==============================] - 4s 72us/sample - loss: 0.2628 - accuracy: 0.9259\n",
"Epoch 2/5\n",
"60000/60000 [==============================] - 4s 63us/sample - loss: 0.1062 - accuracy: 0.9689\n",
"Epoch 3/5\n",
"60000/60000 [==============================] - 4s 63us/sample - loss: 0.0696 - accuracy: 0.9796\n",
"Epoch 4/5\n",
"60000/60000 [==============================] - 4s 73us/sample - loss: 0.0498 - accuracy: 0.9854\n",
"Epoch 5/5\n",
"60000/60000 [==============================] - 4s 68us/sample - loss: 0.0373 - accuracy: 0.9888\n"
]
},
{
"data": {
"text/plain": [
"<tensorflow.python.keras.callbacks.History at 0x10e066d10>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"network.fit(train_images, train_labels, epochs=5, batch_size=128)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000/10000 [==============================] - 1s 146us/sample - loss: 0.0634 - accuracy: 0.9799\n"
]
}
],
"source": [
"test_loss, test_acc = network.evaluate(test_images, test_labels)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment