Wu Dao Kou Subway

alt none

NV12 vs YV12

NV12

alt none

YV12

alt none

Zhang

alt none

有监督学习Tensorflow代码框架

import tensorflow as tf

def inference(X):

def loss(X, Y):

def inputs():

def train(total_loss):

def evaluate(sess, X, Y):

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    X, Y = inputs()
    total_loss = loss(X, Y)
    train_op = train(total_loss)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    training_steps = 1000000
    for step in range(training_steps):
        sess.run([train_op])
        
        if step % 10000 == 0:
            print("loss: ", sess.run([total_loss]))

    evaluate(sess, X, Y)

    coord.request_stop()
    coord.join(threads)

    sess.close()

eg:

import tensorflow as tf

W = tf.Variable(tf.zeros([2, 1]), name="weight")
b = tf.Variable(0., name="bias")

def inference(X):
    return tf.matmul(X, W) + b

def loss(X, Y):
    Y_predicted = inference(X)
    return tf.reduce_sum(tf.squared_difference(Y, Y_predicted))

def inputs():
    weight_age = [[84, 46], [73, 20], [65, 52], [70, 30],
                  [76, 57], [69, 25], [63, 28], [72, 36],
                  [79, 57], [75, 44], [27, 24], [89, 31], 
                  [65, 52], [57, 23], [59, 60], [69, 48],
                  [60, 34], [79, 51], [75, 50], [82, 34], 
                  [59, 46], [67, 23], [85, 37], [55, 40], 
                  [63, 30]]
    blood_fat_content = [354, 190, 405, 263, 451, 302, 288,
                         385, 402, 365, 209, 290, 346, 254,
                         395, 434, 220, 374, 308, 220, 311,
                         181, 274, 303, 244]
    return tf.to_float(weight_age), tf.to_float(blood_fat_content)

def train(total_loss):
    learning_rate = 0.0000001
    return tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)

def evaluate(sess, X, Y):
    print(sess.run(inference([[80., 25.]])))
    print(sess.run(inference([[65., 25.]])))

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    X, Y = inputs()
    total_loss = loss(X, Y)
    train_op = train(total_loss)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    training_steps = 1000000
    for step in range(training_steps):
        sess.run([train_op])
        
        if step % 10000 == 0:
            print("loss: ", sess.run([total_loss]))

    evaluate(sess, X, Y)

    coord.request_stop()
    coord.join(threads)

    sess.close()

Ref: 《面向机器智能的Tensorflow实践》 41%

TensorFlow 实战Google深度学习框架勘误

《TensorFlow:实战Google深度学习框架》7.3.2 输入文件队列章节中多线程读取记录的代码如下:

import tensorflow as tf

files = tf.train.match_filenames_once("/tmp/data.tfrecords-*")
filename_queue = tf.train.string_input_producer(files, shuffle=False)

reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,
                features={
                    'i': tf.FixedLenFeature([], tf.int64),
                    'j': tf.FixedLenFeature([], tf.int64),
                })
with tf.Session() as sess:
    tf.global_variables_initializer().run()
   
    print([str(i.name) for i in tf.local_variables()])

    print(sess.run(files))

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    for i in range(6):
        print(sess.run([features['i'], features['j']]))

    coord.request_stop()
    coord.join(threads)

运行上述代码会报如下错误:

tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value matching_filenames
         [[Node: _retval_matching_filenames_0_0 = _Retval[T=DT_STRING, index=0, _device="/job:localhost/replica:0/task:0/device:CPU:0"](matching_filenames)]]

很诡异的代码中好像没有matching_filenames这个本地变量,但是如果看一下match_filenames_once这个函数的话就会发现问题所在了:

@tf_export("train.match_filenames_once")
def match_filenames_once(pattern, name=None):
  """Save the list of files matching pattern, so it is only computed once.

  NOTE: The order of the files returned can be non-deterministic.

  Args:
    pattern: A file pattern (glob), or 1D tensor of file patterns.
    name: A name for the operations (optional).

  Returns:
    A variable that is initialized to the list of files matching the pattern(s).
  """
  with ops.name_scope(name, "matching_filenames", [pattern]) as name:
    return vs.variable(
        name=name, initial_value=io_ops.matching_files(pattern),
        trainable=False, validate_shape=False,
        collections=[ops.GraphKeys.LOCAL_VARIABLES])

所以解决方法很简单:

...
with tf.Session() as sess:
    tf.global_variables_initializer().run()
    tf.local_variables_initializer().run()
...