私は行動価値近似関数でq学習を実装しようとしています。私はアルゴリズムをテストするためにopenai-gymと "MountainCar-v0"環境を使用しています。私の問題は、収束せず、目標を全く見つけられないことです。関数近似とq学習
基本的に、近似は次の2つの機能を提供します:位置と速度、ワンホットエンコーディングの3つのアクションの1つ:0 - > [1,0,0]、1 - > [ 0,1,0]及び2 - > [0,0,1]である。出力は、特定の1つのアクションに対するアクション値近似Q_approx(s、a)です。
私は通常、入力が状態(2つの機能)であり、出力レイヤーが各アクションに対して1つの出力を含んでいることを知っています。私が見る大きな違いは、フィードフォワードパスを3回(各アクションに1つずつ)実行し、maxを取ることです。標準実装では、一度実行して出力を最大にします。
多分私の実装はまったく間違っていて、間違っていると思います。ここにコードを貼り付けると、混乱ですが、ちょうど少し実験しています:
import gym
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation
env = gym.make('MountainCar-v0')
# The mean reward over 20 episodes
mean_rewards = np.zeros(20)
# Feature numpy holder
features = np.zeros(5)
# Q_a value holder
qa_vals = np.zeros(3)
one_hot = {
0 : np.asarray([1,0,0]),
1 : np.asarray([0,1,0]),
2 : np.asarray([0,0,1])
}
model = Sequential()
model.add(Dense(20, activation="relu",input_dim=(5)))
model.add(Dense(10,activation="relu"))
model.add(Dense(1))
model.compile(optimizer='rmsprop',
loss='mse',
metrics=['accuracy'])
epsilon_greedy = 0.1
discount = 0.9
batch_size = 16
# Experience replay containing features and target
experience = np.ones((10*300,5+1))
# Ring buffer
def add_exp(features,target,index):
if index % experience.shape[0] == 0:
index = 0
global filled_once
filled_once = True
experience[index,0:5] = features
experience[index,5] = target
index += 1
return index
for e in range(0,100000):
obs = env.reset()
old_obs = None
new_obs = obs
rewards = 0
loss = 0
for i in range(0,300):
if old_obs is not None:
# Find q_a max for s_(t+1)
features[0:2] = new_obs
for i,pa in enumerate([0,1,2]):
features[2:5] = one_hot[pa]
qa_vals[i] = model.predict(features.reshape(-1,5))
rewards += reward
target = reward + discount*np.max(qa_vals)
features[0:2] = old_obs
features[2:5] = one_hot[a]
fill_index = add_exp(features,target,fill_index)
# Find new action
if np.random.random() < epsilon_greedy:
a = env.action_space.sample()
else:
a = np.argmax(qa_vals)
else:
a = env.action_space.sample()
obs, reward, done, info = env.step(a)
old_obs = new_obs
new_obs = obs
if done:
break
if filled_once:
samples_ids = np.random.choice(experience.shape[0],batch_size)
loss += model.train_on_batch(experience[samples_ids,0:5],experience[samples_ids,5].reshape(-1))[0]
mean_rewards[e%20] = rewards
print("e = {} and loss = {}".format(e,loss))
if e % 50 == 0:
print("e = {} and mean = {}".format(e,mean_rewards.mean()))
ありがとうございます!
私はこれまでの機能として行動を使っていると聞いたことがありますが、うまく機能していることは聞いていません。私はあなたがこれについての伝統と一緒に行くのが最善だと思って、行動をアウトプットとして使います。数学的には、これら2つのネットワークは全く異なるでしょう。 – Andnp