def play(self):
        key = Arrow.input()
        while key != Arrow.NONE:
            print(key)
            key = Arrow.input()

        print("None of the arrow keys was pressed")
Beispiel #2
0
def min_approx_error_arrow(arrow: Arrow,
                           input_data: List,
                           error_filter=is_error_port,
                           **kwargs) -> CompositeArrow:
    """
    Find parameter values of arrow which minimize approximation error of arrow(data)
    Args:
        arrow: Parametric Arrow
        input_data: List of input data for each input of arrow

    Returns:
        parametric_arrow with parameters fixed
    """
    with tf.name_scope(arrow.name):
        input_tensors = gen_input_tensors(arrow)
        output_tensors = arrow_to_graph(arrow, input_tensors)
    # show_tensorboard_graph()

    param_tensors = [
        t for i, t in enumerate(input_tensors)
        if is_param_port(arrow.in_ports()[i])
    ]
    error_tensors = [
        t for i, t in enumerate(output_tensors)
        if error_filter(arrow.out_ports()[i])
    ]
    assert len(param_tensors) > 0, "Must have parametric inports"
    assert len(error_tensors) > 0, "Must have error outports"
    train_tf(param_tensors, error_tensors, input_tensors, output_tensors,
             input_data, **kwargs)
    def play(self):
        self.print_board()

        key = Arrow.input()
        while key != Arrow.NONE and self.game_going():
            self.arrange_board(key)
            self.open_two()
            self.open_two()
            self.print_board()
            key = Arrow.input()

        if self.game_going():
            print("None of the arrow keys was pressed")
        else:
            print("Game over!")
    def play(self):
        self.add_number()
        self.add_number()
        self.print_board()
        key = Arrow.input()
        while key != Arrow.NONE:
            print(key)
            moves = self.turn(key)
            if moves != 0:
                self.turn_finish()
                self.add_number()
            self.print_board()
            if self.lose_check():
                break
            key = Arrow.input()

        print("None of the arrow keys was pressed")
Beispiel #5
0
def extract_tensors(arrow: Arrow, grabs, extra_ports=[], optional=None):
    """
    Converts an arrow into a graph and extracts tensors which correspond to
    ports with particular properties
    Args:
        arrow: arrow to convert
        grabs: dict {name: filter function}
        append_default: append to default grabs, if false then replaces it
    Returns:

    """
    optional = set() if optional is None else optional
    _grabs = grabs

    # Convert to tensorflow graph and get input, output, error, and parma_tensors
    with tf.name_scope(arrow.name):
        input_tensors = gen_input_tensors(arrow, param_port_as_var=False)
        port_grab = {p: None for p in extra_ports}
        output_tensors = arrow_to_graph(arrow,
                                        input_tensors,
                                        port_grab=port_grab)

    extra_tensors = list(port_grab.values())
    output = {}
    for grab_key, grab_func in _grabs.items():
        all_dem = []
        for i, t in enumerate(input_tensors):
            if grab_func(arrow.in_port(i)):
                all_dem.append(t)
        for i, t in enumerate(output_tensors):
            if grab_func(arrow.out_port(i)):
                all_dem.append(t)
        # for port, t in port_grab:
        #     if grab_func(port):
        #         all_dem.append(t)
        assert grab_key in optional or len(all_dem) > 0, "%s empty" % grab_key
        if len(all_dem) > 0:
            output[grab_key] = all_dem

    output['extra'] = extra_tensors
    return output
Beispiel #6
0
    def wrap(a: Arrow):
        """Wrap an arrow in a composite arrow"""
        c = CompositeArrow(name=a.name)
        for port in a.ports():
            c_port = c.add_port()
            if is_in_port(port):
                make_in_port(c_port)
                c.add_edge(c_port, port)
            if is_param_port(port):
                make_param_port(c_port)
            if is_out_port(port):
                make_out_port(c_port)
                c.add_edge(port, c_port)
            if is_error_port(port):
                make_error_port(c_port)
            transfer_labels(port, c_port)

        assert c.is_wired_correctly()
        return c
Beispiel #7
0
def generic_binary_inv(arrow: Arrow, port_values: PortAttributes,
                       PInverseArrow, Port0ConstArrow, Port0ConstPortMap,
                       Port1ConstArrow,
                       Port1ConstPortMap) -> Tuple[Arrow, PortMap]:
    # FIXME: Is this actually correct for mul/add/sub
    port_0_const = is_constant(arrow.in_ports()[0], port_values)
    port_1_const = is_constant(arrow.in_ports()[1], port_values)

    if port_0_const and port_1_const:
        # If both ports constant just return arrow as is
        inv_arrow = deepcopy(arrow)
        port_map = {0: 0, 1: 1, 2: 2}
    elif port_0_const:
        inv_arrow = Port0ConstArrow()
        port_map = Port0ConstPortMap
    elif port_1_const:
        inv_arrow = Port1ConstArrow()
        port_map = Port1ConstPortMap
    else:
        # Neither constant, do 'normal' parametric inversison
        inv_arrow = PInverseArrow()
        port_map = {0: 2, 1: 3, 2: 0}

    return inv_arrow, port_map
Beispiel #8
0
def input_gen(arrow: Arrow):
    return [np.random.rand() for i in range(arrow.num_in_ports())]
Beispiel #9
0
def generate_constraints(arrow: Arrow,
                         input_expr: MutableMapping[int, Expr],
                         output_expr: MutableMapping[int, Expr]) -> Set[Rel]:
    if arrow.is_primitive():
        return arrow.gen_constraints(input_expr, output_expr)
Beispiel #10
0
def input_gen(arrow: Arrow):
    ndim = 3
    maxdim = 100
    input_shape = tuple([np.random.randint(1, maxdim) for i in range(ndim)])
    return {port: {'shape': (), 'value': 5} for port in arrow.out_ports()}  # if not is_param_port(port)}