@@ -0,0 +1,2657 @@
+/*******************************************************************************%0A * DIANNE  - Framework for distributed artificial neural networks%0A * Copyright (C) 2015  iMinds - IBCN - UGent%0A *%0A * This file is part of DIANNE.%0A *%0A * DIANNE is free software: you can redistribute it and/or modify%0A * it under the terms of the GNU Affero General Public License as%0A * published by the Free Software Foundation, either version 3 of the%0A * License, or (at your option) any later version.%0A *%0A * This program is distributed in the hope that it will be useful,%0A * but WITHOUT ANY WARRANTY; without even the implied warranty of%0A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A * GNU Affero General Public License for more details.%0A *%0A * You should have received a copy of the GNU Affero General Public License%0A * along with this program.  If not, see %3Chttp://www.gnu.org/licenses/%3E.%0A *%0A * Contributors:%0A *     Tim Verbelen, Steven Bohez%0A *******************************************************************************/%0Apackage be.iminds.iot.dianne.rl.agent.strategy;%0A%0Aimport java.util.Map;%0A%0Aimport be.iminds.iot.dianne.api.nn.NeuralNetwork;%0Aimport be.iminds.iot.dianne.api.rl.agent.ActionStrategy;%0Aimport be.iminds.iot.dianne.api.rl.agent.AgentProgress;%0Aimport be.iminds.iot.dianne.api.rl.environment.Environment;%0Aimport be.iminds.iot.dianne.nn.util.DianneConfigHandler;%0Aimport be.iminds.iot.dianne.rl.agent.strategy.config.BoltzmannConfig;%0Aimport be.iminds.iot.dianne.tensor.ModuleOps;%0Aimport be.iminds.iot.dianne.tensor.Tensor;%0Aimport be.iminds.iot.dianne.tensor.TensorOps;%0A%0A/**%0A * Discrete sampling ActionStrategy that takes the output of a (discrete) policy network%0A * ending with a softmax/logsoftmax module and samples an actual discrete action from it.  %0A * @author tverbele%0A *%0A */%0Apublic class DiscreteSamplingActionStrategy implements ActionStrategy %7B%0A%09%0A%09private BoltzmannConfig config;%0A%09private NeuralNetwork nn;%0A%09%0A%09@Override%0A%09public void setup(Map%3CString, String%3E config, Environment env, NeuralNetwork... nns) throws Exception %7B%0A%09%09this.config = DianneConfigHandler.getConfig(config, BoltzmannConfig.class);%0A%09%09this.nn = nns%5B0%5D;%0A%09%7D%0A%0A%09@Override%0A%09public AgentProgress processIteration(long i, Tensor state) throws Exception %7B%0A%09%09Tensor output = nn.forward(state);%0A%09%09%0A%09%09Tensor action = new Tensor(output.size());%0A%09%09action.fill(0);%0A%09%09%0A%09%09if(TensorOps.max(output) %3C 0)%7B%0A%09%09%09// assume logsoftmax output, take exp%0A%09%09%09output = TensorOps.exp(output, output);%0A%09%09%7D%0A%09%09%0A%09%09double s = 0, r = Math.random();%0A%09%09int a = 0;%0A%09%09%0A%09%09while((s += output.get(a)) %3C r && a %3C action.size())%0A%09%09%09a++;%0A%09%09%0A%09%09action.set(1, a);%0A%09%09%0A%09%09return new AgentProgress(i, action);%0A%09%7D%0A%0A%7D%0A

add discrete sampling strategy that samples from (log)softmax output