Example #1
0
def handle_cross_entropy(node, name, c_name):
    new_tmp = []
    nm = nodes.handler.entitiesHandler.node_map
    outer_inputs = []
    for node_name in nodes.handler.entitiesHandler.node_map.keys():
        if (name in node_name.split("/") or all(x in node_name.split("/"))
                for x in name.split("/")) and 'gradients' not in node_name:
            for inp in nm[node_name].get_inputs():
                if name not in inp.get_name():
                    outer_inputs.append(inp)
    while outer_inputs != []:
        for inp in outer_inputs:
            #print("Searching for ",inp.get_name()," operation ",inp.get_op())
            if inp.get_op() == "Placeholder":
                for dim in inp.get_attr()["shape"].shape.dim:
                    if dim.size != -1:
                        if dim.size > 2:
                            return cost_function(
                                c_name, categorical_cross_entropy(name, node))
                        else:
                            return cost_function(
                                c_name, binary_cross_entropy(name, node))
                print(
                    "ERROR:Found placeholder,but the sizes are all negatives.")
                return cost_function(c_name,
                                     categorical_cross_entropy(name, node))
            elif inp.get_op(
            ) in nodes.handler.entitiesHandler.intermediate_operations:
                for inp_ in inp.get_inputs():
                    new_tmp.append(inp_)
        outer_inputs = new_tmp
        new_tmp = []
    return ""
Example #2
0
def handle_mul_as_cross_entropy(node, c_name):
    children = node.get_inputs()
    log = False
    placeholder = False
    type = None
    for elem in children:
        if elem.get_op() == "Log":
            log = True
        if elem.get_op() == "Placeholder":
            placeholder = True
            for dim in elem.get_attr()["shape"].shape.dim:
                if dim.size != -1:
                    if dim.size > 2:
                        type = "c"
                    else:
                        type = "b"
    if log == True and placeholder == True:
        if type == "b":
            return cost_function(c_name,
                                 binary_cross_entropy(node.get_name(), node))
        else:
            return cost_function(
                c_name, categorical_cross_entropy(node.get_name(), node))
    else:
        return ""
Example #3
0
def handle_sparse_cross_entropy(node, c_name):
    for dim in node.get_output():
        for i, elem in enumerate(dim.dim):
            if elem.size != -1:
                if elem.size > 2:
                    return cost_function(
                        c_name,
                        categorical_cross_entropy(node.get_name(), node))
                else:
                    return cost_function(
                        c_name, binary_cross_entropy(node.get_name(), node))
Example #4
0
def handle_log(node):
    return cost_function("_cost",
                         categorical_cross_entropy(node.get_name(), node))
Example #5
0
def handle_sigmoid_entropy(node, c_name, name):
    #TODO SIGMOID
    return cost_function(c_name, categorical_cross_entropy(name, node, True))