Exemple #1
0
 def solver_eval(self, y_true, y_pred):
     if self.output_dim == 1:
         y_pred = tf.reshape(tf.convert_to_tensor(y_pred, np.float32), [-1])
         y_true = tf.reshape(tf.convert_to_tensor(y_true, np.float32), [-1])
         return K.mean(keras.losses.mean_squared_error(
             y_true, y_pred)).eval(session=tf.Session())
     elif self.output_dim == 2:
         y_pred = tf.reshape(tf.convert_to_tensor(y_pred, np.float32), [-1])
         y_true = tf.reshape(tf.convert_to_tensor(y_true, np.float32), [-1])
         if self.entrophy:
             return K.mean(K.binary_crossentropy(y_true, y_pred),
                           axis=-1).eval(session=tf.Session())
         return 1 - K.mean(keras.metrics.binary_accuracy(
             y_true, y_pred)).eval(session=tf.Session())
     else:
         y_pred = tf.reshape(tf.convert_to_tensor(y_pred, np.float32),
                             [-1, self.output_dim])
         y_true = tf.reshape(tf.convert_to_tensor(y_true, np.float32),
                             [-1, self.output_dim])
         if self.entrophy:
             return K.mean(K.categorical_crossentropy(y_true, y_pred),
                           axis=-1).eval(session=tf.Session())
         return 1 - K.mean(
             keras.metrics.categorical_accuracy(
                 y_true, y_pred)).eval(session=tf.Session())
Exemple #2
0
 def eval_split(self, name, want, model_l, parts_train, parts_valid):
     y_pred_l = []
     y_valid_l = []
     if not self.solver:
         for i in range(max(self.output_dim, len(self.k_mean_list))):
             model = model_l[i][1]
             X_valid_tmp, y_valid_tmp = parts_valid[i]
             y_pred_l.append(model.predict(X_valid_tmp))
             y_valid_l.append(y_valid_tmp)
         y_pred = np.concatenate(y_pred_l)
         y_valid = np.concatenate(y_valid_l)
         if self.output_dim == 1:
             y_pred = tf.reshape(tf.convert_to_tensor(y_pred, np.float32),
                                 [-1])
             y_valid = tf.reshape(tf.convert_to_tensor(y_valid, np.float32),
                                  [-1])
             return K.mean(keras.losses.mean_squared_error(
                 y_valid, y_pred)).eval(session=tf.Session())
         elif self.output_dim == 2:
             y_pred = tf.reshape(tf.convert_to_tensor(y_pred, np.float32),
                                 [-1])
             y_valid = tf.reshape(tf.convert_to_tensor(y_valid, np.float32),
                                  [-1])
             return K.mean(K.binary_crossentropy(y_valid, y_pred),
                           axis=-1).eval(session=tf.Session())
         else:
             y_pred = tf.reshape(tf.convert_to_tensor(y_pred, np.float32),
                                 [-1, self.output_dim])
             y_valid = tf.reshape(tf.convert_to_tensor(y_valid, np.float32),
                                  [-1, self.output_dim])
             return K.mean(K.categorical_crossentropy(y_valid, y_pred),
                           axis=-1).eval(session=tf.Session())
     else:
         for i in range(max(self.output_dim, len(self.k_mean_list))):
             model = model_l[i][1]
             X_train, y_train = parts_train[i]
             X_valid, y_valid = parts_valid[i]
             dense_layer_model = Model(
                 inputs=model.input,
                 outputs=model.get_layer(index=-2).output)
             hidden = dense_layer_model.predict(X_train)
             hidden2 = dense_layer_model.predict(X_valid)
             mix = np.concatenate((X_train, hidden), 1)
             mix2 = np.concatenate((X_valid, hidden2), 1)
             my_solver_train = Solver(X_train,
                                      X_valid,
                                      y_train,
                                      y_valid,
                                      train=self.solver)
             loss_train = self.solver_eval(my_solver_train.predict()[0],
                                           my_solver_train.predict()[1])
             my_solver_hidden = Solver(hidden,
                                       hidden2,
                                       y_train,
                                       y_valid,
                                       train=self.solver)
             loss_hidden = self.solver_eval(my_solver_hidden.predict()[0],
                                            my_solver_hidden.predict()[1])
             my_solver_mix = Solver(mix,
                                    mix2,
                                    y_train,
                                    y_valid,
                                    train=self.solver)
             loss_mix = self.solver_eval(my_solver_mix.predict()[0],
                                         my_solver_mix.predict()[1])
             loss_min = min(loss_train, loss_hidden, loss_mix)
             if loss_min == loss_train:
                 self.solver_dict[name + '_' + str(i) + want + '_' +
                                  str(len(model.layers))] = (
                                      my_solver_train, 'train')
                 y_valid_tmp, y_pred_tmp = my_solver_train.predict()
             elif loss_min == loss_hidden:
                 self.solver_dict[name + '_' + str(i) + want + '_' +
                                  str(len(model.layers))] = (
                                      my_solver_hidden, 'hidden')
                 y_valid_tmp, y_pred_tmp = my_solver_hidden.predict()
             else:
                 self.solver_dict[name + '_' + str(i) + want + '_' +
                                  str(len(model.layers))] = (my_solver_mix,
                                                             'mix')
                 y_valid_tmp, y_pred_tmp = my_solver_mix.predict()
             y_pred_l.append(y_pred_tmp)
             y_valid_l.append(y_valid_tmp)
         y_pred = np.concatenate(y_pred_l)
         y_valid = np.concatenate(y_valid_l)
         return self.solver_eval(y_valid, y_pred)
