我试图实施CNN来玩游戏。 我和theano / lasagne一起使用python。我已经建立了网络,现在正在研究如何训练它。
所以现在我有一批32个州,对于该批次中的每个州,该行动的行动和预期奖励。
现在我如何训练网络,以便了解这些状态下的这些行为会带来这些奖励?
编辑:澄清我的问题。
以下是我的完整代码:http://pastebin.com/zY8w98Ng 蛇导入:http://pastebin.com/fgGCabzR
我遇到这个问题:
def _train(self):
# Prepare Theano variables for inputs and targets
input_var = T.tensor4('inputs')
target_var = T.ivector('targets')
states = T.tensor4('states')
print "sampling mini batch..."
# sample a mini_batch to train on
mini_batch = random.sample(self._observations, self.MINI_BATCH_SIZE)
# get the batch variables
previous_states = [d[self.OBS_LAST_STATE_INDEX] for d in mini_batch]
actions = [d[self.OBS_ACTION_INDEX] for d in mini_batch]
rewards = [d[self.OBS_REWARD_INDEX] for d in mini_batch]
current_states = np.array([d[self.OBS_CURRENT_STATE_INDEX] for d in mini_batch])
agents_expected_reward = []
# print np.rollaxis(current_states, 3, 1).shape
print "compiling current states..."
current_states = np.rollaxis(current_states, 3, 1)
current_states = theano.compile.sharedvalue.shared(current_states)
print "getting network output from current states..."
agents_reward_per_action = lasagne.layers.get_output(self._output_layer, current_states)
print "rewards adding..."
for i in range(len(mini_batch)):
if mini_batch[i][self.OBS_TERMINAL_INDEX]:
# this was a terminal frame so need so scale future reward...
agents_expected_reward.append(rewards[i])
else:
agents_expected_reward.append(
rewards[i] + self.FUTURE_REWARD_DISCOUNT * np.max(agents_reward_per_action[i].eval()))
# figure out how to train the model (self._output_layer) with previous_states,
# actions and agent_expected_rewards
我想使用previous_states,actions和agent_expected_rewards更新模型,以便它了解这些操作会带来这些奖励。
我希望它看起来像这样:
train_model = theano.function(inputs=[input_var],
outputs=self._output_layer,
givens={
states: previous_states,
rewards: agents_expected_reward
expected_rewards: agents_expected_reward)
我只是不知道这些赠品会如何影响模型,因为在构建网络时我没有指定它们。我也无法在theano和lasagne文档中找到它。
那么我该如何更新模型/网络以便它能够学习'。
如果仍不清楚,请评论仍需要哪些信息。我现在已经试图解决这个问题几天了。
答案 0 :(得分:1)
完成文档后,我终于找到了答案。我以前在错误的地方寻找过。
network = self._output_layer
prediction = lasagne.layers.get_output(network)
loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
loss = loss.mean()
params = lasagne.layers.get_all_params(network, trainable=True)
updates = lasagne.updates.sgd(loss, params, self.LEARN_RATE)
givens = {
states: current_states,
expected: agents_expected_reward,
real_rewards: rewards
}
train_fn = theano.function([input_var, target_var], loss,
updates=updates, on_unused_input='warn',
givens=givens,
allow_input_downcast='True')
train_fn(current_states, agents_expected_reward)