def train(): if not os.path.exists(tfrecord_path): para, faultcode = input_data.get_data(data_path) tfrecord_manager.create_tfrecord(para, faultcode, tfrecord_path) para_batch, faultcode_batch = tfrecord_manager.read_tfrecord(tfrecord_path, BATCH_SIZE, CAPACITY, NUM_THREADS) p_data = para_batch fc_data = faultcode_batch l1 = addLayer(p_data, 100, 40, activate_function=tf.nn.relu) # layer1: 100 x 40 l2 = addLayer(l1, 40, 887, activate_function=None) # layer2: 40 x 887 # train_loss = tf.reduce_mean(tf.reduce_sum(tf.square((y_data - l2)), reduction_indices=[0])) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=l2, labels=fc_data) train_loss = tf.reduce_mean(cross_entropy) train_op = tf.train.AdamOptimizer(0.01).minimize(train_loss) train_acc = tf.nn.in_top_k(l2, fc_data, 1) train_acc = tf.reduce_mean(tf.cast(train_acc, tf.float16)) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() init = tf.global_variables_initializer() sess.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: with open('loss_accuracy.csv', 'w+') as fp: for step in range(10000): if coord.should_stop(): break _, tr_loss, tr_acc = sess.run([train_op, train_loss, train_acc]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tr_loss, tr_acc * 100)) fp.write(str(tr_loss)) fp.write(',') fp.write(str(tr_acc * 100)) fp.write('\n') summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == 10000: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print("done") finally: coord.request_stop() coord.join(threads) sess.close()
def test_on_picture(): train_x, test_x, train_y, test_y = input_data.get_data() with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [None, 64, 64, 3], name="x") out = infer.inference(x, train=False, regularizer=None) predict = tf.argmax(out, 1) with tf.Session() as sess: saver = tf.train.Saver() saver.restore(sess, tf.train.latest_checkpoint("./model")) res = sess.run(predict, feed_dict={x: test_x}) print(res)
def test(datapath, tfrecord_path): if not os.path.exists(tfrecord_path): para, faultcode = input_data.get_data(datapath) create_tfrecord(para, faultcode, tfrecord_path) para_batch, faultcode_batch = read_tfrecord(tfrecord_path, 20, 200, 2) sess = tf.Session() init = tf.global_variables_initializer() sess.run(init) tf.train.start_queue_runners(sess=sess) para_val, faultcode_val = sess.run([para_batch, faultcode_batch]) print('first batch:') print('para_val: ', para_val) print('faultcode_val', faultcode_val) print('len para', para_val.shape) para_val, faultcode_val = sess.run([para_batch, faultcode_batch]) print('second batch:') print('para_val: ', para_val) print('faultcode_val', faultcode_val)
import tensorflow as tf import numpy as np import input_data as input_data import inference as infer import os import sys from parameters import * train_x, test_x, train_y, test_y = input_data.get_data() def next_batch(num, data, labels): ''' 返回batch_size 为 num 的batch ''' idx = np.arange(0, len(data)) np.random.shuffle(idx) idx = idx[:num] data_shuffle = [data[i] for i in idx] labels_shuffle = [labels[i] for i in idx] return np.asarray(data_shuffle), np.asarray(labels_shuffle) def train(): x = tf.placeholder( tf.float32, [None, IMG_SIZE, IMG_SIZE, 3], name='x-input') y_ = tf.placeholder(tf.float32, [None, 2], name='y-input') regularizer = tf.contrib.layers.l2_regularizer(REGULARAZATION_RATE) # 训练时, train=True,则会启动dropout # 给regularizer赋值则会将加入正则化,weights被正则化后加入集合losses当中 y = infer.inference(x, train=True, regularizer=regularizer)
# convolution def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') # pooling def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') train_images, train_labels, test_images, test_labels = input_data.get_data() # model x = tf.placeholder('float', shape=[None, 784]) y_ = tf.placeholder('float', shape=[None, 10]) # first convolutional layer x_image = tf.reshape(x, [-1, 28, 28, 1]) W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) # second convolutional layer W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64])
def test(): log_dir = '/home/lixiangyu/Documents/logs/' test_dir = '/home/lixiangyu/Desktop/test_data.csv' tfrecord_path = 'test.tfrecord' BATCH_SIZE = 64 n_test = 50000 CAPACITY = 2000 NUM_THREADS = 32 with tf.Graph().as_default(): if not os.path.exists(tfrecord_path): test_para, test_faultcode = input_data.get_data(test_dir) tfrecord_manager.create_tfrecord(test_para, test_faultcode, tfrecord_path) para_test_batch, faultcode_test_batch = tfrecord_manager.read_tfrecord( tfrecord_path, BATCH_SIZE, CAPACITY, NUM_THREADS) # logits = model.alexnet(test_batch, BATCH_SIZE, n_classes, keep_prob) l1 = bpnn.addLayer(para_test_batch, 100, 40, activate_function=tf.nn.relu) # relu是激励函数的一种 logits = bpnn.addLayer(l1, 40, 887, activate_function=None) top_k_op = tf.nn.in_top_k(logits, faultcode_test_batch, 1) saver = tf.train.Saver(tf.global_variables()) with tf.Session() as sess: print("Reading checkpoints...") ckpt = tf.train.get_checkpoint_state(log_dir) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print('Loading success, global_step is %s' % global_step) else: print('No checkpoint file found') return coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: num_iter = int(math.ceil(n_test / BATCH_SIZE)) true_count = 0 total_sample_count = num_iter * BATCH_SIZE step = 0 with open('submission.csv', 'w+') as fp: while step < num_iter and not coord.should_stop(): predictions = sess.run([top_k_op]) for p in predictions: for pp in p: fp.write(str(pp)) fp.write('\n') true_count += np.sum(predictions) step += 1 precision = true_count / total_sample_count print('precision = %.4f' % precision) except Exception as e: coord.request_stop(e) finally: coord.request_stop() coord.join(threads)
def run_training(): inputDataObj = input_data.get_data(FLAGS.input_data_dir) # Tell TensorFlow that the model will be built into the default Graph. with tf.Graph().as_default(): # Generate placeholders for the MFCCfeatures and labels. MFCCfeature_placeholder, labels_placeholder = placeholder_inputs( FLAGS.batch_size) # Build a Graph that computes predictions from the inference model. logits = multilayer_perceptron.inference(MFCCfeature_placeholder, FLAGS.hidden1, FLAGS.hidden2) # Add to the Graph the Ops for loss calculation. loss = multilayer_perceptron.loss(logits, labels_placeholder) # Add to the Graph the Ops that calculate and apply gradients. train_op = multilayer_perceptron.training(loss, FLAGS.learning_rate) # Add the Op to compare the logits to the labels during evaluation. eval_correct = multilayer_perceptron.evaluation( logits, labels_placeholder) # Build the summary Tensor based on the TF collection of Summaries. summary = tf.summary.merge_all() # Add the variable initializer Op. init = tf.global_variables_initializer() # Create a saver for writing training checkpoints. saver = tf.train.Saver() # Create a session for running Ops on the Graph. sess = tf.Session() # Instantiate a SummaryWriter to output summaries and the Graph. summary_writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph) # And then after everything is built: # Run the Op to initialize the variables. sess.run(init) # Start the training loop. for step in xrange(FLAGS.max_steps): start_time = time.time() # Fill a feed dictionary with the actual set of feature and labels # for this particular training step. feed_dict = fill_feed_dict(inputDataObj['train'], MFCCfeature_placeholder, labels_placeholder) # Run one step of the model. The return values are the activations # from the `train_op` (which is discarded) and the `loss` Op. To # inspect the values of your Ops or variables, you may include them # in the list passed to sess.run() and the value tensors will be # returned in the tuple from the call. _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict) duration = time.time() - start_time # Write the summaries and print an overview fairly often. if step % 100 == 0: # Print status to stdout. print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration)) # Update the events file. summary_str = sess.run(summary, feed_dict=feed_dict) summary_writer.add_summary(summary_str, step) summary_writer.flush() # Save a checkpoint and evaluate the model periodically. if (step + 1) % 1000 == 0 or (step + 1) == FLAGS.max_steps: checkpoint_file = os.path.join(FLAGS.log_dir, 'model.ckpt') saver.save(sess, checkpoint_file, global_step=step) # Evaluate against the training set. print('Training Data Eval:') do_eval(sess, eval_correct, MFCCfeature_placeholder, labels_placeholder, inputDataObj['train']) print('Test Data Eval:') do_eval(sess, eval_correct, MFCCfeature_placeholder, labels_placeholder, inputDataObj['test']) if (step + 1) == FLAGS.max_steps: print('final Data Eval:') do_eval(sess, eval_correct, MFCCfeature_placeholder, labels_placeholder, inputDataObj['valid'])
def __init__(self, ds_id, n_samples=1000, split=[0.6, 0.1, 0.3], output=None, random_state=None): """ Args: ds_id: index of the dataset. 1 - Letter recognition image data 2 - Iris data 3 - Glass identification data 4 - Image segmentation data 5 - Moons Overlapping 6 - Blobs 7 - Circles 8 - Gaussian Quantiles modified 9 - Multiclass 10 - Gaussian Quantiles 11 - Noise 12 - Moons Separable 13 - Marcin Luckner Dataset 14 - MNIST Dataset """ self.ds_id = ds_id if ds_id == 13: self.load_marcin_dataset(0) return elif ds_id == 14: self.load_mnist_data() return # load data x, y, self.target_names = get_data(ds_id, n_samples, binarize=True, random_state=random_state) self.n_features = x.shape[1] self.n_classes = y.shape[1] self.outliers = None # split into 60%, 10% and 30% if len(split) == 3: trn_size = split[0] vld_size = split[1] tst_size = split[2] x_trn, x_tst, y_trn, y_tst = \ train_test_split(x, y, train_size=trn_size) x_vld, x_tst, y_vld, y_tst = \ train_test_split(x_tst, y_tst, train_size=vld_size / (vld_size + tst_size)) self.trn = Set(x_trn, y_trn) self.vld = Set(x_vld, y_vld) self.tst = Set(x_tst, y_tst) elif len(split) == 2: splt1 = train_test_split(x, y, test_size=split[0]) self.trn = Set(splt1[1], splt1[3]) self.vld = None self.tst = Set(splt1[0], splt1[2]) self.print_info() # preprocess self.scale()
wire4 = Wire(['U62', 'R66', 'U55', 'R34', 'D71', 'R55', 'D58', 'R83']) coords3 = wire3.travel() coords4 = wire4.travel() intersection_cords2 = wire3.get_intersections(coords4) assert min( wire3.manhattan_distance(value) for value in intersection_cords2[1:]) == 159 wire5 = Wire([ 'R98', 'U47', 'R26', 'D63', 'R33', 'U87', 'L62', 'D20', 'R33', 'U53', 'R51' ]) wire6 = Wire( ['U98', 'R91', 'D20', 'R16', 'D67', 'R40', 'U7', 'R15', 'U6', 'R7']) coords5 = wire5.travel() coords6 = wire6.travel() intersection_cords3 = wire5.get_intersections(coords6) assert min( wire5.manhattan_distance(value) for value in intersection_cords3[1:]) == 135 data = get_data('data.txt') real_wire1 = Wire(data['Wire_0']) real_wire2 = Wire(data['Wire_1']) real_coords1 = real_wire1.travel() real_coords2 = real_wire2.travel() real_intersections = real_wire1.get_intersections(real_coords2) print( min( real_wire1.manhattan_distance(value) for value in real_intersections[1:]))
def train(): para, faultcode = input_data.get_data(data_path) para_batch, faultcode_batch = input_data.get_batch(para, faultcode, BATCH_SIZE, CAPACITY) x_data = para_batch # 转为列向量 # noise = np.random.normal(0, 0.05, x_data.shape) y_data = faultcode_batch # x_data = np.linspace(-1, 1, 300)[:, np.newaxis] # 转为列向量 # noise = np.random.normal(0, 0.05, x_data.shape) # y_data = np.square(x_data) + 0.5 + noise xs = tf.placeholder(tf.float32, [None, 100]) # 样本数未知,特征数为1,占位符最后要以字典形式在运行中填入 ys = tf.placeholder(tf.int32, [None, 1]) l1 = addLayer(x_data, 100, 20, activate_function=tf.nn.relu) # relu是激励函数的一种 l2 = addLayer(l1, 20, 887, activate_function=None) # train_loss = tf.reduce_mean(tf.reduce_sum(tf.square((y_data - l2)), reduction_indices=[0])) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=l2, labels=y_data) train_loss = tf.reduce_mean(cross_entropy) train_op = tf.train.GradientDescentOptimizer(0.001).minimize(train_loss) train_acc = tf.nn.in_top_k(l2, y_data, 1) train_acc = tf.reduce_mean(tf.cast(train_acc, tf.float16)) summary_op = tf.summary.merge_all() sess = tf.Session() train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) saver = tf.train.Saver() # loss = tf.reduce_mean(tf.reduce_sum(tf.square((y_data - l2)), reduction_indices=[0])) # 需要向相加索引号,reduce执行跨纬度操作 # # train = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # 选择梯度下降法 # init = tf.global_variables_initializer() sess.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: for step in range(10000): if coord.should_stop(): break _, tr_loss, tr_acc = sess.run([train_op, train_loss, train_acc]) # aaa, bbb, cc = sess.run([l2, y_data, cross_entropy]) if step % 50 == 0: print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tr_loss, tr_acc * 100)) summary_str = sess.run(summary_op) train_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == 10000: checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print("done") finally: coord.request_stop() coord.join(threads) sess.close()