# file: logistic_layer_dense_batch.py
#===============================================================================
# Copyright 2014-2017 Intel Corporation All Rights Reserved.
#
# The source code,  information  and material  ("Material") contained  herein is
# owned by Intel Corporation or its  suppliers or licensors,  and  title to such
# Material remains with Intel  Corporation or its  suppliers or  licensors.  The
# Material  contains  proprietary  information  of  Intel or  its suppliers  and
# licensors.  The Material is protected by  worldwide copyright  laws and treaty
# provisions.  No part  of  the  Material   may  be  used,  copied,  reproduced,
# modified, published,  uploaded, posted, transmitted,  distributed or disclosed
# in any way without Intel's prior express written permission.  No license under
# any patent,  copyright or other  intellectual property rights  in the Material
# is granted to  or  conferred  upon  you,  either   expressly,  by implication,
# inducement,  estoppel  or  otherwise.  Any  license   under such  intellectual
# property rights must be express and approved by Intel in writing.
#
# Unless otherwise agreed by Intel in writing,  you may not remove or alter this
# notice or  any  other  notice   embedded  in  Materials  by  Intel  or Intel's
# suppliers or licensors in any way.
#===============================================================================

#
# !  Content:
# !    Python example of forward and backward logistic layer usage
# !
# !*****************************************************************************

#
## <a name="DAAL-EXAMPLE-PY-LOGISTIC_LAYER_BATCH"></a>
## \example logistic_layer_dense_batch.py
#

import os
import sys
from os.path import join as jp

import daal.algorithms.neural_networks.layers as layers
from daal.algorithms.neural_networks.layers import forward, backward, logistic
from daal.algorithms.neural_networks.layers.logistic import forward, backward

from daal.data_management import HomogenTensor, TensorIface

import inspect
utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
if utils_folder not in sys.path:
    sys.path.insert(0, utils_folder)
from utils import printTensor, readTensorFromCSV

# Input data set parameters
datasetName = jp("..", "data", "batch", "layer.csv")

if __name__ == "__main__":

    # Retrieve the input data
    tensorData = readTensorFromCSV(datasetName)

    # Create an algorithm to compute forward logistic layer results using default method
    logisticLayerForward = logistic.forward.Batch()

    # Set input objects for the forward logistic layer
    logisticLayerForward.input.setInput(layers.forward.data, tensorData)

    # Compute forward logistic layer results
    forwardResult = logisticLayerForward.compute()

    # Print the results of the forward logistic layer
    printTensor(forwardResult.getResult(layers.forward.value), "Forward logistic layer result (first 5 rows):", 5)

    # Get the size of forward logistic layer output
    gDims = forwardResult.getResult(layers.forward.value).getDimensions()
    tensorDataBack = HomogenTensor(gDims, TensorIface.doAllocate, 0.01)

    # Create an algorithm to compute backward logistic layer results using default method
    logisticLayerBackward = logistic.backward.Batch()

    # Set input objects for the backward logistic layer
    logisticLayerBackward.input.setInput(layers.backward.inputGradient, tensorDataBack)
    logisticLayerBackward.input.setInputLayerData(layers.backward.inputFromForward, forwardResult.getResultLayerData(layers.forward.resultForBackward))

    # Compute backward logistic layer results
    backwardResult = logisticLayerBackward.compute()

    # Print the results of the backward logistic layer
    printTensor(backwardResult.getResult(layers.backward.gradient), "Backward logistic layer result (first 5 rows):", 5)
