<jupyter_start><jupyter_text>Basic classification in TensorFlow/Keras Based on:https://www.tensorflow.org/tutorials/keras/basic_classificationandhttps://elitedatascience.com/keras-tutorial-deep-learning-in-python  0. Install TensorFlow and Keras TensorFlow is an open source machine learning framework created by Google.Keras is a high-level API to build and train models in TensorFlow used in this tutorial.  0.1. Install TensorFlow and Keras with Anaconda3: pip install tensorflowpip install keras  0.2. TensorFlow and Python3 compatibility issues<jupyter_code><jupyter_output><empty_output><jupyter_text>(As of November, 2018) Anaconda3 is supplied with Python 3.7, unfortunately Tensorflow is not ready yet for that, and TensorFlow installation would fail.You should downgrade to Python 3.5 and then reinstall TensorFlow+Keras:conda install python=3.5.0pip install tensorflowpip install keras  1. Prepare the workspace and obtain+preprocess MNIST data<jupyter_code># TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Flatten, Dense, Dropout, Convolution2D, MaxPooling2D
from keras.utils import np_utils
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
print("TensorFlow version:", tf.__version__)
print("Keras version:", keras.__version__)<jupyter_output>TensorFlow version: 1.13.1
Keras version: 2.2.4-tf<jupyter_text>MNIST dataset: 70,000 images of decimal digits (0..9) in format 28x28:![image.png](attachment:image.png)<jupyter_code># Import MNIST dataset split into 60,000 for training and 10,000 for testing
dset = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = dset.load_data()
type(dset)
train_images[:10]
# Insight into imported data
num = 0
plt.imshow(train_images[num])
print(train_images[num])
print(train_images[num].shape)
print(train_labels[num])
print(train_labels)
# Categorize labels (not mandatory for our first simple network)
train_labels_cat = np_utils.to_categorical(train_labels, 10)
test_labels_cat = np_utils.to_categorical(test_labels, 10)
print(train_labels[num], train_labels_cat[num])
print(test_labels[0], test_labels_cat[0])
# Preprocess image data
test_images_orig = test_images
train_images = train_images / 255
test_images = test_images / 255
print(train_images[num])<jupyter_output>[[0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.    [...]<jupyter_text>2. Create, train and run model  2.1. Create simple model<jupyter_code>model = keras.Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dense(10, activation=tf.nn.softmax))<jupyter_output>WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.<jupyter_text>2.2. Compile model<jupyter_code>model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>2.3. Train model<jupyter_code>model.fit(train_images, train_labels_cat, epochs=10)<jupyter_output>Epoch 1/10
60000/60000 [==============================] - 7s 110us/sample - loss: 0.0373 - acc: 0.9884
Epoch 2/10
60000/60000 [==============================] - 7s 115us/sample - loss: 0.0297 - acc: 0.9907
Epoch 3/10
60000/60000 [==============================] - 7s 110us/sample - loss: 0.0235 - acc: 0.9928
Epoch 4/10
60000/60000 [==============================] - 7s 109us/sample - loss: 0.0210 - acc: 0.9933
Epoch 5/10
60000/60000 [==============================] - 7s 112us/sample - loss: 0.0159 - acc: 0.9956
Epoch 6/10
60000/60000 [==============================] - 7s 112us/sample - loss: 0.0139 - acc: 0.9959
Epoch 7/10
60000/60000 [==============================] - 7s 115us/sample - loss: 0.0117 - acc: 0.9962
Epoch 8/10
60000/60000 [==============================] - 7s 111us/sample - loss: 0.0110 - acc: 0.9962
Epoch 9/10
60000/60000 [==============================] - 7s 109us/sample - loss: 0.0087 - acc: 0.9975
Epoch 10/10
60000/60000 [==============================] - 7s 110us/sampl[...]<jupyter_text>3. Test model<jupyter_code>test_loss, test_acc = model.evaluate(test_images, test_labels_cat)
# Print accuracy
print('Test accuracy:', test_acc)
# Calculate prediction for test data
predictions = model.predict(test_images)
# Check what is predicted
num = 1
plt.imshow(test_images[num])
print(predictions[num])b

print(np.argmax(predictions[num]))<jupyter_output>2<jupyter_text>4. Visualize results  4.1. Define auxiliary functions<jupyter_code>def plot_image(i, predictions_array, true_label, img):
    predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    plt.imshow(img, cmap=plt.cm.binary)
    predicted_label = np.argmax(predictions_array)
    if predicted_label == true_label:
        color = 'blue'
    else:
        color = 'red'
    plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
    predictions_array, true_label = predictions_array[i], true_label[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    thisplot = plt.bar(range(10), predictions_array, color="#777777")
    plt.ylim([0, 1]) 
    predicted_label = np.argmax(predictions_array)
    thisplot[predicted_label].set_color('red')
    thisplot[true_label].set_color('blue')<jupyter_output><empty_output><jupyter_text>4.2. Plot prediction<jupyter_code># class names just for visualization
class_names = ['0-zero', '1-one', '2-two', '3-three', '4-four', 
               '5-five', '6-six', '7-seven', '8-eight', '9-nine']
# visualization of prediction of one image
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images_orig)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)<jupyter_output><empty_output><jupyter_text>4.3. More visualization<jupyter_code># Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
num_rows = 10
num_cols = 5
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
    plt.subplot(num_rows, 2*num_cols, 2*i+1)
    plot_image(i, predictions, test_labels, test_images_orig)
    plt.subplot(num_rows, 2*num_cols, 2*i+2)
    plot_value_array(i, predictions, test_labels)<jupyter_output><empty_output><jupyter_text>5. Import, preprocess and train FASHION-MNIST Fashion-MNIST is similar to MNIST but of clothings:![image.png](attachment:image.png)<jupyter_code># Load data
