def train(self, sess: tf.InteractiveSession, train_x: np.ndarray, train_y: np.ndarray, valid_x: np.ndarray, valid_y: np.ndarray, epoch=20, batch_size=128, valid_batch_size=50, step=200, verbose=True): print(get_now(), 'start training') train_idx = sorted(range(len(train_x)), key=lambda x: len(train_x[x]), reverse=True) valid_idx = sorted(range(len(valid_x)), key=lambda x: len(valid_x[x]), reverse=True) sess.run(tf.global_variables_initializer()) best_aupr = best_auc = current = 0 for idx_epoch in range(epoch): for i in range(0, len(train_idx), batch_size): batch_idx = train_idx[i:i + batch_size] train_loss = self.train_step(sess, train_x[batch_idx], train_y[batch_idx]) current += 1 if current % step == 0: print(get_now()) valid_loss, valid_res = 0, np.empty([len(valid_idx), 1], dtype=int) for j in range(0, len(valid_idx), valid_batch_size): valid_batch_idx = valid_idx[j:j + valid_batch_size] loss, output = self.valid_step( sess, valid_x[valid_batch_idx], valid_y[valid_batch_idx]) valid_res[valid_batch_idx] = output valid_loss += loss * len(valid_batch_idx) valid_loss /= len(valid_idx) auc, aupr = get_auc(valid_y, valid_res), get_aupr( valid_y, valid_res) if aupr > best_aupr: best_aupr = aupr best_auc = auc self.saver.save(sess, self.model_path) if verbose: print(get_now(), current, current * batch_size, idx_epoch, i + batch_size, 'train loss:', round(train_loss, 5), 'valid loss:', round(valid_loss, 5), 'AUC:', round(auc, 5), 'AUPR:', round(aupr, 5)) print(get_now(), 'Summary ', 'Best AUC:', best_auc, 'Best AUPR:', best_aupr)
def get_gradient(network: keras.models.Sequential, X: numpy.array, session: tensorflow.InteractiveSession) -> numpy.array: """ Calculates the gradient for a given network/input :param network: Network :type network: keras.Sequential :param X: Input to network. :type X: numpy.array :param session: Session object :type session: tensorflow.InteractiveSession :returns: Gradient as numpy.array """ x = tensorflow.placeholder(tensorflow.float32, X.shape) prediction = network(x) y_shape = tensorflow.shape(prediction) classes = y_shape[1] index = tensorflow.argmax(prediction, axis=1) target = tensorflow.one_hot(index, classes, on_value=1.0, off_value=0.0) logits, = prediction.op.inputs loss = tensorflow.nn.softmax_cross_entropy_with_logits(labels=target, logits=logits) gradient, = tensorflow.gradients(loss, x) return session.run(gradient, feed_dict={x: X, keras.backend.learning_phase(): 0})
def predict_step(self, sess: tf.InteractiveSession, data_x: np.ndarray): batch_num, res = len(data_x), [] for i in range(batch_num): feed_dict = { self.data_smi: data_x[i][0], self.data_adj: data_x[i][1], self.data_seq: data_x[i][2], self.training: False } output = sess.run(self.output, feed_dict=feed_dict) res.append(output[0]) return res
def train_step(self, sess: tf.InteractiveSession, train_x: np.ndarray, train_y: np.ndarray): batch_loss, batch_num = 0, len(train_x) for i in range(batch_num): batch_x, batch_y = train_x[i], train_y[i] feed_dict = { self.data_smi: batch_x[0], self.data_adj: batch_x[1], self.data_seq: batch_x[2], self.data_y: np.array(batch_y).reshape([-1, 1]), self.training: True } _, loss = sess.run([self.optimizer, self.loss], feed_dict=feed_dict) batch_loss += loss / batch_num return batch_loss
def valid_step(self, sess: tf.InteractiveSession, valid_x: np.ndarray, valid_y: np.ndarray): batch_loss, batch_num, res = 0, len(valid_x), [] for i in range(batch_num): batch_x, batch_y = valid_x[i], valid_y[i] feed_dict = { self.data_smi: batch_x[0], self.data_adj: batch_x[1], self.data_seq: batch_x[2], self.data_y: np.array(batch_y).reshape([-1, 1]), self.training: False } loss, output = sess.run([self.loss, self.output], feed_dict=feed_dict) batch_loss += loss / batch_num res.append(output[0]) return batch_loss, res
class CNNModel: def __init__(self, image_size=[28, 28], char_number=10, channel=1): self.image_size = image_size self.char_number = char_number self.channel = channel # self.learning_rate = learning_rate # self.eval_every = eval_every # self.epochs = epochs # self.evaluation_size = evaluation_size # self.batch_size = batch_size # self.optimizer = optimizer self.inputs = placeholder(float32, shape=[None, image_size[0] * image_size[1]], name="inputs") self.model = reshape(self.inputs, [-1, image_size[0], image_size[1], channel]) self.labels = placeholder(float32, shape=[None, 10], name="labels") self.sess = InteractiveSession() def addLayer(self, layer): self.model = layer.implement(self.model) def addOutputLayer(self, layer): self.loss = layer.implement(self.model, self.labels) def train(self, dataset, eval_every=5, epochs=500, evaluation_size=500, batch_size=100, optimizer=train.MomentumOptimizer(0.005, 0.9)): train_step = optimizer.minimize(self.loss) prediction = argmax(self.model, 1, name="prediction") result = equal(argmax(self.labels, 1), prediction, name="result") self.accuracy = reduce_mean(cast(result, float32), name="accuracy") train_loss = [] train_accuracy = [] test_accuracy = [] global_variables_initializer().run() for i in range(epochs): # Lay ra batch_size hinh anh tu tap train train_batch = dataset.train.next_batch(batch_size) train_dict = { self.inputs: train_batch[0], self.labels: train_batch[1] } if i % eval_every == 0: # Cu sau eval_every buoc lap thi test mot lan test_batch = dataset.test.next_batch(evaluation_size) temp_train_loss = self.loss.eval(feed_dict=train_dict) temp_train_accuracy = self.accuracy.eval(feed_dict=train_dict) temp_test_accuracy = self.accuracy.eval( feed_dict={ self.inputs: test_batch[0], self.labels: test_batch[1] }) print( 'Epoch # %d, Train Loss: %g. Train Accuracy (Test Accuracy): %g (%g)' % (i, temp_train_loss, temp_train_accuracy, temp_test_accuracy)) # Luu cac gia tri de ve bieu do train_loss.append(temp_train_loss) train_accuracy.append(temp_train_accuracy) test_accuracy.append(temp_test_accuracy) # Chay thuat toan toi uu ham mat mat self.sess.run(train_step, feed_dict=train_dict) # Show plots loss_per_epoch(epochs, eval_every, train_loss) train_test_accuracy(epochs, eval_every, train_accuracy, test_accuracy) def test(self, dataset): """ test model with entire mnist_test """ print('test accuracy %g' % self.accuracy.eval( feed_dict={ self.inputs: dataset.test.images, self.labels: dataset.test.labels, })) def save(self, model_path): """ save model to folder in model_path """ saver = train.Saver() saver.save(self.sess, model_path)