def main(): args = get_arguments() print(args) np.random.seed(args.seed) dataset, train_img_feature, train_data = get_train_data(args) dataset, test_img_feature, test_data, val_answers = get_data_test(args) train_X = [train_data[u'question'], train_img_feature] train_Y = np_utils.to_categorical(train_data[u'answers'], args.nb_classes) test_X = [test_data[u'question'], test_img_feature] test_Y = np_utils.to_categorical(val_answers, args.nb_classes) model_name = importlib.import_module("models." + args.model) model = model_name.model(args) model.compile(loss='categorical_crossentropy', optimizer=args.optimizer, metrics=['accuracy']) model.summary() # prints model layers with weights history = model.fit(train_X, train_Y, batch_size=args.batch_size, nb_epoch=args.nb_epoch, validation_data=(test_X, test_Y)) return history.history
def train_model(path=None): model_file = get_model_file(path) X, y = get_train_data(path) if len(X) == 0: raise ValueError("No wifi access points have been found during training") # fantastic: because using "quality" rather than "rssi", we expect values 0-150 # 0 essentially indicates no connection # 150 is something like best possible connection # Not observing a wifi will mean a value of 0, which is the perfect default. lp = get_pipeline() lp.fit(X, y) with open(model_file, "wb") as f: pickle.dump(lp, f) return lp
def model(): is_training = tf.placeholder(tf.bool, []) train_images, train_label = data.get_train_data(batch_size) test_images, test_label = data.get_test_data(batch_size) x = tf.cond(is_training, lambda:train_images, lambda:test_images) y_ = tf.cond(is_training, lambda:train_label, lambda:test_label) y_ = tf.cast(y_, tf.int64) with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.crelu, normalizer_fn=slim.batch_norm, weights_regularizer=slim.l2_regularizer(0.005), normalizer_params={'is_training': is_training, 'decay': 0.95} ): conv1 =slim.conv2d(x, 39, [1,1], weights_initializer=tf.truncated_normal_initializer(mean=0.54, stddev=0.24)) conv2 =slim.conv2d(conv1, 8, [6,6], weights_initializer=tf.truncated_normal_initializer(mean=0.07, stddev=0.57)) pool1 = slim.avg_pool2d(conv2, [4,4], stride=4, padding='SAME') flatten = slim.flatten(pool1) full1 = slim.fully_connected(flatten, 1598, weights_initializer=tf.truncated_normal_initializer(mean=-0.1043610, stddev=0.40), biases_initializer=tf.constant_initializer(0.1, dtype=tf.float32)) logits = slim.fully_connected(full1, 10, activation_fn=None, weights_initializer=tf.truncated_normal_initializer(mean=-0.244986, stddev=0.477889), biases_initializer=tf.constant_initializer(0.1, dtype=tf.float32)) correct_prediction = tf.equal(tf.argmax(logits, 1), y_) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) regularization_loss = tf.add_n(slim.losses.get_regularization_losses()) cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y_, logits=logits))+ regularization_loss step = tf.get_variable("step", [], initializer=tf.constant_initializer(0.0), trainable=False) # lr = tf.train.exponential_decay(0.1, # step, # 550*30, # 0.9, # staircase=True) # # # optimizer = tf.train.GradientDescentOptimizer(lr) optimizer = tf.train.AdamOptimizer(0.001) # lr_summary = tf.summary.scalar('lr', lr) train_step = slim.learning.create_train_op(cross_entropy, optimizer, global_step=step) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) if update_ops: updates = tf.group(*update_ops) cross_entropy = control_flow_ops.with_dependencies([updates], cross_entropy) loss_summary = tf.summary.scalar('loss', cross_entropy) accuracy_summary = tf.summary.scalar('accuracy', accuracy) merge_summary = tf.summary.merge([loss_summary, accuracy_summary]) return is_training, train_step, step, accuracy, cross_entropy, merge_summary
def model(): x = tf.placeholder(dtype=tf.float32, shape=[batch_size, 32, 32, 1], name='Input') y = tf.placeholder(dtype=tf.float32, shape=[batch_size], name='True_Y') y = tf.cast(y, tf.int64) keep_prob = tf.placeholder(dtype=tf.float32, shape=(), name='dropout') is_training = tf.placeholder(tf.bool, shape=()) with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.crelu, normalizer_fn=slim.batch_norm, normalizer_params={ 'is_training': is_training, 'decay': 0.95 }): h = slim.conv2d(inputs=x, num_outputs=100, kernel_size=2, weights_regularizer=slim.l2_regularizer(0.0010)) h = slim.conv2d(inputs=h, num_outputs=82, kernel_size=2, weights_regularizer=slim.l2_regularizer(0.0018)) h = slim.conv2d(inputs=h, num_outputs=100, kernel_size=2, weights_regularizer=slim.l2_regularizer(0.0001)) h = slim.conv2d(inputs=h, num_outputs=100, kernel_size=2, weights_regularizer=slim.l2_regularizer(0.0001)) h = slim.max_pool2d(h, kernel_size=2, stride=2) flatten = slim.flatten(h) full = slim.fully_connected(flatten, 512) drop_full = slim.dropout(full, keep_prob) with tf.name_scope('accuracy'): logits = slim.fully_connected(drop_full, 10, activation_fn=None) correct_prediction = tf.equal(tf.argmax(logits, 1), y) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) with tf.name_scope('loss'): loss = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( labels=y, logits=logits)) + tf.add_n( tf.losses.get_regularization_losses()) with tf.name_scope('train'): optimizer = tf.train.AdamOptimizer() step = tf.get_variable("step", [], initializer=tf.constant_initializer(0.0), trainable=False) train_op = slim.learning.create_train_op(loss, optimizer, global_step=step) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) if update_ops: updates = tf.group(*update_ops) loss = control_flow_ops.with_dependencies([updates], loss) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) train_data, train_label = get_data.get_train_data() validate_data, validate_label = get_data.get_test_data() epochs = total_epochs for current_epoch in range(epochs): train_loss_list = [] train_accu_list = [] total_length = train_data.shape[0] idx = np.arange(total_length) np.random.shuffle(idx) train_data = train_data[idx] train_label = train_label[idx] total_steps = total_length // batch_size for step in range(total_steps): batch_train_data = train_data[step * batch_size:(step + 1) * batch_size] batch_train_label = train_label[step * batch_size:(step + 1) * batch_size] _, loss_v, accuracy_str = sess.run( [train_op, loss, accuracy], { x: batch_train_data, y: batch_train_label, keep_prob: 0.5, is_training: True }) train_loss_list.append(loss_v) train_accu_list.append(accuracy_str) #test test_length = validate_data.shape[0] test_steps = test_length // batch_size test_loss_list = [] test_accu_list = [] for step in range(test_steps): batch_test_data = validate_data[step * batch_size:(step + 1) * batch_size] batch_test_label = validate_label[step * batch_size:(step + 1) * batch_size] loss_v, accuracy_str = sess.run( [loss, accuracy], { x: batch_test_data, y: batch_test_label, keep_prob: 1.0, is_training: False }) test_loss_list.append(loss_v) test_accu_list.append(accuracy_str) print( '{}, epoch:{}/{}, step:{}/{}, loss:{:.6f}, accu:{:.4f}, test loss:{:.6f}, accu:{:.4f}' .format(datetime.now(), current_epoch, total_epochs, total_steps * current_epoch + step, total_steps * epochs, np.mean(train_loss_list), np.mean(train_accu_list), np.mean(test_loss_list), np.mean(test_accu_list)))
torch.save(model.state_dict(), model_path + "\\rnn7_epoch_%d.pth" % (epoch)) root_path = os.path.abspath('../') #项目的根目录,这里要保证它是整个项目的根目录 #读入词典 vocab_path = root_path + '\\Data\\word_vocab_for_rnn.pkl' with open(vocab_path, 'rb') as f: vocab = pickle.load(f) wd2Idx = {wd: idx for idx, wd in enumerate(vocab)} idx2Wd = {idx: wd for idx, wd in enumerate(vocab)} #读取训练数据,这里是七言的古诗 train_data_path = root_path + '\\Data\\qtrain' poem_line_lst7, poem_vec_lst7 = get_train_data(train_data_path, wd2Idx, 7) # 超参数 LR = 0.001 BATCH_SIZE = 128 vocab_size = 6773 embed_size = 200 hidden_size = 200 start_epoch = 0 #现在是从第start_epoch轮开始训练的 sentence_len = 7 #七言 net7 = Net(sentence_len=sentence_len, batch_size=BATCH_SIZE, vocab_size=vocab_size, embed_size=embed_size, hidden_size=hidden_size)
torch.save(model.state_dict(), model_path + "\\rnn5_epoch_%d.pth" % (epoch)) root_path = os.path.abspath('../') #项目的根目录,这里要保证它是整个项目的根目录 #读入词典 vocab_path = root_path + '\\Data\\word_vocab_for_rnn.pkl' with open(vocab_path, 'rb') as f: vocab = pickle.load(f) wd2Idx = {wd: idx for idx, wd in enumerate(vocab)} idx2Wd = {idx: wd for idx, wd in enumerate(vocab)} #读取训练数据,这里是五言的古诗 train_data_path = root_path + '\\Data\\qtrain' poem_line_lst5, poem_vec_lst5 = get_train_data(train_data_path, wd2Idx, 5) # 超参数 LR = 0.001 BATCH_SIZE = 128 vocab_size = 6773 embed_size = 200 hidden_size = 200 start_epoch = 0 #现在是从第start_epoch轮开始训练的 sentence_len = 5 #五言 net5 = Net(sentence_len=sentence_len, batch_size=BATCH_SIZE, vocab_size=vocab_size, embed_size=embed_size, hidden_size=hidden_size)
def parse_individual(self, indi, num_of_input_channel, indi_index, save_path, history_best_score): tf.reset_default_graph() train_data, train_label = get_data.get_train_data(self.batch_size) validate_data, validate_label = get_data.get_validate_data(self.batch_size) is_training, train_op, accuracy, cross_entropy, num_connections, merge_summary = self.build_graph(indi_index, num_of_input_channel, indi, train_data, train_label, validate_data, validate_label) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) steps_in_each_epoch = (self.train_data_length//self.batch_size) total_steps = int(self.epochs*steps_in_each_epoch) coord = tf.train.Coordinator() #threads = tf.train.start_queue_runners(sess, coord) try: threads = [] for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS): threads.extend(qr.create_threads(sess, coord=coord, daemon=True, start=True)) for i in range(total_steps): if coord.should_stop(): break _, accuracy_str, loss_str, _ = sess.run([train_op, accuracy,cross_entropy, merge_summary], {is_training:True}) if i % (2*steps_in_each_epoch) == 0: test_total_step = self.validate_data_length//self.batch_size test_accuracy_list = [] test_loss_list = [] for _ in range(test_total_step): test_accuracy_str, test_loss_str = sess.run([accuracy, cross_entropy], {is_training:False}) test_accuracy_list.append(test_accuracy_str) test_loss_list.append(test_loss_str) mean_test_accu = np.mean(test_accuracy_list) mean_test_loss = np.mean(test_loss_list) print('{}, {}, indi:{}, Step:{}/{}, train_loss:{}, acc:{}, test_loss:{}, acc:{}'.format(datetime.now(), i // steps_in_each_epoch, indi_index, i, total_steps, loss_str, accuracy_str, mean_test_loss, mean_test_accu)) #print('{}, test_loss:{}, acc:{}'.format(datetime.now(), loss_str, accuracy_str)) #validate the last epoch test_total_step = self.validate_data_length//self.batch_size test_accuracy_list = [] test_loss_list = [] for _ in range(test_total_step): test_accuracy_str, test_loss_str = sess.run([accuracy, cross_entropy], {is_training:False}) test_accuracy_list.append(test_accuracy_str) test_loss_list.append(test_loss_str) mean_test_accu = np.mean(test_accuracy_list) mean_test_loss = np.mean(test_loss_list) print('{}, test_loss:{}, acc:{}'.format(datetime.now(), mean_test_loss, mean_test_accu)) mean_acc = mean_test_accu if mean_acc > history_best_score: save_mean_acc = tf.Variable(-1, dtype=tf.float32, name='save_mean') save_mean_acc_op = save_mean_acc.assign(mean_acc) sess.run(save_mean_acc_op) saver0 = tf.train.Saver() saver0.save(sess, save_path +'/model') saver0.export_meta_graph(save_path +'/model.meta') history_best_score = mean_acc except Exception as e: print(e) coord.request_stop(e) finally: print('finally...') coord.request_stop() coord.join(threads) return mean_test_accu, np.std(test_accuracy_list), num_connections, history_best_score
for train_index, test_index in kf.split(X, Y): X_train = X[train_index] Y_train = Y[train_index] X_test = X[test_index] Y_test = Y[test_index] clf.fit(X_train, Y_train) predict_value = clf.predict_proba(X_test)[:, 1] AUC = metrics.roc_auc_score(Y_test, predict_value) precision, recall, _ = precision_recall_curve(Y_test, predict_value) AUCPR = auc(recall, precision) AUPR_list.append(AUCPR) p = precision_score(Y_test, predict_value.round()) p_list.append(p) r = recall_score(Y_test, predict_value.round()) r_list.append(r) f1 = f1_score(Y_test, predict_value.round()) f1_list.append(f1) AUC_list.append(AUC) print("the average of the AUC is ", sum(AUC_list) / len(AUC_list)) print("the average of the AUPR is ", sum(AUPR_list) / len(AUPR_list)) print("the average of p is ", sum(p_list) / len(p_list)) print("the average of r is ", sum(r_list) / len(r_list)) print("the average of f1 is ", sum(f1_list) / len(f1_list)) if __name__ == "__main__": sample_data, label = get_train_data() sample_data = np.array(sample_data) label = np.array(label) train(sample_data, label)
metadata = get_metadata(input_json) #embedding_matrix = prepare_embeddings(vocabulary_size, word_emb_dim, metadata, embedding_matrix_filename, glove_path) #print(embedding_matrix.shape) model = san_atten(common_word_emb_dim, img_vec_dim, activation_1, activation_2, dropout, vocabulary_size, num_hidden_units_lstm, max_ques_length, word_emb_dim, num_hidden_layers_mlp, num_hidden_units_mlp, nb_classes, class_activation, filter_sizes, num_attention_layers) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) model.summary() # prints model layers with weights train_X, train_Y = get_train_data(input_img_train_h5, input_ques_h5) test_X, test_Y, multi_val_y = get_test_data(input_img_test_h5, input_ques_h5, metadata, val_file) model.fit(train_X, train_Y, batch_size=batch_size, epochs=epochs, validation_data=(test_X, test_Y), verbose=1) print("Evaluating Accuracy on validation set:") metric_vals = model.evaluate(test_X, test_Y) print("") for metric_name, metric_val in zip(model.metrics_names, metric_vals):
cae.update(self.pops.gbest) log_particle_info(i, 'after:' + str(cae)) self.pops.indi[i] = cae for i in range(self.pops.get_pop_size()): cae = self.pops.indi[i] if cae.score < cae.b_score: cae.set_pbest(cae) if cae.score < self.pops.gbest_score: self.pops.set_gbest(cae) if __name__ == '__main__': #cuda2 os.environ["CUDA_VISIBLE_DEVICES"] = "1" train_data = get_data.get_train_data() #test_data, test_label = get_data.get_test_data() validate_data = get_data.get_validate_data() params = {} params['train_data'] = train_data #params['train_label'] = train_label params['validate_data'] = validate_data #params['validate_label'] = validate_label #params['test_data'] = test_data #params['test_label'] = test_label params['pop_size'] = 50 params['num_class'] = 10 params['cae_length'] = 5 params['x_prob'] = 0.9 params['x_eta'] = 20
parser.add_argument( '--model_name', default='lr', choices=names(), help='Name of the model to use.') parser.add_argument( '--data_dir', default='./data/timeseries_data.csv', help='Dir of the data to train') parser.add_argument( '--predict_time', type=int, help='The prediction time.') args = parser.parse_args() check_result = check_param(args) if check_result == '': ori_data, timestamp_list, value_list = get_train_data(args.data_dir, args.predict_time) predict_data = predict_model(timestamp_list, value_list, args, freq=args.predict_time) print("the prediction result:") print(predict_data) truth_data = get_truth_data(args.data_dir, args.predict_time) if predict_data is not None and truth_data is not None: pct_mean_value, RMSE = pct(predict_data, truth_data) print("the prediction error:%f" % pct_mean_value) print("the prediction RMSE:%f" % RMSE) get_figure(value_list, predict_data, truth_data, RMSE) else: print('The result of prediction is null') else: print(check_result)
best_map = 0 best_precision = 0 best_recall = 0 best_ep = 0 max_epoch = 500 if flags.is_training: print('do train!!!!!!') for ep_idx in range(max_epoch): train_loss_sum = 0 scale_loss_sum = 0 batch_num = 0 while True: images, labels, end_of_epoch = get_data.get_train_data( anchors, batch_size) if images.shape[0]: if not flags.scale: feed_dict = { images_plh: images, labels_plh: labels, is_training_plh: True, best_scale_plh: no_scale } _, loss = sess.run([model.optimizer, model.loss], feed_dict=feed_dict) train_loss_sum += loss batch_num += 1 else:
np.mean(test_pure_loss_list))) return np.mean(test_pure_loss_list) def max_unpool_2x2(self, x, name): width = x.get_shape()[1].value height = x.get_shape()[2].value inference = tf.image.resize_images(x, [width * 2, height * 2]) return inference if __name__ == '__main__': #cuda3 os.environ["CUDA_VISIBLE_DEVICES"] = "1" unlabeldata = get_data.get_unlabeled_data() train_data, train_label = get_data.get_train_data() test_data, test_label = get_data.get_test_data() params = {} params['unlabel_data'] = unlabeldata params['train_data'] = train_data params['train_label'] = train_label params['test_data'] = test_data params['test_label'] = test_label params['pop_size'] = 50 params['num_class'] = 10 params['cae_length'] = 5 params['x_prob'] = 0.9 params['x_eta'] = 20 params['m_prob'] = 0.1 params['m_eta'] = 20