TF-Agents 환경 살펴보기


TF-Agents - TensorFlow 강화학습 라이브러리

이전 페이지에서 tf_agents.environments.suite_gym를 사용해서 강화학습의 환경을 불러오는 과정을 소개했습니다.

이번에는 강화학습 환경 (Environment)의 사양을 확인하는 과정에 대해 소개합니다.



1) TimeStep 사양 확인하기

예제1

import tf_agents
from tf_agents.environments import suite_gym

env_name = 'CartPole-v0'
env = suite_gym.load(env_name)

print(env.time_step_spec())
TimeStep(step_type=ArraySpec(shape=(), dtype=dtype('int32'), name='step_type'), reward=ArraySpec(shape=(), dtype=dtype('float32'), name='reward'), discount=BoundedArraySpec(shape=(), dtype=dtype('float32'), name='discount', minimum=0.0, maximum=1.0), observation=BoundedArraySpec(shape=(4,), dtype=dtype('float32'), name='observation', minimum=[-4.8000002e+00 -3.4028235e+38 -4.1887903e-01 -3.4028235e+38], maximum=[4.8000002e+00 3.4028235e+38 4.1887903e-01 3.4028235e+38]))

TF-Agents 환경 (env)의 time_step_spec() 메서드는 TimeStep의 사양을 반환합니다.

출력 결과를 보면, step_type은 정수형 (int32)의 스칼라 사양을 가지고, reward는 실수형 (float32)의 스칼라 사양을 가지는 것을 알 수 있습니다.

observation은 최대, 최소값의 범위를 갖는 실수형 (float32)의 어레이 형태임을 알 수 있습니다.



예제2

import tf_agents
from tf_agents.environments import suite_gym

env_name = 'CartPole-v0'
env = suite_gym.load(env_name)

# print(env.time_step_spec())
print('step_type spec:', env.time_step_spec().step_type)
print('reward spec:', env.time_step_spec().reward)
print('discount spec:', env.time_step_spec().discount)
print('observation spec:', env.time_step_spec().observation)
step_type spec: ArraySpec(shape=(), dtype=dtype('int32'), name='step_type')
reward spec: ArraySpec(shape=(), dtype=dtype('float32'), name='reward')
discount spec: BoundedArraySpec(shape=(), dtype=dtype('float32'), name='discount', minimum=0.0, maximum=1.0)
observation spec: BoundedArraySpec(shape=(4,), dtype=dtype('float32'), name='observation', minimum=[-4.8000002e+00 -3.4028235e+38 -4.1887903e-01 -3.4028235e+38], maximum=[4.8000002e+00 3.4028235e+38 4.1887903e-01 3.4028235e+38])

step_type, reward, discount, observation과 같이 각각의 속성을 통해서도 사양을 확인할 수 있습니다.

ArraySpec 객체는 NumPy Array, Scalar의 형태 (shape)와 자료형 (dtype)을 표시하기 위해 사용됩니다.

BoundedArraySpec는 최소, 최대값 범위를 갖는 ArraySpec입니다.




2) Action 사양 확인하기

예제

import tf_agents
from tf_agents.environments import suite_gym

env_name = 'CartPole-v0'
env = suite_gym.load(env_name)

print(env.action_spec())
BoundedArraySpec(shape=(), dtype=dtype('int64'), name='action', minimum=0, maximum=1)

강화학습의 환경 (Environment)과 에이전트 (Agent)는 행동 (Action)과 관찰 (Observation)을 통해 상호작용합니다.

action_spec()을 사용해서 이러한 행동 (Action)의 사양을 확인할 수 있습니다.



이전글/다음글