<jupyter_start><jupyter_text>Trenowanie prostej sieci neuronowej na zbiorze MNIST<jupyter_code>import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K

import warnings
warnings.filterwarnings('ignore')<jupyter_output>Using TensorFlow backend.<jupyter_text>Załadowanie danych<jupyter_code># zdefiniowanie wymiarów obrazu wejsciowego
img_rows, img_cols = 28, 28
(X_train, y_train), (X_test, y_test) = mnist.load_data()<jupyter_output><empty_output><jupyter_text>Eksploracja danych<jupyter_code>print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)

print('Liczba dabych treningowych:', X_train.shape[0])
print('Liczba danych testowych:', X_test.shape[0])
print('Rozmiar pojedynczego obrazka:', X_train[0].shape)<jupyter_output>(60000, 28, 28)
(10000, 28, 28)
(60000,)
(10000,)
Liczba dabych treningowych: 60000
Liczba danych testowych: 10000
Rozmiar pojedynczego obrazka: (28, 28)<jupyter_text>Wyświetlenie obrazka<jupyter_code>import matplotlib.pyplot as plt

plt.imshow(X_train[0], cmap='Greys')
plt.axis('off')<jupyter_output><empty_output><jupyter_text>Wyświetlenie kilku obrazków<jupyter_code>plt.figure(figsize=(13, 13))
for i in range(1, 11):
    plt.subplot(1, 10, i)
    plt.axis('off')
    plt.imshow(X_train[i], cmap='Greys')
plt.show()<jupyter_output><empty_output><jupyter_text>Wyświetlenie danych<jupyter_code>print(X_train[0][10])

# dolna połówka obrazka
plt.imshow(X_train[0][14:], cmap='Greys')
# górna połówka obrazka
plt.imshow(X_train[0][:14], cmap='Greys')<jupyter_output><empty_output><jupyter_text>Przycinanie obrazka<jupyter_code>plt.imshow(X_train[0][5:20, 5:20], cmap='Greys')<jupyter_output><empty_output><jupyter_text>Obsługa problemu zapisu obrazów wejściowych - channel first vs. channel last<jupyter_code>print(K.image_data_format())

if K.image_data_format() == 'channel_first':
    X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
    X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
    X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)
    
print(input_shape)<jupyter_output>channels_last
(28, 28, 1)<jupyter_text>Wyświetlenie etykiet<jupyter_code>print('y_train:', y_train)
print('y_train shape:', y_train.shape)<jupyter_output>y_train: [5 0 4 ... 5 6 8]
y_train shape: (60000,)<jupyter_text>Przygotowanie danych<jupyter_code>X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

X_train /= 255
X_test /= 255

print(X_train.shape)
print(X_test.shape)<jupyter_output>(60000, 28, 28, 1)
(10000, 28, 28, 1)<jupyter_text>Przygotowanie etykiet<jupyter_code>y_train = keras.utils.to_categorical(y_train, num_classes=10)
y_test = keras.utils.to_categorical(y_test, num_classes=10)

print(y_train.shape)
print(y_test.shape)
print(y_train[0])<jupyter_output>[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]<jupyter_text>Budowa modelu<jupyter_code>model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(units=128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_7 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 24, 24, 64)        18496     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 12, 12, 64)        0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 12, 12, 64)        0         
_________________________________________________________________
flatten_2 (Flatten)          (None, 9216)              0         
_________________________________________________________________
dense_2 (Dense)              (None, 128)               1179776   
_________________________________________________________________
dropout_3 [...]<jupyter_text>Kompilacja modelu<jupyter_code>model.compile(optimizer='adadelta',
             loss='categorical_crossentropy',
             metrics=['accuracy'])<jupyter_output>W0711 08:08:29.919452 139693191944064 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

W0711 08:08:29.953210 139693191944064 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3295: The name tf.log is deprecated. Please use tf.math.log instead.<jupyter_text>Trenowanie modelu<jupyter_code>history = model.fit(X_train, y_train,
                   batch_size=128,
                   epochs=20,
                   validation_data=(X_test, y_test))<jupyter_output>W0711 08:08:33.070628 139693191944064 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_grad.py:1250: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where<jupyter_text>Ocena modelu<jupyter_code>score = model.evaluate(X_test, y_test)
print('Test loss:', score[0])
print('Test accuracy:', score[1])<jupyter_output>10000/10000 [==============================] - 1s 105us/step
Test loss: 0.030402481143582055
Test accuracy: 0.9911<jupyter_text>Wykres dokładności<jupyter_code>def make_accuracy_plot(history):
    """
    Funkcja zwraca wykres dokładności (accuracy) modelu na zbiorze treningowym
    i walidacyjnym.
    """
    import matplotlib.pyplot as plt
    import seaborn as sns
    sns.set()
    acc, val_acc = history.history['acc'], history.history['val_acc']
    epochs = range(1, len(acc) + 1)

    plt.figure(figsize=(10, 8))
    plt.plot(epochs, acc, label='Dokładność trenowania', marker='o')
    plt.plot(epochs, val_acc, label='Dokładność walidacji', marker='o')
    plt.legend()
    plt.title('Dokładność trenowania i walidacji')
    plt.xlabel('Epoki')
    plt.ylabel('Dokładność')
    plt.show()

def make_loss_plot(history):
    """
    Funkcja zwraca wykres straty (loss) modelu na zbiorze treningowym
    i walidacyjnym.
    """
    import matplotlib.pyplot as plt
    import seaborn as sns
    sns.set()
    loss, val_loss = history.history['loss'], history.history['val_loss']
    epochs = range(1, len(loss) + 1)
    
    plt.figure(figsize=(10, 8))
    plt.plot(epochs, loss, label='Strata trenowania', marker='o')
    plt.plot(epochs, val_loss, label='Strata walidacji', marker='o')
    plt.legend()
    plt.title('Strata trenowania i walidacji')
    plt.xlabel('Epoki')
    plt.ylabel('Strata')
    plt.show()
make_accuracy_plot(history)
make_loss_plot(history)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup