<jupyter_start><jupyter_text>BMIR Homework-2 (108003817 楊元福)<jupyter_code>import tensorflow as tf

from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0<jupyter_output><empty_output><jupyter_text>Original Model Performance<jupyter_code>model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Original test accuracy: ", test_acc)<jupyter_output><empty_output><jupyter_text>Original model performance: the train accuracy is **0.7795**, the validation accuracy is **0.7070**, the test accuracy is **0.7070**   Part 1:During the model construction, we start with a sequential model using convolution neural networks and max pooling:1. model = models.Sequential()2. model.add(layers.Conv2D(32, (3, 3), activation='relu',input_shape=(32, 32, 3)))3. model.add(layers.MaxPooling2D((2, 2)))4. model.add(layers.Conv2D(64, (3, 3),activation='relu'))5. model.add(layers.MaxPooling2D((2, 2)))6. model.add(layers.Conv2D(64, (3, 3),activation='relu'))  a. Change the mask size to (5, 5) in line 2 and find the accuracy<jupyter_code>model = models.Sequential()
model.add(layers.Conv2D(32, (5, 5), activation='relu', input_shape=(32, 32, 3))) # <= change here !!
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Part_1_a: test accuracy: ", test_acc)<jupyter_output><empty_output><jupyter_text>Answer of Part_1_a: * 更改Line2 mask size (5,5) 之後的測試準確率為**0.7229**, 高於與原先model的測試準確率**0.7070**  b. Change the channel size from 64 down to 32 in line 4 and find accuracy.<jupyter_code>model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(32, (3, 3), activation='relu')) # <= change here !!
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Part_1_b: test accuracy: ", test_acc)<jupyter_output><empty_output><jupyter_text>Answer of Part_1_b: * 更改Line4 channel size 32 之後的測試準確率為**0.6959**, 低於與原先model的測試準確率**0.7070**  c. Change the channel size from 64 down to 32 in line 6 and find accuracy<jupyter_code>model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(32, (3, 3), activation='relu')) # <= change here !!
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Part_1_c: test accuracy: ", test_acc)<jupyter_output><empty_output><jupyter_text>Answer of Part_1_c: * 更改Line6 channel size 32 之後的準確率為**0.7046**, 與原先model的測試準確率**0.7070** 相似(略低)  d. Delete lines 5 and 6 (i.e., we are using only two convolutional layers), and find accuracy.<jupyter_code>model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
# model.add(layers.MaxPooling2D((2, 2))) # <= change here !!
# model.add(layers.Conv2D(64, (3, 3), activation='relu')) # <= change here !!
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Part_1_d: test accuracy: ", test_acc)<jupyter_output><empty_output><jupyter_text>Answer of Part_1_d: * 刪除Line5與Line6之後的準確率為**0.6896**, 低於與原先model的測試準確率**0.7070**  Part 2:For the second half of the model, we use flattening layer, one hidden layer, and one output layer with full connections:7. model.add(layers.Flatten())8. model.add(layers.Dense(64, activation='relu'))9. model.add(layers.Dense(10))  e. Change the node number in line 8 to values 128 and 32 and compare the accuracy.<jupyter_code>model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu')) # <= change here !!
model.add(layers.Dense(10))

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Part_2_e_128: test accuracy: ", test_acc)
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(32, activation='relu')) # <= change here !!
model.add(layers.Dense(10))

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Part_2_e_32: test accuracy: ", test_acc)<jupyter_output><empty_output><jupyter_text>Answer of Part_2_e: * 更改Line8 node數為128之後的測試準確率為**0.7173**, 高於與原先model的測試準確率**0.7070*** 更改Line8 node數為32之後的測試準確率為**0.7079**, 高於與原先model的測試準確率**0.7070*** 看起來在dense layer的節點增加, 準確率會隨之提高一些* Active learning, 由A.I.主動參與學習過程與model制定, 有機會制定更好的節點數量  f. Please add another hidden layer with node number > 64 between line 7 and 8 and compare the accuracy.<jupyter_code>model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Part_2_f: test accuracy: ", test_acc)<jupyter_output><empty_output><jupyter_text>Answer of Part_2_e: * 增加1層dense layer(node=128)之後的測試測試準確率為**0.7009**, 比起原先model的測試準確率**0.7070**相似(略低)* 準確度沒有比預期高的原因, 可能是之前convolution layer的特徵提取還沒有足夠後面給neural network 學習, 改成較深的網路如Resnet, 應該會大幅提高準確率.  Part 3:Please revise the original code so that it can classify either the mnist fashion data or digit data. And compare the performance of this cnn model versus the traditional neural network of HW1.  Fashion_mnist<jupyter_code>from tensorflow import keras
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images.astype('float32') / 255
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1)
test_images = test_images.astype('float32') / 255
test_images = test_images.reshape(test_images.shape[0], 28, 28, 1)
train_labels = train_labels.reshape(train_labels.shape[0], 1)
test_labels = test_labels.reshape(test_labels.shape[0], 1)
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Part_3_fashion_mist: test accuracy: ", test_acc)<jupyter_output><empty_output><jupyter_text>Answer of Part_3_fashion_mist: * CNN model測試測試準確率為**0.9074**, 高於原先DNN model 的測試準確率**0.8791**, 符合預期  Digit data<jupyter_code>digit_mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = digit_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images.astype('float32') / 255
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1)
test_images = test_images.astype('float32') / 255
test_images = test_images.reshape(test_images.shape[0], 28, 28, 1)
train_labels = train_labels.reshape(train_labels.shape[0], 1)
test_labels = test_labels.reshape(test_labels.shape[0], 1)
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Part_3_digit_mist: test accuracy: ", test_acc)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15741885625965996, 'fraction_numerical': 0.04791344667697063, 'mean_word_length': 6.122179416620804, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '54472', 'license': 'Apache-2.0', 'n_tokens_mistral': 5160, 'n_tokens_neox': 4905, 'n_words': 844, 'path': 'Homework2.ipynb', 'repo_name': 'Yfyangd/BMIR', 'size': '12940'}