Пользовательская сеть и политика в Stable-Baselines3Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Пользовательская сеть и политика в Stable-Baselines3

Сообщение Anonymous »

Я пытаюсь создать небольшой рабочий пример того, как использовать пространства действий MultiDiscrete вместе с пространством наблюдения Box. Одна из проблем, с которой я столкнулся, заключается в том, что измерение, возвращаемое при использовании обычной политики, не соответствует измерениям Box. Базовая политика возвращает что-то размером 25, тогда как мне нужно что-то размером (5,5).
Я попытался облегчить эту проблему, создав собственную «политику» (на самом деле сетевую ), где на последнем этапе я изменил форму вывода до (5,5), а не до 25. Это привело к множеству проблем. Я попытался прочитать документацию о том, как создавать собственные политики; однако я не могу найти проблему хоть убей.

[*]Я пытался использовать policy_kwargs; однако я не знаю, как написать, что NN следует изменить.

[*]Я пытался использовать BaseFeaturesExtractor, но безуспешно. а также.

[*]Различные комбинации 1 и 2.


Я включил некоторые сообщения об ошибках, которые я получаю при различных попытках. Кто-нибудь знает, чего мне не хватает? Это что-то совершенно фундаментальное, что я неправильно понял?
import numpy as np
import gym
import torch.nn as nn
import torch as th
from stable_baselines3 import PPO
from stable_baselines3.common.torch_layers import BaseFeaturesExtractor # don't know if this is necessary

# -------- Attempt using BaseFeaturesExtractor
# class CustomPolicy(BaseFeaturesExtractor): # Don't know if BaseFeaturesExtractor is correct
# def __init__(self, observation_space, action_space, features_dim: int = 25): # Features should perhaps be (5,5)
# super().__init__(observation_space, features_dim)
# --------

# Define a custom neural network architecture
class CustomPolicy():
def __init__(self, observation_space, action_space):
super().__init__()
# Define the layers of the neural network

self.fc1 = nn.Linear(observation_space.shape[0], 64)
self.fc2 = nn.Linear(64, 64)
self.fc3 = nn.Linear(64, action_space.shape[0])

# Reshape the output to match the Box observation space shape

def forward(self, x):
x = nn.functional.relu(self.fc1(x))
x = nn.functional.relu(self.fc2(x))
x = self.fc3(x)
x = th.reshape(x, (5, 5))
return x

# Define the grid world environment
class GridWorldEnv(gym.Env):
def __init__(self):
self.observation_space = gym.spaces.Box(low=0, high=1, shape=(5, 5), dtype=np.float32)
self.action_space = gym.spaces.MultiDiscrete([5, 3]) # 5 movement directions, 3 movement distances

self.state = np.zeros((5, 5))
self.state[0, 0] = 1 # Start location
self.goal = (4, 4) # Goal location
self.steps = 0
self.state.flatten()

def reset(self):
self.state = np.zeros((5, 5))
self.state[0, 0] = 1 # Start location
self.goal = (4, 4) # Goal location
self.steps = 0
return self.state.flatten()

def step(self, action):
direction, distance = action
reward = -1
done = False

# Calculate the movement offset based on the selected direction and distance
if direction == 0:
offset = (distance, 0)
elif direction == 1:
offset = (-distance, 0)
elif direction == 2:
offset = (0, distance)
elif direction == 3:
offset = (0, -distance)
else:
offset = (0, 0)

# Calculate the new position based on the current position and movement offset
current_pos = np.argwhere(self.state == 1)[0]
new_pos = tuple(np.clip(current_pos + np.array(offset), 0, 4))

# Update the state with the new position
self.state[current_pos] = 0
self.state[new_pos] = 1

# Check if the agent has reached the goal
if np.argmax(self.state) == np.ravel_multi_index(self.goal, self.state.shape):
reward = 10
done = True

# Increment step count and check if episode should end
self.steps += 1
if self.steps >= 50:
done = True

return self.state, reward, done, {}

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# Create an environment with the CustomEnv environment
env = GridWorldEnv()

# Create policy
policy = CustomPolicy(env.observation_space, env.action_space)

# Create a PPO agent with the CustomPolicy
model = PPO(policy=policy, env=env, verbose=1)
# --------- TypeError: 'CustomPolicy' object is not callable

# --------- Attempt at using policy_kwargs
# policy_kwargs = dict(activation_fn=th.nn.ReLU,
# net_arch=dict(pi=[32, 32], vf=[32, 32]))
# model = PPO("MlpPolicy", env=env, verbose=1, policy_kwargs=policy_kwargs)
# --------- ValueError: could not broadcast input array from shape (25,) into shape (5,5)

# --------- Attempt at using policy_kwargs with custom policy
# policy_kwargs = dict(
# features_extractor_class=CustomPolicy,
# features_extractor_kwargs=dict(features_dim=25), # should perhaps be (5,5)
# )
# model = PPO(policy=policy, env=env, verbose=1, policy_kwargs=policy_kwargs)
# --------- TypeError: CustomPolicy.forward() got an unexpected keyword argument 'use_sde'

# Train the agent for 1000 steps
model.learn(total_timesteps=1000)


Подробнее здесь: https://stackoverflow.com/questions/754 ... baselines3
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»