<jupyter_start><jupyter_text>Keras  1.6.1 Loss functions  Loss functions The loss function (or objective function) is the quantity that will be minimized during training. It represents a measure of success on the task at hand.For different problems there are better suiting functions:Some of them were mathematically proven to be good. Others were only observed to suit best for a certain kind of problem.Other functions: - mean_absolute_error - mean_absolute_percentage_error - mean_squared_logarithmic_error - squared_hinge - hinge - categorical_hinge - logcosh - sparse_categorical_crossentropy - kullback_leibler_divergence - poisson - cosine_proximity  Code Let us run the code from the previous section first.<jupyter_code># Importing the MNIST dataset
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Processing the input data
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255

# Processing the output data
from keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# Build a network
from keras import models
from keras import layers

network = models.Sequential()
network.add(layers.Dense(units=512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(units=10, activation='softmax'))<jupyter_output><empty_output><jupyter_text>Let us now examine the code related to compiling a network.<jupyter_code># Compile the network
network.compile(optimizer='rmsprop',
                loss='categorical_crossentropy',
                metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Here we are using `rmsprop` as the optimizer and `categorical_crossentropy` as the loss function. The performance metric is `accuracy`.<jupyter_code>import matplotlib.pyplot as plt
def plot_training_history(history):
    plt.plot(history.history['acc'])
    plt.plot(history.history['val_acc'])
    plt.title('model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()
    #loss
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()
# Train the network
history = network.fit(train_images, train_labels, epochs=5, batch_size=128, 
                      verbose=1, validation_data=(test_images, test_labels))

# Plot the training results
plot_training_history(history)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10196649672250546, 'fraction_numerical': 0.01638747268754552, 'mean_word_length': 5.329493087557604, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '190296', 'license': 'MIT', 'n_tokens_mistral': 878, 'n_tokens_neox': 827, 'n_words': 223, 'path': 'Keras/keras09.ipynb', 'repo_name': 'mlwkshops/ML101', 'size': '2746'}