layers.GRU(200, input_shape=[32, 32 * 3]), layers.Dense(100, activation='relu'), layers.Dense(10, activation='softmax') ]) model.summary() # Compile the model model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Train the model history = model.fit(x_train, y_train, epochs=30, validation_split=0.1) # Plot the learning curve plot_utils.plot_learning_curve(training_acc=history.history['accuracy'], validation_acc=history.history['val_accuracy'], file_name='gru_learning_curve') # Evaluate the model on the test set results = model.evaluate(x_test, y_test) print(f'Test accuracy: {np.round(results[1], 4)}') # Using the model to make predictions y_pred = model.predict_classes(x_test[:5]) plot_utils.plot_predictions(x_test[:5], class_names, y_test[:5], y_pred, file_name='gru_predictions')
'Shirt', 'Sneaker', 'Bag', 'Ankle boot' ] # preprocess data # Scale to num between 0 and 1 test_images = test_images / 255.0 # Define the model model = keras.models.load_model('fashion_model.h5') # Review summary model.summary() # Test model test_loss, test_acc = model.evaluate(test_images, test_labels) print('Test accuracy:', test_acc) # Let's make some predictions predictions = model.predict(test_images) print('Predictions', predictions[0]) print('First prediction (most likely):', class_names[test_labels[np.argmax(predictions[0])]]) # Plot the first 10 test images, their predicted label, and the true label # Color correct predictions in blue, incorrect predictions in red plot_utils.plot_predictions(10, predictions, test_labels, test_images)
# Accuracy acc = accuracy_score(y_true, y_pred) print 'Accuracy on test set:', acc # Find labels in use label_list = sorted(df.y_true.unique()) label_list = [LABELS[l] for l in label_list] # Plot normalized confusion matrix cnf_matrix = confusion_matrix(y_true, y_pred) output_file = os.path.join(OUTPUT_DIR, 'confusion_matrix.png') _ = plot_confusion_matrix(cnf_matrix, output_file, classes=label_list, normalize=True, title='Confusion matrix (accuracy=%.2f)' % acc) print 'Plot saved:', output_file # Classification report (F1 score, etc.) clf_report = classification_report(y_true, y_pred, target_names=label_list) output_file = os.path.join(OUTPUT_DIR, 'classification_report.png') plot_classification_report(clf_report, output_file) print 'Plot saved:', output_file # Plot predictions output_file = os.path.join(OUTPUT_DIR, 'predictions.html') title = 'Predictions (accuracy=%s)' % acc plot_predictions(t, X_values, y_true, y_pred, output_file, title) print 'Plot saved:', output_file
if vote_window > 0: y_pred = predictions_vote(y_pred, vote_window) # Accuracy acc = accuracy_score(y_true, y_pred) print 'Accuracy on test set:', acc label_list = sorted(df.y_true.unique()) # Plot normalized confusion matrix cnf_matrix = confusion_matrix(y_true, y_pred) output_file = os.path.join(output_dir, 'confusion_matrix.png') _ = plot_confusion_matrix(cnf_matrix, output_file, classes=label_list, normalize=True, title='Confusion matrix (accuracy=%.2f)' % acc) print 'Plot saved:', output_file # Classification report (F1 score, etc.) clf_report = classification_report(y_true, y_pred) output_file = os.path.join(output_dir, 'classification_report.png') plot_classification_report(clf_report, output_file) print 'Plot saved:', output_file # Plot predictions output_file = os.path.join(output_dir, 'predictions.html') title = 'Predictions (accuracy=%s)' % acc plot_predictions(t, X_values, y_true, y_pred, output_file, title) print 'Plot saved:', output_file