Код: Выделить всё
state_tensor = torch.FloatTensor(state).unsqueeze(0) # Ensure state is a 2D tensor with shape (1, 768)
# Get Q-values for the current state
q_values = env.q_network(torch.FloatTensor(state_tensor))
# Mask out invalid actions
valid_actions = env.get_valid_actions() # This method should return a binary mask of valid actions
# Convert valid_actions to a tensor and reshape to match q_values shape
valid_actions = torch.tensor(valid_actions, dtype=torch.bool).unsqueeze(0) # Now valid_actions has shape [1, 4672]
# Assuming q_values is obtained from the neural network output
#q_values = torch.tensor(q_values, dtype=torch.float32) # Convert q_values to tensor if it isn't already
# Verify the shapes
print(f'q_values shape: {q_values.shape}') # Expected output: [1, 4672]
print(f'valid_actions shape: {valid_actions.shape}') # Expected output: [1, 4672]
# Apply the mask
q_values[~valid_actions] = float('-inf')
IndexError: форма маски [1, 4672] по индексу 1 не соответствует форме индексированного тензора [1, 1, 4672] по индексу 1
по какой-то причине размеры тензора всегда равны размеру маски +1. Я понимаю, что это, вероятно, из-за «unsqueeze(0)», но я пытался создавать бесконечные комбинации тензор и маска одинаковых размеров, но это просто не сработает, в конце должно быть в формате [1, 4672]
Код: Выделить всё
#q_values = torch.tensor(q_values, dtype=torch.float32) # Convert q_values to tensor if it isn't alreadyКод: Выделить всё
valid_actions = torch.tensor(valid_actions, dtype=torch.bool).unsqueeze(0) # Now valid_actions has shape [1, 4672]Подробнее здесь: https://stackoverflow.com/questions/786 ... -different