def load_models(self, load_train_logits=True, load_test_logits=True): self.classifier_model = functions.get_model(self.model_name, optim = functions.get_optim(self.meta_data["optim"], self.meta_data["optim_config"])) self.classifier_model.load_weights("%s/classifiermodel-weights-before.h5"%self.base_path) # self.classifier_model = tf.keras.models.load_model("%s/classifiermodel-weights-before.h5"%self.base_path) # get outputs from a certain layer #self.sub_model = tf.keras.Model(inputs=self.classifier_model.inputs, outputs=self.classifier_model.get_layer(self.layer_key).output) #self.embedding_config["embedding_size"] = self.sub_model.output.shape[1] self.init_logits(train=load_train_logits, test=load_test_logits) self.embedder_model = functions.get_model(self.embedding_approach, optim=self.embedding_optim, batch_size=self.embedding_batch_size, epochs=self.embedding_epochs, save_path="%s/embeddermodel"%self.base_path, config=self.embedding_config)
def fit_embedding(self): self.init_logits() self.embedder_model = functions.get_model(self.embedding_approach, verbose=self.verbose, optim=self.embedding_optim, batch_size=self.embedding_batch_size, epochs=self.embedding_epochs, config=self.embedding_config) self.embedder_model.fit(self.logits_train[::self.embedding_subset]) # with tensorflow tensors, there is an indexing error... self.embedder_model.save("%s/embeddermodel"%self.base_path)
def main(): start_time = time.time() in_arg = args_input() # get the data dataset, dataloaders = functions.load_data() # get the model model = functions.get_model(in_arg.arch) # get the classifier, criterion, optimizer, device model, classifier, criterion, optimizer, device = network_param( model, in_arg.arch, in_arg.hidden_units, in_arg.learning_rate, in_arg.gpu) model.to(device) print('Training model...') functions.train_model(model, criterion, optimizer, device, dataloaders['train'], dataloaders['val'], in_arg.epochs) # validation on test data print('\nGetting results on test data accuracy...') functions.test_model(model, criterion, device, dataloaders['test']) # saving checkpoint on trained model print('\nSaving checkpoint for current trained model...') functions.save_checkpoint(model, optimizer, dataset, in_arg.arch, in_arg.epochs, in_arg.save_dir) print('Checkpoint saved!')
def main(): # set these! matrix_name = "reciprocal_pop_conf" save_model = True matrix_filename = f'data/matrices/{matrix_name}_matrix.npz' matrix = sp.load_npz(matrix_filename) # ratio = 65464776.0 / matrix.sum() # total interactions # alpha, reg, factors = round(3360 * ratio)/40, 1.19, 128 alpha, reg, factors = 107480, 4, 128 # set this to true if you want to save the model print( f'matrix: {matrix_name}, alpha: {alpha}, reg: {reg}, factors: {factors}' ) os.environ['MKL_NUM_THREADS'] = '1' os.environ['MKL_DEBUG_CPU_TYPE'] = '5' sh, mb = 2641, 22530 train, test, masked = functions.get_train_test_masked(matrix) print(f'alpha: {alpha}, reg: {reg}, factors: {factors}') model_name = f'data/models/{matrix_name}_a{alpha}_r{reg}_f{factors}_model.pickle' try: with open(model_name, "rb") as input_file: model = pickle.load(input_file) except FileNotFoundError: model = functions.get_model(train, alpha=alpha, reg=reg, factors=factors) if save_model: try: with open(model_name, 'wb') as output_file: pickle.dump(model, output_file) except FileNotFoundError: os.mkdir('data/models') with open(model_name, 'wb') as output_file: pickle.dump(model, output_file) del train pop_gap, auc, sh_auc, mb_auc, lt_auc = functions.score_model( model, test, masked, sh, mb) results = [ matrix_name, alpha, reg, factors, pop_gap, auc, sh_auc, mb_auc, lt_auc ] with open('data/results.csv', 'a') as result_file: wr = csv.writer(result_file, dialect='excel') wr.writerow(results)
def fit_basemodel(self): self.classifier_model = functions.get_model(self.model_name, optim=functions.get_optim(self.meta_data["optim"], self.meta_data["optim_config"])) history = self.classifier_model.fit( self.X_train, self.y_train, batch_size=self.batch_size, epochs=self.epochs, shuffle=True, verbose=self.verbose ) self.classifier_model.save("%s/classifiermodel-weights-before.h5"%self.base_path) # self.classifier_model.save_weights("%s/classifiermodel-weights-before.hdf5"%self.base_path) self.save_dict("%s/classifiermodel-history-before.json"%self.base_path, history.history, to_float=True)
def fit_intervention(self): if self.classifier_model is None: print("classifier model was None; load model from ", "%s/classifiermodel-weights-before.h5"%self.base_path, "instead") self.classifier_model = functions.get_model(self.model_name, optim = functions.get_optim(self.meta_data["optim"], self.meta_data["optim_config"])) self.classifier_model.load_weights("%s/classifiermodel-weights-before.h5"%self.base_path) # self.classifier_model = tf.keras.models.load_model("%s/classifiermodel-weights-before.h5"%self.base_path) if self.embedder_model is None: print("embedder model was None; load model from ", "%s/embeddermodel"%self.base_path, "instead") self.embedder_model = functions.get_model(self.embedding_approach, optim = functions.get_optim(self.meta_data["optim"], self.meta_data["optim_config"]), batch_size=self.embedding_batch_size, epochs=self.embedding_epochs, save_path="%s/embeddermodel"%self.base_path, config=self.embedding_config) # --Train classifier in conjunction with the shifted embedding loss embedding_encoder = self.embedder_model.encoder embedding_encoder = tf.keras.Model(embedding_encoder.inputs, embedding_encoder.outputs, name="embedder") # rename the model; name is later needed embedding_encoder.trainable = False flat = nn.Flatten()(self.classifier_model.get_layer(self.layer_key).output) # if conv layer is used as latent layer, we need to flatten the latent space... embedder_out = embedding_encoder(flat) #embedder_out = embedding_encoder(self.classifier_model.get_layer(self.layer_key).output) classifier_embedder_model = tf.keras.Model(self.classifier_model.input, [self.classifier_model.output, embedder_out]) classifier_embedder_model.compile( optimizer= functions.get_optim(self.meta_data["optim"], self.meta_data["optim_config"]), loss={"classifier": tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), "embedder": tf.keras.losses.MeanSquaredError()}, loss_weights={"classifier": (1.0 - self.embedding_weight)*2, "embedder": self.embedding_weight*2}, metrics={"classifier":['accuracy']} ) shifted_train = self.get_shifted_data() checkpoint = tf.keras.callbacks.ModelCheckpoint("%s/classifiermodel-weights-after-embedding-epoch{epoch:02d}.hdf5"%self.base_path, monitor='loss', save_weights_only=True, verbose=self.verbose, save_best_only=False, save_freq="epoch") history = classifier_embedder_model.fit( x=self.X_train, y={"classifier": self.y_train, "embedder": shifted_train}, batch_size=self.batch_size, epochs=self.post_epochs, shuffle=True, callbacks=[checkpoint], verbose=self.verbose ) classifier_embedder_model.save("%s/classifiermodel-weights-after-embedding.h5"%self.base_path) # classifier_embedder_model.save_weights("%s/classifiermodel-weights-after-embedding.hdf5"%self.base_path) self.save_dict("%s/classifiermodel-history-after-embedding.json"%self.base_path, history.history, to_float=True) # --train classifier as usual, such that it is comparable with the combined model self.classifier_model = functions.get_model(self.model_name, optim = functions.get_optim(self.meta_data["optim"], self.meta_data["optim_config"])) # need to reinitialize the compiler, since we also do this with the "classifier_embedder_model -> comparability self.classifier_model.load_weights("%s/classifiermodel-weights-before.h5"%self.base_path) # self.classifier_model = tf.keras.models.load_model("%s/classifiermodel-weights-before.h5"%self.base_path) checkpoint = tf.keras.callbacks.ModelCheckpoint("%s/classifiermodel-weights-after-baseline-epoch{epoch:02d}.hdf5"%self.base_path, monitor='loss', save_weights_only=True, verbose=self.verbose, save_best_only=False, save_freq="epoch") history = self.classifier_model.fit( x=self.X_train, y=self.y_train, batch_size=self.batch_size, epochs=self.post_epochs, shuffle=True, callbacks=[checkpoint], verbose=self.verbose ) self.classifier_model.save("%s/classifiermodel-weights-after-baseline.h5"%self.base_path) # self.classifier_model.save_weights("%s/classifiermodel-weights-after-baseline.hdf5"%self.base_path) self.save_dict("%s/classifiermodel-history-after-baseline.json"%self.base_path, history.history, to_float=True)
raise Exception("Models trained using the inf norm can only be " "evaluated using the inf norm") p_norm_eval = to_p_norm(args.eval_norm) p_norm_model = to_p_norm(args.model_norm) # Handle_ dataset (x_train, y_train), (x_test, y_test) = get_data(args.dataset) data_shape = tuple(x_train.shape[1:]) data_size = np.product(data_shape) # Load model model = get_model(args.model, data_shape, n_classes=10, number_prototypes=args.prototypes, p_norm=p_norm_model, batch_size=args.batch_size, negated_dissimilarities=True, weights_provided=True, number_tangents=args.tangents) model.load_weights(args.weights_path) # Determine model accuracy test_predictions = model.predict(x_test, verbose=True) n_test_correct = np.sum( np.squeeze(np.argmax(test_predictions, 1)) == np.squeeze( np.argmax(y_test, 1))) test_acc = float(n_test_correct / x_test.shape[0]) # Determine model certificates test_cert = calculate_certificates(model, (x_test, y_test),
y.append(float(yy)) x, y = fun.np.array(x), fun.np.array(y) # Perform normalization x = (x - x.mean()) / x.std() # Scatter dataset plt.figure() plt.scatter(x, y, c="r", s=20) plt.show() # Set degrees test_set = (1, 4, 10) # show line's x s_x = fun.np.linspace(-2, 4, 100) # Visualize results plt.scatter(x, y, c="g", s=20) for d in test_set: s_y = fun.get_model(d, x, y, s_x)() plt.plot(s_x, s_y, label="degree = {}".format(d)) plt.xlim(-2, 4) plt.ylim(1e5, 8e5) plt.legend() plt.show() #cost print('cost') for d in test_set: print(fun.get_cost(d, x, y))
action='store_true', help="If set, the exact plot from the paper will be " "replicated.") parser.add_argument("-n", "--number_of_samples", type=int, default=10000) args = parser.parse_args() if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) (x_train, y_train), (x_test, y_test) = get_data('mnist') n = args.number_of_samples model = get_model('glvq', (28, 28, 1), n_classes=10, number_prototypes=128, p_norm=np.inf, batch_size=128, negated_dissimilarities=False, weights_provided=True, number_tangents=-1) # GLVQ LOSS model.load_weights("weight_files/GLVQ/mnist/linf_trained/glvq_loss.h5") y_pred = model.predict(x_test[:n], verbose=True) margins_glvq = hypothesis_margin(np.inf, y_pred, y_test[:n]) acc_glvq = calculate_urte(margins_glvq) # RELU 03 Loss model.load_weights("weight_files/GLVQ/mnist/linf_trained/03_loss.h5") y_pred = model.predict(x_test[:n], verbose=True)