Course materials · Reinforcement Learning for Electrical Engineering

Week 1: Introduction to Reinforcement Learning

English translation

[What is Reinforcement Learning?]

1. Definition

2. Main components

3. The objective of reinforcement learning

4. Policy

5. Development environment setup (Google Colab)

[Activity 1: PyTorch tensor basics]

1. Create tensors

import torch

x = torch.tensor([1.0, 2.0, 3.0])
print(x)
tensor([1., 2., 3.])

2. Tensor operations

y = x + 2
print(y)
tensor([3., 4., 5.])

3. Check tensor size

print(x.shape)
torch.Size([3])

4. Check tensor data types

print(x.dtype)
torch.float32

5. Check the device used for tensor operations

print(x.device)
cpu

6. Check GPU availability and move tensors

if torch.cuda.is_available():
  device = torch.device('cuda')
  print('GPU 사용 가능')
else:
  device = torch.device('cpu')
  print('GPU 사용 불가능')

x = x.to(device)
print(x.device)
GPU 사용 불가능
cpu

7. Access and modify tensor elements

print(x[0])

x[0] = 10.0
print(x)
tensor(1.)
tensor([10.,  2.,  3.])

8. Change tensor dimensions

x = x.view(1, 3)  # Reshape to (1, 3)
print(x)
print(x.shape)
tensor([[10.,  2.,  3.]])
torch.Size([1, 3])

9. Tensor element sums and maxima

print(torch.sum(x))

print(torch.max(x))
tensor(15.)
tensor(10.)

10. Convert tensors to NumPy arrays and NumPy arrays to tensors

x_np = x.cpu().numpy()
print(x_np)

x_tensor = torch.from_numpy(x_np)
print(x_tensor)
[[10.  2.  3.]]
tensor([[10.,  2.,  3.]])

[Exercise 2: take random actions in CartPole-v1]

1. Import libraries

import gymnasium as gym
import random

2. Create the environment

env = gym.make('CartPole-v1')

3. Repeat episodes

for episode in range(10):  # Run 10 episodes
    observation, _ = env.reset()  # Reset the environment
    done = False  # Initialize the variable indicating whether the episode has ended
    total_reward = 0  # Initialize the total reward for the episode

    while not done:  # Repeat until the episode ends
        # Select a random action (0: left, 1: right)
        action = env.action_space.sample()

        # Apply the selected action to the environment and return the next state, reward, episode end status, and additional information
        next_observation, reward, terminated, truncated, info = env.step(action)
        done = terminated or truncated

        total_reward += reward  # Accumulate rewards over the episode
        observation = next_observation  # Update to the next state

        # Render the environment (comment out to disable rendering)
        # env.render()

    # Print the total reward when each episode ends
    print(f"Episode {episode + 1}: Total reward = {total_reward}")
Episode 1: Total reward = 30.0
Episode 2: Total reward = 19.0
Episode 3: Total reward = 38.0
Episode 4: Total reward = 19.0
Episode 5: Total reward = 14.0
Episode 6: Total reward = 18.0
Episode 7: Total reward = 11.0
Episode 8: Total reward = 33.0
Episode 9: Total reward = 51.0
Episode 10: Total reward = 33.0

4. Close the environment

env.close()