일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- BART 논문리뷰
- 정책기반 agent
- BERT란
- 길찾기
- ImageNet Classification with Deep ConvolutionalNeural Networks 리뷰
- BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding 논문리뷰
- CNN 논문리뷰
- T5 논문 리뷰
- Attention Is All You Need
- Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer 리뷰
- NLP 논문 리뷰
- Attention Is All You Need 리뷰
- RuntimeError: DataLoader worker (pid(s) ) exited unexpectedly
- Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer
- BERT 사용방법
- attention 설명
- 다양한 모듈에서 log쓰기
- The Natural Language Decathlon:Multitask Learning as Question Answering
- 바닥부터 배우는 강화 학습
- 뉴텝스 400
- Multi Task Learning Objectives for Natural Language Processing
- Zero-shot Generalization in Dialog State Tracking through GenerativeQuestion Answering
- hugging face tokenizer에서 special case 추가하기
- MMTOD
- Multi Task Learning Objectives for Natural Language Processing 리뷰
- Evaluate Multiwoz
- A Neural Attention Model for Abstractive Sentence Summarization
- TOD 논문리뷰
- BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding 리뷰
- UBAR: Towards Fully End-to-End Task-Oriented Dialog System with GPT-2
Archives
- Today
- Total
one by one ◼◻◼◻
CH9. 정책기반 Agent 본문
바닥부터 배우는 강화 학습(노승은) 을 읽고 정리한 페이지 내용입니다.
코드는 https://github.com/seungeunrho를 참고하였습닌다.
정책기반 Agent 예시 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
import gym
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical
#Hyperparameters
learning_rate = 0.0002
gamma = 0.98
class Policy(nn.Module):
def __init__(self):
super(Policy, self).__init__()
self.data = []
self.fc1 = nn.Linear(4, 128)
self.fc2 = nn.Linear(128, 2)
self.optimizer = optim.Adam(self.parameters(), lr=learning_rate)
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.softmax(self.fc2(x), dim=0)
return x
def put_data(self, item):
self.data.append(item)
def train_net(self): # 에피소드가 끝나면 모인 data로 업데이트, data는 에피소드마다 초기화
R = 0
self.optimizer.zero_grad()
for r, prob in self.data[::-1]: # 뒤에서 부터 하나씩 꺼냅니다.
R = r + gamma * R # 뒤에서 부터 꺼내면서 총 리워드 계산
loss = -torch.log(prob) * R # - 는 gardient ascent라서 붙여준다. 비슷해 져야 하거든
loss.backward() # 자동미분
self.optimizer.step() # 고! 실행!
self.data = []
def main():
env = gym.make('CartPole-v1')
pi = Policy()
score = 0.0
print_interval = 20
for n_epi in range(10000):
s = env.reset()
done = False
while not done: # CartPole-v1 forced to terminates at 500 step.
prob = pi(torch.from_numpy(s).float()) # 상태에 대한 action의 확률
m = Categorical(prob)
a = m.sample() # action sampling
s_prime, r, done, info = env.step(a.item()) # 다음 한 액션 진행
pi.put_data((r,prob[a])) # 바로받는 리워드와 a의 확률
s = s_prime # 이동합니다
score += r # 점수는 계속 더해줍니다.
pi.train_net() # 에피소드가 끝나면 업데이트 해줍니다.
if n_epi%print_interval==0 and n_epi!=0:
print("# of episode :{}, avg score : {}".format(n_epi, score/print_interval))
score = 0.0
env.close()
if __name__ == '__main__':
main()
|
cs |
Comments