Training and Evaluating a NeuralNetwork Model#

Training a Model#

Once a NeuralNetwork model is defined, it can be trained with either encrypted or non-encrypted data using its fit() function:

>>> net.fit(X_train, X_test, batch_size=4, epochs=5)

The fit() function accepts the following parameters:

  • X_trainlist or numpy.ndarray
    • The input data.

  • y_trainlist or numpy.ndarray
    • The target labels.

  • batch_sizeint
    • The number of samples used per gradient update. Defaults to 32.

  • epochsint
    • The number of epochs to train the model. Defaults to 1.

  • metrics{'accuracy'}
    • A list of performance metrics to be used for training. If None, only the training loss is used. Defaults to None.

  • save_file_pathstring
    • Path for the resulting history file. Defaults to None.

Further, the function returns a history dictionary containing each updated performance metric per epoch:

>>> history = net.fit(X_train, X_test, batch_size=4, epochs=5)
>>> history
{"loss": [{"_usencrypt_type": "usencrypt.cipher._float.Float", "hex": "0x01b29648574cd151df21382c651cd602237b175dca1515c725852dc872dbb519"}, {"_usencrypt_type": "usencrypt.cipher._float.Float", "hex": "0xde720dca738273c5b22ea02c273c9d442db3eb3c50ce3de81d292368c413f3c3"}, {"_usencrypt_type": "usencrypt.cipher._float.Float", "hex": "0x654111ba98ed34dc46e435259d7c445766aad941c70438867d3b2f52fcbd63c2"}, {"_usencrypt_type": "usencrypt.cipher._float.Float", "hex": "0x9bc832c539b940939b6ee066b94de3e0e03343d495b26398e84bccaace3c9ca4"}, {"_usencrypt_type": "usencrypt.cipher._float.Float", "hex": "0xc28e2978a79587bc5f93cdc6c02c6f9a42949c81063b5f87588a3a332de0da25"}], "train_time": 1409.9865138530731}

Evaluating a Model#

Similarly, a trained neural network can be evaluated with its evaluate() function:

>>> net.evaluate(X_test, X_test, metrics=['accuracy'], return_dict=True)

The evaluate() function accepts the following parameters:

  • X_testlist or numpy.ndarray of float or usencrypt.cipher.Float
    • The testing features.

  • y_testlist or numpy.ndarray of float or usencrypt.cipher.Float
    • The testing labels.

  • batch_sizeint
    • The number of samples per batch. Defaults to 32.

  • return_dictstring
    • If True, it returns the results as a Python dictionary. If False, the results are returned as an array. Defaults to False.

  • metrics{'accuracy'}
    • A list of performance metrics to be used for evaluation. If None, only the overall loss is used. Defaults to None.

  • save_file_pathstring
    • Path for the resulting performance file. Defaults to None.

Further, evaluate() returns either an array or dictionary containing the resulting performance metrics, as determined by the return_dict parameter:

>>> results = net.evaluate(X_test, X_test, metrics=['accuracy'], return_dict=True)
>>> results
{"loss": {"_usencrypt_type": "usencrypt.cipher._float.Float", "hex": "0x180ce3113ca41dbd1bb1a4bd65a5aa95b6a18d1aa0092377a4cba7d0e4b95224"}, "accuracy": {"_usencrypt_type": "usencrypt.cipher._float.Float", "hex": "0x861c2d0d20f4ecb3f272133b375e82fd40035f353a3649418b443882caa8320e"}, "eval_time": 83.90078377723694}