Exemple #3
0
 def eval_model(self, name, model, X_train, X_valid, y_train, y_valid):
     if not self.solver:
         y_pred = model.predict(X_valid)
         if self.output_dim == 1:
             y_pred = tf.reshape(tf.convert_to_tensor(y_pred, np.float32),
                                 [-1])
             y_valid = tf.reshape(tf.convert_to_tensor(y_valid, np.float32),
                                  [-1])
             return K.mean(keras.losses.mean_squared_error(
                 y_valid, y_pred)).eval(session=tf.Session())
         elif self.output_dim == 2:
             y_pred = tf.reshape(tf.convert_to_tensor(y_pred, np.float32),
                                 [-1])
             y_valid = tf.reshape(tf.convert_to_tensor(y_valid, np.float32),
                                  [-1])
             return K.mean(K.binary_crossentropy(y_valid, y_pred),
                           axis=-1).eval(session=tf.Session())
         else:
             y_pred = tf.reshape(tf.convert_to_tensor(y_pred, np.float32),
                                 [-1, self.output_dim])
             y_valid = tf.reshape(tf.convert_to_tensor(y_valid, np.float32),
                                  [-1, self.output_dim])
             return K.mean(K.categorical_crossentropy(y_valid, y_pred),
                           axis=-1).eval(session=tf.Session())
     else:
         dense_layer_model = Model(inputs=model.input,
                                   outputs=model.get_layer(index=-2).output)
         hidden = dense_layer_model.predict(X_train)
         hidden2 = dense_layer_model.predict(X_valid)
         mix = np.concatenate((X_train, hidden), 1)
         mix2 = np.concatenate((X_valid, hidden2), 1)
         my_solver_train = Solver(X_train,
                                  X_valid,
                                  y_train,
                                  y_valid,
                                  train=self.solver)
         loss_train = self.solver_eval(my_solver_train.predict()[0],
                                       my_solver_train.predict()[1])
         my_solver_hidden = Solver(hidden,
                                   hidden2,
                                   y_train,
                                   y_valid,
                                   train=self.solver)
         loss_hidden = self.solver_eval(my_solver_hidden.predict()[0],
                                        my_solver_hidden.predict()[1])
         my_solver_mix = Solver(mix,
                                mix2,
                                y_train,
                                y_valid,
                                train=self.solver)
         loss_mix = self.solver_eval(my_solver_mix.predict()[0],
                                     my_solver_mix.predict()[1])
         loss_min = min(loss_train, loss_hidden, loss_mix)
         if loss_min == loss_train:
             self.solver_dict[name + '_' +
                              str(len(model.layers))] = (my_solver_train,
                                                         'train')
         elif loss_min == loss_hidden:
             self.solver_dict[name + '_' +
                              str(len(model.layers))] = (my_solver_hidden,
                                                         'hidden')
         else:
             self.solver_dict[name + '_' +
                              str(len(model.layers))] = (my_solver_mix,
                                                         'mix')
         return loss_min