Fixed Q-targets
Bạn đang chuẩn bị huấn luyện Lunar Lander với fixed Q-targets. Trước hết, bạn cần khởi tạo cả online network (chọn hành động) và target network (dùng để tính TD-target).
Bạn cũng cần triển khai hàm update_target_network để dùng ở mỗi bước huấn luyện. Target network không được cập nhật bằng gradient descent; thay vào đó, update_target_network sẽ đẩy trọng số của nó tiến gần về Q-network một lượng nhỏ, giúp nó ổn định theo thời gian.
Lưu ý: chỉ trong bài này, bạn dùng một mạng rất nhỏ để có thể in và quan sát dễ dàng state dictionary. Mạng chỉ có một tầng ẩn kích thước 2; action space và state space cũng có kích thước 2.
Hàm print_state_dict() có sẵn trong môi trường để in state dict.
Bài tập này là một phần của khóa học
Deep Reinforcement Learning bằng Python
Hướng dẫn bài tập
- Lấy
.state_dict()cho cả target network và online network. - Cập nhật state dict của target network bằng cách lấy trung bình có trọng số giữa các tham số của online network và target network, dùng
taulàm trọng số cho online network. - Nạp (load) state dict đã cập nhật trở lại vào target network.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
def update_target_network(target_network, online_network, tau):
# Obtain the state dicts for both networks
target_net_state_dict = ____
online_net_state_dict = ____
for key in online_net_state_dict:
# Calculate the updated state dict for the target network
target_net_state_dict[key] = (online_net_state_dict[____] * ____ + target_net_state_dict[____] * ____)
# Load the updated state dict into the target network
target_network.____
return None
print("online network weights:", print_state_dict(online_network))
print("target network weights (pre-update):", print_state_dict(target_network))
update_target_network(target_network, online_network, .001)
print("target network weights (post-update):", print_state_dict(target_network))