tensorflow编程一些需要知道的 - 4

时间:2021-09-16 19:33:08
训练1024维特征的数据集合,multi-label, 转成多个1的onehot的label, 2个全连接。很直接的顺序编程,练习、便于理解
# Import your data#from tensorflow.examples.tutorials.mnist import input_dataimport osimport tensorflow as tffrom tensorflow import gfilefrom tensorflow import loggingfrom tensorflow import flagsfrom tensorflow import appimport numpy as np# Parameterslearning_rate = 0.001training_epochs = 5batch_size = 512display_step = 2# Network Parametersn_hidden_1 = 256 # 1st layer number of featuresn_hidden_2 = 128 # 2nd layer number of featuresn_input = 1024 #n_classes = 4716 # tf Graph inputx = tf.placeholder("float", [None, n_input])y = tf.placeholder("float", [None, n_classes])# Create modeldef multilayer_perceptron(x, weights, biases):    # Hidden layer with RELU activation    input_dim = len(x.get_shape()) - 1    model_input = tf.nn.l2_normalize(x, input_dim)    layer_1 = tf.add(tf.matmul(model_input, weights['h1']), biases['b1'])    layer_1 = tf.nn.relu(layer_1)    # Hidden layer with RELU activation    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])    layer_2 = tf.nn.relu(layer_2)    layer_2 = tf.nn.dropout(layer_2, 0.5)    # Output layer with linear activation    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']    return out_layer# Store layers weight & biasweights = {    'h1': tf.get_variable("wh1", shape=[n_input, n_hidden_1],           initializer=tf.contrib.layers.xavier_initializer()),    'h2': tf.get_variable("wh2", shape=[n_hidden_1, n_hidden_2],           initializer=tf.contrib.layers.xavier_initializer()),    'out': tf.get_variable("wout", shape=[n_hidden_2, n_classes],           initializer=tf.contrib.layers.xavier_initializer())}biases = {    'b1': tf.Variable(tf.zeros([n_hidden_1])),    'b2': tf.Variable(tf.zeros([n_hidden_2])),    'out': tf.Variable(tf.zeros([n_classes]))}# Construct modelpred = multilayer_perceptron(x, weights, biases)# Define loss and optimizercost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred, labels=y))optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)# Initializing the variablesinit = tf.global_variables_initializer()video_level_record_path="./video_level/train/" #NEED CHANGEsaver = tf.train.Saver()summ = tf.Summary()# Launch the graphcheckpoint_dir="./ckp" #NEED CHANGEwith tf.Session() as sess:    writer = tf.summary.FileWriter(checkpoint_dir, sess.graph)    if os.path.exists (checkpoint_dir):        ckpt = tf.train.get_checkpoint_state(checkpoint_dir)        saver.restore(sess, ckpt.model_checkpoint_path)     else:         sess.run(init)    # Training cycle    avg_cost = 0.    i = 0    total = 0    batches = 0    binary_labels_list = np.array([])    data_list = np.array([])    for epoch in range(training_epochs):        for video_level_record in os.listdir(video_level_record_path):            video_level_record = os.path.join (video_level_record_path, video_level_record)            for example in tf.python_io.tf_record_iterator(video_level_record):                if  i < batch_size:                    tf_example = tf.train.Example.FromString(example)                    #print (i, tf_example.features.feature['video_id'].bytes_list.value[0].decode(encoding='UTF-8'))                    labels = tf_example.features.feature['labels'].int64_list.value                    binary_labels = np.asarray([0] * n_classes)    for label in labels:        binary_labels[label] = 1    binary_labels = binary_labels.reshape(1, n_classes)    binary_labels = np.asarray(binary_labels)    data = np.asarray(tf_example.features.feature['mean_rgb'].float_list.value).reshape(1, n_input)    if len(binary_labels_list) == 0:         binary_labels_list = binary_labels    binary_labels_list = np.vstack([binary_labels_list, binary_labels])    if len(data_list) == 0:        data_list = data    data_list = np.vstack([data_list, data])    i += 1    total += 1else:    if len(data_list) > 0 and len(binary_labels_list) > 0:                        # Run optimization op (backprop) and cost op (to get loss value)                        _, c = sess.run([optimizer, cost], feed_dict={x: data_list,                                                      y: binary_labels_list})                        avg_cost = c     i = 0    binary_labels_list = np.array([])    data_list = np.array([])    batches += 1                    if batches % display_step == 0:                        print("Epoch:", '%04d' % (epoch+1), \                              "Iter:", "%d" % (batches), \                              "cost=", str(avg_cost))                        summ.value.add(tag="cost", simple_value = avg_cost)                        writer.add_summary (summ, batches)                    if batches % 100 == 0:                        save_path = saver.save(sess, checkpoint_dir + "/model.ckpt-" + str(batches))                        print "saved", save_path    print("Optimization Finished!")    # Test model    #correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))    # Calculate accuracy    #accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))    #print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))