Пример #1
0
 def __init__(self, input_file: str):
     """
     Constructor for Simulation class.
     :param input_file: Path to the input file.
     """
     self.p = []
     self.a = []
     self.l = []
     self.n = Network()
     self.t_max = 0
     self.events = []
     self.propose_counter = 0
     self.accepted = None
     self.accepted_n = 0
     self.current_tick = 0
     self.read_input_file(input_file)
Пример #2
0
def run_simple_net():
    network = Network([
        Layer.init_with_weights(
            read_matrix_from_file('resources/weights' + str(i)), sigmoid,
            sigmoid_prime) for i in range(2)
    ], j_cross_entropy, j_cross_entropy_derivative)

    # data = transform_input_imgs_to_data(read_char_images_from_dir('resources/test', False, 36, 27))
    data, answers = read_all_char_examples_with_answers(
        'resources/train', False)

    data = transform_input_imgs_to_data(data)
    print(network.sgd(data, answers, 110, 10, 10, eps=1e-7, visualize=True))

    for i, l in enumerate(network.layers):
        save_matrix_to_file('resources/weights' + str(i), l.get_weights())

    print(network.get_result_matrix(data))

    print(network.process_input(data, answers))
def create_networks(labels, net_size):
    """
    Create different networks based on the labels (digits) in the image file.
    :param labels: The labels for which we will create networks, need to cast to str to iterate and then to int for
    the Network class.
    :param net_size: The size of the weight list the network should have.
    :return: A list of testing images and a list of training images
    """
    labels = str(labels)
    networks = []
    for num in labels:
        networks.append(Network(int(num), net_size))

    return networks
Пример #4
0
def check_image(letter):
    letter = np.asarray(letter.convert('LA'))[:, :, -1] / 255
    conv_layer1 = ConvolutionLayer.init_with_weights_from_file(
        'resources/weights_kernels_t0', 5, 2)
    conv_layer2 = ConvolutionLayer.init_with_weights_from_file(
        'resources/weights_kernels_t1', 5, 2)
    network_conv = Network([
        Layer.init_with_weights(
            read_matrix_from_file('resources/weights_conv_t' + str(i)),
            sigmoid, sigmoid_prime) for i in range(2)
    ], target_func_for_tests, j_cross_entropy_derivative)
    network = ConvolutionNetwork([conv_layer1, conv_layer2], network_conv)

    res_matrix = network.get_result_matrix(np.array([[letter]]))
    return np.argmax(res_matrix.flatten())
Пример #5
0
def run_conv_net():
    conv_layer1 = ConvolutionLayer.init_with_weights_from_file(
        'resources/weights_kernels0', 5, 2)
    conv_layer2 = ConvolutionLayer.init_with_weights_from_file(
        'resources/weights_kernels1', 5, 2)
    # conv_layer1 = ConvolutionLayer.init_random_convolution_layer(10, 5, 2)
    # conv_layer2 = ConvolutionLayer.init_random_convolution_layer(20, 5, 2)
    layer1 = Layer.layer_with_random_weights(50, 320, sigmoid, sigmoid_prime)
    layer2 = Layer.layer_with_random_weights(10, 50, sigmoid, sigmoid_prime)

    # network_conv = Network([layer1, layer2], j_cross_entropy, j_cross_entropy_derivative)
    network_conv = Network([
        Layer.init_with_weights(
            read_matrix_from_file('resources/weights_conv' + str(i)), sigmoid,
            sigmoid_prime) for i in range(2)
    ], j_cross_entropy, j_cross_entropy_derivative)

    network = ConvolutionNetwork([conv_layer1, conv_layer2], network_conv)

    images, answers = get_example_batch(2000)
    network.sgd(images, answers, 200, 1, 1, 200, 1e-9, visualize=False)

    # for i, l in enumerate(network.fully_connected_net.layers):
    #     save_matrix_to_file('resources/weights_conv' + str(i), l.get_weights())
    #
    # for i, l in enumerate(network.layers):
    #     save_matrix_to_file('resources/weights_kernels' + str(i), map(lambda x: x.flatten(), l.get_weights()))

    for i, l in enumerate(network.fully_connected_net.layers):
        save_matrix_to_file('resources/weights_conv_t' + str(i),
                            l.get_weights())

    for i, l in enumerate(network.layers):
        save_matrix_to_file('resources/weights_kernels_t' + str(i),
                            map(lambda x: x.flatten(), l.get_weights()))

    print(network.process_input(images, answers))
Пример #6
0
def run_on_test_data():
    conv_layer1 = ConvolutionLayer.init_with_weights_from_file(
        'resources/weights_kernels_t0', 5, 2)
    conv_layer2 = ConvolutionLayer.init_with_weights_from_file(
        'resources/weights_kernels_t1', 5, 2)
    network_conv = Network([
        Layer.init_with_weights(
            read_matrix_from_file('resources/weights_conv_t' + str(i)),
            sigmoid, sigmoid_prime) for i in range(2)
    ], target_func_for_tests, j_cross_entropy_derivative)
    network = ConvolutionNetwork([conv_layer1, conv_layer2], network_conv)

    mndata = MNIST('resources/train/mnist')

    images, labels = mndata.load_testing()
    images = np.array(images).reshape(len(images), 1, 28, 28) / 255
    answers = transform_labels_to_vectors(labels)
    print("\nQuality is {:.2f}%\n".format(
        network.process_input(images[:1000], answers[:1000]) * 100 / 1000))

    test_data = cv2.imread("/home/saul/Documents/2.png",
                           cv2.IMREAD_GRAYSCALE) / 255
    network.process_input(np.array([[abs(1 - test_data)]]),
                          np.array([[0, 0, 1, 0, 0, 0, 0, 0, 0, 0]]))
Пример #7
0
                        'mailbox': {
                            'content': encode.decode('utf-8')
                        },
                        'time': nowStr,
                        'email': email,
                        'end': 'mailbox'
                    }

                    sock.sendMessage(message)
                    lives[email] -= 1
            else:
                del lives[email]


# Initialize the connection
sock = Network(app, connected, disconnected, processMessage)
sock.connect()

# Initialize the mail detector
detector = MailDetector(app, mailDetected, frameAvailable)

# Initialize the mail sending monitor
try:
    # https://raspberrypi.stackexchange.com/questions/22444/importerror-no-module-named-thread
    _thread.start_new_thread(mailSendingMonitor, ())
except Exception as e:
    print('Unable to start thread for mail sending monitor:', e)

# Initialize the heartbeat sender
try:
    _thread.start_new_thread(sock.heartbeat, ())
Пример #8
0
class Simulation:
    def __init__(self, input_file: str):
        """
        Constructor for Simulation class.
        :param input_file: Path to the input file.
        """
        self.p = []
        self.a = []
        self.l = []
        self.n = Network()
        self.t_max = 0
        self.events = []
        self.propose_counter = 0
        self.accepted = None
        self.accepted_n = 0
        self.current_tick = 0
        self.read_input_file(input_file)

    def read_input_file(self, input_file: str) -> None:
        """
        Loads the input file.
        :param input_file: Path to the input file.
        :return: None.
        """
        with open(input_file, 'r') as file:
            events = file.readlines()
            events = list(map(lambda x: x.rstrip('\n').split(' ', 3), events))
            n_p, n_a, n_l, t_max = events[0]
            events.pop(0)
            self.events = events
            self.t_max = int(t_max)
            self.setup_computers(n_p, n_a, n_l)

    def setup_computers(self, n_p: int, n_a: int, n_l: int) -> None:
        """
        Initializes all the computers.
        :param n_p: Number of Proposers.
        :param n_a: Number of Acceptors.
        :param n_l: Number of Learners.
        :return: None.
        """
        for i in range(int(n_p)):
            self.p.append(Proposer(i + 1, self))

        for i in range(int(n_a)):
            self.a.append(Acceptor(i + 1, self))

        for i in range(int(n_l)):
            self.l.append(Learner(i + 1, self))

    def start(self) -> None:
        """
        Runs the simulations.
        :return: None.
        """
        for tick in range(self.t_max):
            self.current_tick = tick
            tick_done = False  # Tick is done when a message is send!
            for event in self.events:
                if int(event[0]) == tick:
                    event_type = event[1]
                    # Handle if a computer fails.
                    if event_type == 'FAIL':
                        if event[2] == 'PROPOSER':
                            print(f'{tick:04}: P{int(event[3])} **kapot**')
                            self.p[int(event[3]) - 1].failed = True
                        elif event[2] == 'ACCEPTOR':
                            print(f'{tick:04}: A{int(event[3])} **kapot**')
                            self.a[int(event[3]) - 1].failed = True
                    # Handle if a computer recovers.
                    elif event_type == 'RECOVER':
                        if event[2] == 'PROPOSER':
                            print(
                                f'{tick:04}: P{int(event[3])} **gerepareerd**')
                            self.p[int(event[3]) - 1].failed = False
                        elif event[2] == 'ACCEPTOR':
                            print(
                                f'{tick:04}: A{int(event[3])} **gerepareerd**')
                            self.a[int(event[3]) - 1].failed = False
                    # Handle if a proposal is made.
                    elif event_type == 'PROPOSE':
                        tick_done = True
                        m = Message(None, self.p[int(event[2]) - 1], event[1],
                                    event[3], None, None)
                        m.dst.receive_message(m)
            if not tick_done:
                self.msg_from_queue()
        for proposer in self.p:
            if proposer.value is not None:
                print(
                    f'P{proposer.id} heeft wel consensus (voorgesteld: {proposer.proposed_value}, geaccepteerd: {self.accepted})'
                )
            else:
                print(f'P{proposer.id} heeft geen consensus')

    def msg_from_queue(self) -> None:
        """
        Handles getting messages from the queue.
        :return: None
        """
        m = self.n.extract_message()
        if m:
            m.dst.receive_message(m)
        else:
            print(f'{self.current_tick:04}: ')

    def success(self) -> None:
        """
        Communicates with the Learners.
        :return:
        """
        for learner in self.l:
            learner.receive_message(
                Message(self, learner, 'SUCCESS', self.accepted, None, None))

        for acceptor in self.a:
            acceptor.prior_promised_value = None
            acceptor.prior_promised_id = 0
Пример #9
0
BATCH_SIZE = 10

def shuffle_and_split(X_train, y_train, batch_size=10):
    p = np.random.permutation(len(X_train))

    new_X_train = X_train[p]
    new_y_train = y_train[p]

    splitted_X_train = np.array_split(new_X_train, batch_size)
    splitted_y_train = np.array_split(new_y_train, batch_size)

    return splitted_X_train, splitted_y_train

if __name__ == "__main__":
    network = Network(error="l2")
    network.addLayer(1, inputSize=1, activation="identity")
    #network.addLayer(1, activation="identity")

    ds = pd.read_csv("data/1in_tanh.txt", sep=r"\s+", header=None)
    X_train, y_train = ds.iloc[:,:-1].to_numpy(), ds.iloc[:,1].to_numpy()

    splitted_X_train, splitted_y_train = shuffle_and_split(X_train, y_train, batch_size=20)

    p = np.random.permutation(len(X_train))

    new_X_train = X_train[p][:10]
    new_y_train = y_train[p][:10]


    #network.train(new_X_train, new_y_train)