Ejemplo n.º 1
0
 def fitting(self, model: Sequential) -> Sequential:
     model.compile(
         optimizer="adam",
         loss="sparse_categorical_crossentropy",
         metrics=["accuracy"],
     )
     model.fit(self.data_source.x_train, self.data_source.y_train, epochs=10)
     # loss, accuracyを出力してくれる
     model.evaluate(self.data_source.x_test, self.data_source.y_test, verbose=2)
     return model
 def fitting(self, model: Sequential) -> Sequential:
     # 予測値はロジットや対数オッズ比で出力される
     predictions = model(self.data_source.x_train[:1]).numpy()
     # 確率に変換
     probability = tf.nn.softmax(predictions).numpy()
     # 損失関数。下記の書き方をすればそれぞれの標本についてクラスごとに損失のスカラを返す
     loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(
         from_logits=True)
     # loss確認する場合はコメント外す
     # loss = loss_fn(mnist.y_train[:1], predictions).numpy()
     model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
     model.fit(self.data_source.x_train, self.data_source.y_train, epochs=5)
     model.evaluate(self.data_source.x_test,
                    self.data_source.y_test,
                    verbose=2)
     return model
Ejemplo n.º 3
0
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()

    #Plot model 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()

    #Evaluate model on test data
    accuracy = model.evaluate(x_test, y_test)
    print(f"Test accuracy: {accuracy}")

    #Generate predictions for 10 samples
    print("Predictions for 10 samples")
    predictions = model.predict(x_test[:10])
    y_new = model.predict_classes(x_test[:10])
    y_pred = model.predict(x_test)
    print("Shape of predictions")
    print(y_pred.shape)

    #Generate confusion matrix

    # rounded_predictions = model.predict_classes(x_test, batch_size=128, verbose=0
    Y_pred = np.argmax(y_pred, 1)
    Y_test = np.argmax(y_test, 1)