Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발이 취미인 개발자

OpenAI Gym-FrozenLake-v1 integer argument expected, got float 오류 처리 본문

딥러닝 & 머신러닝

OpenAI Gym-FrozenLake-v1 integer argument expected, got float 오류 처리

도그풋79 2022. 3. 29. 20:05

강화학습을 공부하기 위해 인터넷으로 강좌를 보면서 공부를 하던 중

예시로 나온 FrozenLake 실행을 했더니

elf_img = pygame.transform.scale(elf_img, elf_dims)
TypeError: integer argument expected, got float

위와 같은 오류가 뜨면서 동작을 하지 않았다.

큰 마음을 먹고 진득하게 공부를 시작했더니 시작부터 참...

 

입력값이 integer로 와야 하는데 float으로 들어오면서 오류가 발생한다는 메시지였다.

왜 동영상 강좌에서는 발생하지 않는 갈까? 혹시 강사가 mac인데 내 PC는 window라서 발생하는 건지...

아무튼 오류가 발생한 경로인

c:\Python39\lib\site-packages\gym\envs\toy_text

로 이동해서 frozen_lake.py 소스를 열고 코드를 확인해 보았다.

        if self.elf_images is None:
            elfs = [
                path.join(path.dirname(__file__), "img/elf_left.png"),
                path.join(path.dirname(__file__), "img/elf_down.png"),
                path.join(path.dirname(__file__), "img/elf_right.png"),
                path.join(path.dirname(__file__), "img/elf_up.png"),
            ]
            self.elf_images = [pygame.image.load(f_name) for f_name in elfs]

        board = pygame.Surface(self.window_size, flags=SRCALPHA)
        cell_width = self.window_size[0] // self.ncol
        cell_height = self.window_size[1] // self.nrow
        smaller_cell_scale = 0.6
        small_cell_w = smaller_cell_scale * cell_width
        small_cell_h = smaller_cell_scale * cell_height

        # prepare images
        last_action = self.lastaction if self.lastaction is not None else 1
        elf_img = self.elf_images[last_action]
        elf_scale = min(
            small_cell_w / elf_img.get_width(),
            small_cell_h / elf_img.get_height(),
        )
        elf_dims = (
            elf_img.get_width() * elf_scale,
            elf_img.get_height() * elf_scale,
        )

frozen_lake.py를 보니 pygame 엔진으로 개발된 것을 알 수 있었다.

pygame 엔진은 이미지 화면에 표시하는 좌표 값을 integer만 사용할 수 있다.

그런데 elf_scale, elf_dims는 연산 이후 float형으로 변환이 될 것 같은 느낌이 들었다. 

혹시나 모르니 print를 코드를 넣고 오류 전에 값을 출력해 보았다.

역시나 소숫점이...

문제가 예상되는 부분을 모두 integer 형으로 변환하도록 캐스팅처리를 했다.

        board = pygame.Surface(self.window_size, flags=SRCALPHA)
        cell_width = self.window_size[0] // self.ncol
        cell_height = self.window_size[1] // self.nrow
        smaller_cell_scale = 0.6
        #small_cell_w = smaller_cell_scale * cell_width
        #small_cell_h = smaller_cell_scale * cell_height
        #int형으로 cast 처리        
        small_cell_w = int(smaller_cell_scale * cell_width)
        small_cell_h = int(smaller_cell_scale * cell_height)

        # prepare images
        last_action = self.lastaction if self.lastaction is not None else 1
        elf_img = self.elf_images[last_action]
        elf_scale = min(
            #small_cell_w / elf_img.get_width(),
            #small_cell_h / elf_img.get_height(),
            #int형으로 cast 처리
            int(small_cell_w / elf_img.get_width()),
            int(small_cell_h / elf_img.get_height()),
        )

 

다시 프로그램을 실행해 보면 아래와 같이 Frozen-Lake가 정상적으로 실행되는 걸 확인할 수가 있다.