def test_loss_masking(): weighted_loss = weighted_objective(objectives.get('mae')) shape = (3, 4, 2) X = np.arange(24).reshape(shape) Y = 2 * X # Normally the trailing 1 is added by standardize_weights weights = np.ones((3, )) mask = np.ones((3, 4)) mask[1, 0] = 0 out = K.eval( weighted_loss(K.variable(X), K.variable(Y), K.variable(weights), K.variable(mask)))
def test_loss_masking(): weighted_loss = weighted_objective(objectives.get('mae')) shape = (3, 4, 2) X = np.arange(24).reshape(shape) Y = 2 * X # Normally the trailing 1 is added by standardize_weights weights = np.ones((3,)) mask = np.ones((3, 4)) mask[1, 0] = 0 out = K.eval(weighted_loss(K.variable(X), K.variable(Y), K.variable(weights), K.variable(mask)))
def test_loss_masking_time(self): theano.config.mode = 'FAST_COMPILE' weighted_loss = weighted_objective(objectives.get('categorical_crossentropy')) shape = (3, 4, 2) X = np.arange(24).reshape(shape) Y = 2 * X weights = np.ones((3, 4, 1)) # Normally the trailing 1 is added by standardize_weights weights[0, 0] = 0 mask = np.ones((3, 4)) mask[1, 0] = 0 out = weighted_loss(X, Y, weights, mask).eval() weights[0, 0] = 1e-9 # so that nonzero() doesn't remove this weight out2 = weighted_loss(X, Y, weights, mask).eval() print(out) print(out2) assert abs(out - out2) < 1e-8
y_test = output.output(train=False) mask_train = output.output_mask(train=True) # None in this example mask_test = output.output_mask(train=False) # None in this example print("X_train:", P.pprint(X_train)) print("X_test:", P.pprint(X_test)) print("y_train:") print(P.debugprint(y_train)) print("y_test:") print(P.debugprint(y_test)) """loss""" loss = objectives.get("categorical_crossentropy") weighted_loss = models.weighted_objective(loss) y = K.placeholder(ndim=K.ndim(y_train)) weights = K.placeholder(ndim=1) train_loss = weighted_loss(y, y_train, weights, mask_train) test_loss = weighted_loss(y, y_test, weights, mask_test) _y_train = K.placeholder(ndim=3, name="y_train") _y_test = K.placeholder(ndim=3, name="y_test") _train_loss = weighted_loss(y, _y_train, weights, mask_train) _test_loss = weighted_loss(y, _y_test, weights, mask_test) print("train_loss:", P.pprint(_train_loss)) print("test_loss", P.pprint(_test_loss)) """categorical accuracy""" train_accuracy = K.mean(K.equal(K.argmax(y, axis=-1), K.argmax(y_train, axis=-1))) test_accuracy = K.mean(K.equal(K.argmax(y, axis=-1), K.argmax(y_test, axis=-1)))
def compile(self, optimizer, loss, class_mode="categorical", sample_weight_mode=None): '''Configure the learning process. # Arguments optimizer: str (name of optimizer) or optimizer object. See [optimizers](optimizers.md). loss: str (name of objective function) or objective function. See [objectives](objectives.md). class_mode: one of "categorical", "binary". This is only used for computing classification accuracy or using the predict_classes method. sample_weight_mode: if you need to do timestep-wise sample weighting (2D weights), set this to "temporal". "None" defaults to sample-wise weights (1D). ''' self.optimizer = optimizers.get(optimizer) self.sample_weight_mode = sample_weight_mode self.loss = objectives.get(loss) weighted_loss = weighted_objective(self.loss) # input of model self.X_train = self.get_input(train=True) self.X_test = self.get_input(train=False) self.single_y_train = self.get_output(train=True) self.single_y_test = self.get_output(train=False) self.diff_train = K.placeholder(ndim=1) self.diff_test = K.placeholder(ndim=1) self.y_train = K.concatenate([ K.dot(self.diff_train, self.single_y_train[:self.diff_train.shape[0]]), K.dot(self.diff_train, self.single_y_train[self.diff_train.shape[0]:]) ], axis=0) self.y_test = K.concatenate([ K.dot(self.diff_test, self.single_y_test[:self.diff_test.shape[0]]), K.dot(self.diff_test, self.single_y_test[self.diff_test.shape[0]:]) ], axis=0) # target of model self.y = K.placeholder(ndim=K.ndim(self.y_train)) if self.sample_weight_mode == 'temporal': self.weights = K.placeholder(ndim=2) else: self.weights = K.placeholder(ndim=1) if hasattr(self.layers[-1], "get_output_mask"): mask = self.layers[-1].get_output_mask() else: mask = None train_loss = weighted_loss(self.y, self.y_train, self.weights, mask) test_loss = weighted_loss(self.y, self.y_test, self.weights, mask) if class_mode == "categorical": train_accuracy = K.mean( K.equal(K.argmax(self.y, axis=-1), K.argmax(self.y_train, axis=-1))) test_accuracy = K.mean( K.equal(K.argmax(self.y, axis=-1), K.argmax(self.y_test, axis=-1))) elif class_mode == "binary": train_accuracy = K.mean(K.equal(self.y, K.round(self.y_train))) test_accuracy = K.mean(K.equal(self.y, K.round(self.y_test))) else: raise Exception("Invalid class mode:" + str(class_mode)) self.class_mode = class_mode for r in self.regularizers: train_loss = r(train_loss) updates = self.optimizer.get_updates(self.trainable_weights, self.constraints, train_loss) updates += self.updates if type(self.X_train) == list: train_ins = self.X_train + [self.diff_train, self.y, self.weights] test_ins = self.X_test + [self.diff_test, self.y, self.weights] assert type(self.X_test) == list predict_ins = self.X_test + [self.diff_test] else: train_ins = [self.X_train, self.diff_train, self.y, self.weights] test_ins = [self.X_test, self.diff_test, self.y, self.weights] predict_ins = [self.X_test, self.diff_test] self.__train = K.function(train_ins, [train_loss], updates=updates) self.__train_with_acc = K.function(train_ins, [train_loss, train_accuracy], updates=updates) self.__predict = K.function(predict_ins, [self.y_test], updates=self.state_updates) self.__test = K.function(test_ins, [test_loss], updates=self.state_updates) self.__test_with_acc = K.function(test_ins, [test_loss, test_accuracy], updates=self.state_updates) self._train = lambda rr: self.__train([r[0] for r in rr[:-1]] + [rr[-1]]) self._train_with_acc = lambda rr: self.__train_with_acc( [r[0] for r in rr[:-1]] + [rr[-1]]) self._predict = lambda rr: self.__predict([r[0] for r in rr]) self._test = lambda rr: self.__test([r[0] for r in rr[:-1]] + [rr[-1]]) self._test_with_acc = lambda rr: self.__test_with_acc( [r[0] for r in rr[:-1]] + [rr[-1]])
def compile(self, optimizer, loss, class_mode="categorical", sample_weight_mode=None): '''Configure the learning process. # Arguments optimizer: str (name of optimizer) or optimizer object. See [optimizers](optimizers.md). loss: str (name of objective function) or objective function. See [objectives](objectives.md). class_mode: one of "categorical", "binary". This is only used for computing classification accuracy or using the predict_classes method. sample_weight_mode: if you need to do timestep-wise sample weighting (2D weights), set this to "temporal". "None" defaults to sample-wise weights (1D). ''' self.optimizer = optimizers.get(optimizer) self.sample_weight_mode = sample_weight_mode self.loss = objectives.get(loss) weighted_loss = weighted_objective(self.loss) # input of model self.X_train = self.get_input(train=True) self.X_test = self.get_input(train=False) self.single_y_train = self.get_output(train=True) self.single_y_test = self.get_output(train=False) self.diff_train = K.placeholder(ndim=1) self.diff_test = K.placeholder(ndim=1) self.y_train = K.concatenate( [K.dot(self.diff_train, self.single_y_train[:self.diff_train.shape[0]]), K.dot(self.diff_train, self.single_y_train[self.diff_train.shape[0]:])], axis=0) self.y_test = K.concatenate( [K.dot(self.diff_test, self.single_y_test[:self.diff_test.shape[0]]), K.dot(self.diff_test, self.single_y_test[self.diff_test.shape[0]:])], axis=0) # target of model self.y = K.placeholder(ndim=K.ndim(self.y_train)) if self.sample_weight_mode == 'temporal': self.weights = K.placeholder(ndim=2) else: self.weights = K.placeholder(ndim=1) if hasattr(self.layers[-1], "get_output_mask"): mask = self.layers[-1].get_output_mask() else: mask = None train_loss = weighted_loss(self.y, self.y_train, self.weights, mask) test_loss = weighted_loss(self.y, self.y_test, self.weights, mask) if class_mode == "categorical": train_accuracy = K.mean(K.equal(K.argmax(self.y, axis=-1), K.argmax(self.y_train, axis=-1))) test_accuracy = K.mean(K.equal(K.argmax(self.y, axis=-1), K.argmax(self.y_test, axis=-1))) elif class_mode == "binary": train_accuracy = K.mean(K.equal(self.y, K.round(self.y_train))) test_accuracy = K.mean(K.equal(self.y, K.round(self.y_test))) else: raise Exception("Invalid class mode:" + str(class_mode)) self.class_mode = class_mode for r in self.regularizers: train_loss = r(train_loss) updates = self.optimizer.get_updates(self.trainable_weights, self.constraints, train_loss) updates += self.updates if type(self.X_train) == list: train_ins = self.X_train + [self.diff_train, self.y, self.weights] test_ins = self.X_test + [self.diff_test, self.y, self.weights] assert type(self.X_test) == list predict_ins = self.X_test + [self.diff_test] else: train_ins = [self.X_train, self.diff_train, self.y, self.weights] test_ins = [self.X_test, self.diff_test, self.y, self.weights] predict_ins = [self.X_test, self.diff_test] self.__train = K.function(train_ins, [train_loss], updates=updates) self.__train_with_acc = K.function(train_ins, [train_loss, train_accuracy], updates=updates) self.__predict = K.function(predict_ins, [self.y_test], updates=self.state_updates) self.__test = K.function(test_ins, [test_loss], updates=self.state_updates) self.__test_with_acc = K.function(test_ins, [test_loss, test_accuracy], updates=self.state_updates) self._train = lambda rr: self.__train([r[0] for r in rr[:-1]] + [rr[-1]]) self._train_with_acc = lambda rr: self.__train_with_acc([r[0] for r in rr[:-1]] + [rr[-1]]) self._predict = lambda rr: self.__predict([r[0] for r in rr]) self._test = lambda rr: self.__test([r[0] for r in rr[:-1]] + [rr[-1]]) self._test_with_acc = lambda rr: self.__test_with_acc([r[0] for r in rr[:-1]] + [rr[-1]])
def compile(self, optimizer="sgd", loss="mse", policy_rule="max", sample_weight_mode=None): """Initialize model weights and compile functions Notes ----- This function was modifed from `keras.models.compile` which is under MIT License. """ kmodel = self.keras_model kmodel.build() self.policy_rule = policies.get(policy_rule) self.optimizer = optimizers.get(optimizer) self.sample_weight_mode = sample_weight_mode self.loss = objectives.get(loss) weighted_loss = weighted_objective(self.loss) # input of model self.X_train = kmodel.get_input(train=True) self.X_test = kmodel.get_input(train=False) # calculate policy values values_train = kmodel.get_output(train=True) values_test = kmodel.get_output(train=False) self.y_train = self.policy_rule(values_train) self.y_test = self.policy_rule(values_test) # target of model self.y = K.placeholder(ndim=K.ndim(self.y_train)) if self.sample_weight_mode == 'temporal': self.weights = K.placeholder(ndim=2) else: self.weights = K.placeholder(ndim=1) if hasattr(kmodel.layers[-1], "get_output_mask"): mask = kmodel.layers[-1].get_output_mask() else: mask = None train_loss = weighted_loss(self.y, self.y_train, self.weights, mask) test_loss = weighted_loss(self.y, self.y_test, self.weights, mask) for r in kmodel.regularizers: train_loss = r(train_loss) updates = self.optimizer.get_updates(kmodel.trainable_weights, kmodel.constraints, train_loss) updates += kmodel.updates if type(self.X_train) == list: train_ins = self.X_train + [self.y, self.weights] test_ins = self.X_test + [self.y, self.weights] assert type(self.X_test) == list values_ins_test = self.X_test values_ins_train = self.X_train else: train_ins = [self.X_train, self.y, self.weights] test_ins = [self.X_test, self.y, self.weights] values_ins_test = [self.X_test] values_ins_train = [self.X_train] self._train = K.function(train_ins, [train_loss], updates=updates) self._values_train = K.function(values_ins_train, [values_train], updates=kmodel.state_updates) self._values_test = K.function(values_ins_test, [values_test], updates=kmodel.state_updates) # TODO: check if this is necessary self._test = K.function(test_ins, [test_loss], updates=kmodel.state_updates)