심층 신경망

2개의 층

In [1]:
from tensorflow import keras

(train_input, train_target), (test_input, test_target) = keras.datasets.fashion_mnist.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 1s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
In [2]:
from sklearn.model_selection import train_test_split

train_scaled = train_input / 255.0
train_scaled = train_scaled.reshape(-1, 28*28)

train_scaled, val_scaled, train_target, val_target = train_test_split(
    train_scaled, train_target, test_size=0.2, random_state=42)
In [3]:
dense1 = keras.layers.Dense(100, activation='sigmoid', input_shape=(784,))
dense2 = keras.layers.Dense(10, activation='softmax')

심층 신경망 만들기

In [4]:
model = keras.Sequential([dense1, dense2])
In [5]:
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 100)               78500     
_________________________________________________________________
dense_1 (Dense)              (None, 10)                1010      
=================================================================
Total params: 79,510
Trainable params: 79,510
Non-trainable params: 0
_________________________________________________________________

층을 추가하는 다른 방법

In [6]:
model = keras.Sequential([
    keras.layers.Dense(100, activation='sigmoid', input_shape=(784,), name='hidden'),
    keras.layers.Dense(10, activation='softmax', name='output')
], name='패션 MNIST 모델')
In [7]:
model.summary()
Model: "패션 MNIST 모델"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
hidden (Dense)               (None, 100)               78500     
_________________________________________________________________
output (Dense)               (None, 10)                1010      
=================================================================
Total params: 79,510
Trainable params: 79,510
Non-trainable params: 0
_________________________________________________________________
In [8]:
model = keras.Sequential()
model.add(keras.layers.Dense(100, activation='sigmoid', input_shape=(784,)))
model.add(keras.layers.Dense(10, activation='softmax'))
In [9]:
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_2 (Dense)              (None, 100)               78500     
_________________________________________________________________
dense_3 (Dense)              (None, 10)                1010      
=================================================================
Total params: 79,510
Trainable params: 79,510
Non-trainable params: 0
_________________________________________________________________
In [10]:
model.compile(loss='sparse_categorical_crossentropy', metrics='accuracy')

model.fit(train_scaled, train_target, epochs=5)
Epoch 1/5
1500/1500 [==============================] - 6s 2ms/step - loss: 0.5622 - accuracy: 0.8084
Epoch 2/5
1500/1500 [==============================] - 4s 2ms/step - loss: 0.4080 - accuracy: 0.8534
Epoch 3/5
1500/1500 [==============================] - 4s 2ms/step - loss: 0.3735 - accuracy: 0.8661
Epoch 4/5
1500/1500 [==============================] - 4s 2ms/step - loss: 0.3517 - accuracy: 0.8730
Epoch 5/5
1500/1500 [==============================] - 4s 2ms/step - loss: 0.3358 - accuracy: 0.8787
Out[10]:
<tensorflow.python.keras.callbacks.History at 0x7f27703bc050>

렐루 활성화 함수

In [11]:
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28, 28)))
model.add(keras.layers.Dense(100, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))
In [12]:
model.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten (Flatten)            (None, 784)               0         
_________________________________________________________________
dense_4 (Dense)              (None, 100)               78500     
_________________________________________________________________
dense_5 (Dense)              (None, 10)                1010      
=================================================================
Total params: 79,510
Trainable params: 79,510
Non-trainable params: 0
_________________________________________________________________
In [13]:
(train_input, train_target), (test_input, test_target) = keras.datasets.fashion_mnist.load_data()

train_scaled = train_input / 255.0

train_scaled, val_scaled, train_target, val_target = train_test_split(
    train_scaled, train_target, test_size=0.2, random_state=42)
In [14]:
model.compile(loss='sparse_categorical_crossentropy', metrics='accuracy')

model.fit(train_scaled, train_target, epochs=5)
Epoch 1/5
1500/1500 [==============================] - 4s 2ms/step - loss: 0.5372 - accuracy: 0.8090
Epoch 2/5
1500/1500 [==============================] - 4s 2ms/step - loss: 0.3932 - accuracy: 0.8594
Epoch 3/5
1500/1500 [==============================] - 4s 2ms/step - loss: 0.3552 - accuracy: 0.8716
Epoch 4/5
1500/1500 [==============================] - 4s 2ms/step - loss: 0.3345 - accuracy: 0.8795
Epoch 5/5
1500/1500 [==============================] - 4s 2ms/step - loss: 0.3210 - accuracy: 0.8856
Out[14]:
<tensorflow.python.keras.callbacks.History at 0x7f2770146490>
In [15]:
model.evaluate(val_scaled, val_target)
375/375 [==============================] - 1s 2ms/step - loss: 0.3516 - accuracy: 0.8803
Out[15]:
[0.3515799045562744, 0.8803333044052124]

옵티마이저

In [16]:
model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics='accuracy')
In [17]:
sgd = keras.optimizers.SGD()
model.compile(optimizer=sgd, loss='sparse_categorical_crossentropy', metrics='accuracy')
In [18]:
sgd = keras.optimizers.SGD(learning_rate=0.1)
In [19]:
sgd = keras.optimizers.SGD(momentum=0.9, nesterov=True)
In [20]:
adagrad = keras.optimizers.Adagrad()
model.compile(optimizer=adagrad, loss='sparse_categorical_crossentropy', metrics='accuracy')
In [21]:
rmsprop = keras.optimizers.RMSprop()
model.compile(optimizer=rmsprop, loss='sparse_categorical_crossentropy', metrics='accuracy')
In [22]:
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28, 28)))
model.add(keras.layers.Dense(100, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))
In [23]:
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics='accuracy')

model.fit(train_scaled, train_target, epochs=5)
Epoch 1/5
1500/1500 [==============================] - 4s 2ms/step - loss: 0.5239 - accuracy: 0.8167
Epoch 2/5
1500/1500 [==============================] - 3s 2ms/step - loss: 0.3936 - accuracy: 0.8604
Epoch 3/5
1500/1500 [==============================] - 3s 2ms/step - loss: 0.3490 - accuracy: 0.8728
Epoch 4/5
1500/1500 [==============================] - 3s 2ms/step - loss: 0.3248 - accuracy: 0.8812
Epoch 5/5
1500/1500 [==============================] - 3s 2ms/step - loss: 0.3063 - accuracy: 0.8876
Out[23]:
<tensorflow.python.keras.callbacks.History at 0x7f2729ae52d0>
In [24]:
model.evaluate(val_scaled, val_target)
375/375 [==============================] - 1s 2ms/step - loss: 0.3301 - accuracy: 0.8808
Out[24]:
[0.3301279842853546, 0.8807500004768372]