dset = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = dset.load_data()
num = 0
plt.imshow(train_images[num])
len(train_images)
# Preprocess data
train_labels_cat = np_utils.to_categorical(train_labels, 10)
test_labels_cat = np_utils.to_categorical(test_labels, 10)
test_images_orig = test_images
train_images = train_images / 255
test_images = test_images / 255
# Create and compile model
model = keras.Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dense(10, activation=tf.nn.softmax))
model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
# Train model and obtain predictions
model.fit(train_images, train_labels_cat, epochs=10)
test_loss, test_acc = model.evaluate(test_images, test_labels_cat)
print('Test accuracy:', test_acc)
predictions = model.predict(test_images)
# Plot one prediction
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
              'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
i = 1
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images_orig)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)
# Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
num_rows = 10
num_cols = 5
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
    plt.subplot(num_rows, 2*num_cols, 2*i+1)
    plot_image(i, predictions, test_labels, test_images_orig)
    plt.subplot(num_rows, 2*num_cols, 2*i+2)
    plot_value_array(i, predictions, test_labels)<jupyter_output><empty_output><jupyter_text>6. Convolutional neural network Convolutional neural networks are better suited for image recognition<jupyter_code># Load data
dset = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = dset.load_data()
test_images_orig = test_images
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1)
test_images = test_images.reshape(test_images.shape[0], 28, 28, 1)
train_labels_cat = np_utils.to_categorical(train_labels, 10)
test_labels_cat = np_utils.to_categorical(test_labels, 10)
train_images = train_images / 255
test_images = test_images / 255
# Create and train model, and obtain predictions
model = keras.Sequential()
model.add(Convolution2D(32, (3, 3), activation='relu', input_shape=(28,28, 1)))
model.add(Convolution2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

model.fit(train_images, train_labels_cat, 
              batch_size=32, epochs=3, verbose=1)
test_loss, test_acc = model.evaluate(test_images, test_labels_cat)
print('Test accuracy:', test_acc)
predictions = model.predict(test_images)
# Plot one prediction
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
              'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
i = 1
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images_orig)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)
num_rows = 10
num_cols = 5
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
    plt.subplot(num_rows, 2*num_cols, 2*i+1)
    plot_image(i, predictions, test_labels, test_images_orig)
    plt.subplot(num_rows, 2*num_cols, 2*i+2)
    plot_value_array(i, predictions, test_labels)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup