Python Forum
[PyGame] Window not updating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Window not updating
#1
I have made a simple neural network to solve the xor problem but the issue lies in PyGame. I've used PyGame to display a neural network in real time as the weights are being adjusted over 10,000 iterations of training data. Everything runs fine with no errors, but the PyGame window is not updating to show the progress of the neural network.

Here's a link to my GitHub repository so you can understand the problem I'm having:
https://github.com/michaelberge8/Neural-Network

Running through the training data in main.py, on line 39 a Graphics object gr is passed into the train function. Moving into neural_network.py, on line 86 gr calls the function draw and passes in the information about the network in a matrix. Moving into graphics.py into the draw function, the nodes and weights are updated and drawn and then we exit the loop.

Problem: When the window is displayed the weights should basically be rapidly changing color from red to black depending on if they're negative or not, but the window is not updating for some reason so they are only shown in their initial state.

I'm not sure if anyone can help me, this problem is very specific so let me know if I can clarify anything. This is my first post to a forum, I've read all of the rules. If there's anything in this forum post that I'm doing wrong please let me know so I can fix it.
Reply
#2
Here's the Graphics.draw method:
    def draw(self, input, hidden, output, __weights_ih, __weights_ho):
        pg.init()
        size = (self.__SCREEN_WIDTH, self.__SCREEN_HEIGHT)
        screen = pg.display.set_mode(size)
        pg.display.set_caption("Neural Network Visualization")

        game_exit = False
        while not game_exit:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    sys.exit(0)

            screen.fill(self.white)
            self.update_input_nodes(matrix.Matrix.to_array(input))
            self.update_hidden_nodes(matrix.Matrix.to_array(hidden))
            self.update_output_nodes(matrix.Matrix.to_array(output))
            self.update_ih_weights(matrix.Matrix.to_array(__weights_ih))
            self.update_ho_weights(matrix.Matrix.to_array(__weights_ho))
            self.draw_ih_weights(screen, self.black, self.red)
            self.draw_ho_weights(screen, self.black, self.red)
            self.draw_input_nodes(screen, self.black, self.red)
            self.draw_hidden_nodes(screen, self.black, self.red)
            self.draw_output_nodes(screen, self.black, self.red)
            pg.display.update()
            game_exit = True
This is called by NeuralNet.train(), which itself is called 4 times for each iteration (40,000 times total).

With that in mind, here's what I see...
1) the draw method creates a window. It doesn't just draw, it creates a new window, starts a new event loop, etc. If it wasn't for the game_exit = True at the end of the while loop, this would be an infinite loop that would never do anything.
2) because the draw method is called everytime there's new data to draw, I don't think the Graphics class needs to maintain updating the interface at all. ie: the event loop can just be removed entirely, as it'll be called when it needs to update the screen.
3) I'd suggest moving the window-creation to it's own function and calling that from init (or just moving the creation to init). draw() shouldn't have anything to do with that, unless you want thousands of windows popping up with each individual state of the neural net.

So that brings us to here:
class Graphics:
    def __init__(self):
        # whatever's currently in your init
        self.screen = self.open_window()

    def open_window(self):
        pg.init()
        size = (self.__SCREEN_WIDTH, self.__SCREEN_HEIGHT)
        screen = pg.display.set_mode(size)
        pg.display.set_caption("Neural Network Visualization")
        return screen

    def draw(self, input, hidden, output, __weights_ih, __weights_ho):
        self.screen.fill(self.white)
        self.update_input_nodes(matrix.Matrix.to_array(input))
        self.update_hidden_nodes(matrix.Matrix.to_array(hidden))
        self.update_output_nodes(matrix.Matrix.to_array(output))
        self.update_ih_weights(matrix.Matrix.to_array(__weights_ih))
        self.update_ho_weights(matrix.Matrix.to_array(__weights_ho))
        self.draw_ih_weights(self.screen, self.black, self.red)
        self.draw_ho_weights(self.screen, self.black, self.red)
        self.draw_input_nodes(self.screen, self.black, self.red)
        self.draw_hidden_nodes(self.screen, self.black, self.red)
        self.draw_output_nodes(self.screen, self.black, self.red)
        pg.display.update()
Reply
#3
In addition to what nilamo said i dont believe your color variables are changing at all as you expect it to be. If you throw a couple prints around color is always the same and never seems to change in any of the draw methods.
Output:
Progress: [███-----------------------------------------------] 7.3% Completecolor is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) Progress: [███-----------------------------------------------] 7.3% Completecolor is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) Progress: [███-----------------------------------------------] 7.4% Completecolor is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) Progress: [███-----------------------------------------------] 7.4% Completecolor is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) Progress: [███-----------------------------------------------] 7.4% Completecolor is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) Progress: [███-----------------------------------------------] 7.4% Completecolor is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) Progress: [███-----------------------------------------------] 7.4% Completecolor is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) Progress: [███-----------------------------------------------] 7.4% Completecolor is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) Progress: [███-----------------------------------------------] 7.4% Completecolor is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75) color is (75, 75, 75)
If they are rapidly suppose to be changing from red to black and back, then it doesnt look like the color is changing at all in the first place.

Which leads me to believe that there might be an issue with your condition, or even before further of that. Didn't look too far into your code.
Quote:
        for i in range(len(self.__o_nodes)):
            if self.__o_nodes[i].data >= 0: color = black
            else: color = red

Which in turn, leads me to show you this...
https://python-forum.io/Thread-Basic-Nev...n-sequence
as it makes it hard to read for everyone and may introduce bugs. As well as not very pythonic.
Recommended Tutorials:
Reply
#4
Thanks guys I was able to figure out what to fix in order to get it working, it was a combination of both of what you guys explained!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020