def train(self,x,y,params=None,randomize=True, split_sizes=None, batch_size=None): if params is None: self.params = NeuralNet.default_params else: for param in NeuralNet.default_params: if(param not in params): params[param] = NeuralNet.default_params[param] self.params = params #prepare the data (x,y, val_in,val_out, test_in,test_out, do_validation,do_test) =\ self.__init_data(x, y, split_sizes, randomize) self.y_ranges, self.y_mins = dp.get_ranges(y) self.x_ranges, self.x_mins = dp.get_ranges(x) x_c = dp.map_to_ranges(x,self.x_ranges,self.x_mins) y_c = dp.map_to_ranges(y,self.y_ranges,self.y_mins) #input size is number of features num_features = x.shape[1] num_output = y.shape[1] #default batch size is the whole train dataset #self.ranges_y = d n_samples = len(x) if(batch_size is None): batch_size = n_samples self.__build_architecture(num_features,num_output) self.sigma = self.params["sigma"] self.initial_lambda = self.lambda_ = self.params["lambda"] self.lambda_bar = 0.0 self.max_epochs = self.params["max_epochs"] self.goal = self.params["goal"] output = self.__predict(x_c, self.layers) self.error = self.get_mse(output, y_c) / len(y_c) self.reciprocals = -self.compute_gradients(x_c,y_c,self.layers) self.search_dir = np.copy(self.reciprocals) self.pSqNorm = 0.0 self.success = True self.delta = 0.0 self.k = self.num_params i_it = 0 while(i_it < self.max_epochs and self.error > self.goal): self.train_iteration(x_c,y_c) print self.error i_it +=1
def predict(self,x): x = dp.map_to_ranges(x,self.x_ranges,self.x_mins) return dp.unmap_y(self.__predict(x,self.layers),self.y_ranges)