def train(self, training_files, learningrate=0.01, scaling=True, noise=False, verbose=True): print "building dataset..." ds = SupervisedDataSet(SensorModel.array_length(self.sensor_ids), 1) # read training file line, create sensormodel object, do backprop a = None s = None for logfile in training_files: print "loading file", logfile with open(logfile) as f: for line in f: if line.startswith("Received:"): s = SensorModel(string=line.split(' ', 1)[1]) elif line.startswith("Sending:"): a = Actions.from_string(string=line.split(' ', 1)[1]) if s is not None and a is not None: ds.addSample(inp=s.get_array(self.sensor_ids), target=a[self.action_ids[0]]) if noise: # add the same training sample again but with noise in the sensors s.add_noise() ds.addSample(inp=s.get_array(self.sensor_ids), target=a[self.action_ids[0]]) s = None a = None print "dataset size:", len(ds) if scaling: print "scaling dataset" self.scaler_input = StandardScaler(with_mean=True, with_std=False).fit(ds.data['input']) ds.data['input'] = self.scaler_input.transform(ds.data['input']) ds.data['target'] = ds.data['target'] #self.trainer = BackpropTrainer(self.net, learningrate=learningrate, verbose=verbose) self.trainer = RPropMinusTrainer(self.net, verbose=verbose, batchlearning=True) print "training network..." self.trainer.trainUntilConvergence(dataset=ds, validationProportion=0.25, maxEpochs=10, continueEpochs=2)
def get_action(self, sensors): a = Actions(accel=self.default_accel) if self.steering is not None: a.steering = self.steering.get_action(sensors) if self.accel is not None: a.accel = self.accel.get_action(sensors) elif self.max_speed is not None and abs(sensors['speedX']) > self.max_speed: a.accel = 0.05 if self.gear is not None: a.gear = self.gear.get_action(sensors) else: a.gear = self.update_gear(sensors) a.clutch = self.update_clutch() if self.brake is not None: a.brake = self.brake.get_action(sensors) * self.brake_factor return a