├── Chapter3 ├── 3.1 │ ├── 3.1.3 │ │ └── simple_ops.py │ └── 3.1.4 │ │ ├── add_example.py │ │ ├── calc_depend.py │ │ ├── calc_graph │ │ └── events.out.tfevents.1487481603.DESKTOP-9UQFN7J │ │ ├── constant_print.py │ │ └── print_ops.py ├── 3.2 │ ├── 3.2.1 │ │ ├── initialize_value.py │ │ └── reshape_tensor.py │ ├── 3.2.2 │ │ └── variable_dim.py │ └── 3.2.3 │ │ ├── share_variables.py │ │ └── share_variables2.py ├── 3.5 │ ├── 3.5.1 │ │ └── placeholder_example.py │ └── 3.5.2 │ │ ├── file0.bin │ │ ├── file0.csv │ │ ├── file0.tfrecords │ │ ├── file1.bin │ │ ├── file1.csv │ │ ├── file1.tfrecords │ │ ├── gen_binary_data.py │ │ ├── gen_tfrecord_data.py │ │ ├── read_binary_data.py │ │ ├── read_csv.py │ │ └── read_tfrecord_data.py ├── 3.6 │ ├── 3.6.1 │ │ ├── README.md │ │ └── graph_display.py │ └── 3.6.2 │ │ ├── visual_learning.py │ │ └── visual_learning2.py └── 3.7 │ └── test.py ├── Chapter4 └── 4.7 │ └── sin_example.py ├── Chapter5 └── 5.5 │ ├── data │ ├── test │ │ ├── 0 │ │ │ ├── 001.png │ │ │ ├── 002.png │ │ │ ├── 003.png │ │ │ ├── 004.png │ │ │ ├── 005.png │ │ │ ├── 006.png │ │ │ ├── 007.png │ │ │ ├── 008.png │ │ │ ├── 009.png │ │ │ └── 010.png │ │ └── 1 │ │ │ ├── 1000.png │ │ │ ├── 1001.png │ │ │ ├── 1002.png │ │ │ ├── 1003.png │ │ │ ├── 1004.png │ │ │ ├── 1005.png │ │ │ ├── 1006.png │ │ │ ├── 1007.png │ │ │ ├── 1008.png │ │ │ ├── 1009.png │ │ │ └── 1010.png │ └── train │ │ ├── 0 │ │ ├── 000001.png │ │ ├── 0000010.png │ │ ├── 0000011.png │ │ ├── 0000012.png │ │ ├── 0000013.png │ │ ├── 0000014.png │ │ ├── 0000015.png │ │ ├── 0000016.png │ │ ├── 0000017.png │ │ ├── 0000018.png │ │ ├── 0000019.png │ │ ├── 000002.png │ │ ├── 0000020.png │ │ ├── 0000021.png │ │ ├── 0000022.png │ │ ├── 0000023.png │ │ ├── 0000024.png │ │ ├── 0000025.png │ │ ├── 0000026.png │ │ ├── 0000027.png │ │ ├── 0000028.png │ │ ├── 0000029.png │ │ ├── 000003.png │ │ ├── 0000030.png │ │ ├── 0000031.png │ │ ├── 0000032.png │ │ ├── 0000033.png │ │ ├── 0000034.png │ │ ├── 0000035.png │ │ ├── 0000036.png │ │ ├── 0000037.png │ │ ├── 0000038.png │ │ ├── 0000039.png │ │ ├── 000004.png │ │ ├── 0000040.png │ │ ├── 0000041.png │ │ ├── 0000042.png │ │ ├── 0000043.png │ │ ├── 0000044.png │ │ ├── 0000045.png │ │ ├── 0000046.png │ │ ├── 0000047.png │ │ ├── 0000048.png │ │ ├── 0000049.png │ │ ├── 000005.png │ │ ├── 0000050.png │ │ ├── 000006.png │ │ ├── 000007.png │ │ ├── 000008.png │ │ └── 000009.png │ │ └── 1 │ │ ├── 100001.png │ │ ├── 100002.png │ │ ├── 100003.png │ │ ├── 100004.png │ │ ├── 100005.png │ │ ├── 100006.png │ │ ├── 100007.png │ │ ├── 100008.png │ │ ├── 100009.png │ │ ├── 100010.png │ │ ├── 100011.png │ │ ├── 100012.png │ │ ├── 100013.png │ │ ├── 100014.png │ │ ├── 100015.png │ │ ├── 100016.png │ │ ├── 100017.png │ │ ├── 100018.png │ │ ├── 100019.png │ │ ├── 100020.png │ │ ├── 100021.png │ │ ├── 100022.png │ │ ├── 100023.png │ │ ├── 100024.png │ │ ├── 100025.png │ │ ├── 100026.png │ │ ├── 100027.png │ │ ├── 100028.png │ │ ├── 100029.png │ │ ├── 100030.png │ │ ├── 100031.png │ │ ├── 100032.png │ │ ├── 100033.png │ │ ├── 100034.png │ │ ├── 100035.png │ │ ├── 100036.png │ │ ├── 100037.png │ │ ├── 100038.png │ │ ├── 100039.png │ │ ├── 100040.png │ │ ├── 100041.png │ │ ├── 100042.png │ │ ├── 100043.png │ │ ├── 100044.png │ │ ├── 100045.png │ │ ├── 100046.png │ │ ├── 100047.png │ │ ├── 100048.png │ │ ├── 100049.png │ │ └── 100050.png │ ├── display.py │ ├── gen_tfrecord.py │ ├── onezero.py │ └── tfrecord_data │ ├── test.tfrecord │ └── train.tfrecord ├── Chapter6 ├── asr_lstm_ctc │ ├── asr_lstm_ctc.py │ ├── audio.wav │ └── label.txt └── wordseg │ ├── embeding.txt │ ├── msr_test.utf8 │ ├── msr_test_gold.convert │ ├── msr_test_gold.utf8 │ ├── msr_training.convert │ ├── msr_training.utf8 │ └── wordlstmseg.py └── README.md /Chapter3/3.1/3.1.3/simple_ops.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import tensorflow as tf 3 | 4 | a = tf.Variable(1.0, name="a") 5 | b = tf.add(a, 1, name="b") 6 | c = tf.add(b, 1, name="c") 7 | d = tf.add(b, 10, name="d") 8 | 9 | summary_writer = tf.summary.FileWriter('./calc_graph' ) 10 | graph = tf.get_default_graph() 11 | summary_writer.add_graph(graph) 12 | summary_writer.flush() 13 | 14 | 15 | -------------------------------------------------------------------------------- /Chapter3/3.1/3.1.4/add_example.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | v1 = tf.constant(1, name="value1") 4 | v2 = tf.constant(1, name="value2") 5 | add_op = tf.add(v1, v2, name="add_op_name") 6 | 7 | with tf.Session() as sess: 8 | result = sess.run(add_op) 9 | print("1 + 1 = %.0f" % result) 10 | -------------------------------------------------------------------------------- /Chapter3/3.1/3.1.4/calc_depend.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import tensorflow as tf 3 | 4 | x = tf.Variable(0.0, name="x") 5 | x_plus_1 = tf.assign_add(x, 1, name="x_plus") 6 | 7 | with tf.control_dependencies([x_plus_1]): 8 | y = tf.identity(x, name="y") 9 | 10 | init = tf.global_variables_initializer() 11 | with tf.Session() as sess: 12 | sess.run(init) 13 | for i in range(5): 14 | print(y.eval()) 15 | 16 | summary_writer = tf.summary.FileWriter('./calc_graph' ) 17 | graph = tf.get_default_graph() 18 | summary_writer.add_graph(graph) 19 | summary_writer.flush() -------------------------------------------------------------------------------- /Chapter3/3.1/3.1.4/calc_graph/events.out.tfevents.1487481603.DESKTOP-9UQFN7J: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter3/3.1/3.1.4/calc_graph/events.out.tfevents.1487481603.DESKTOP-9UQFN7J -------------------------------------------------------------------------------- /Chapter3/3.1/3.1.4/constant_print.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | v1 = tf.constant(1, name="value1") 3 | print(v1) 4 | -------------------------------------------------------------------------------- /Chapter3/3.1/3.1.4/print_ops.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | v1 = tf.constant(1, name="value1") 4 | v2 = tf.constant(1, name="value2") 5 | add_op = tf.add(v1, v2, name="add_op_name") 6 | 7 | graph = tf.get_default_graph() 8 | operations = graph.get_operations() 9 | print("number of operations: %d" % len(operations)) 10 | print("operations:") 11 | for op in operations: 12 | print(op) 13 | -------------------------------------------------------------------------------- /Chapter3/3.2/3.2.1/initialize_value.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter3/3.2/3.2.1/initialize_value.py -------------------------------------------------------------------------------- /Chapter3/3.2/3.2.1/reshape_tensor.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | v = tf.Variable([1,2,3,4,5,6,7,8,9]) 4 | reshaped_v = tf.reshape(v,[3,3]) 5 | init = tf.global_variables_initializer() 6 | with tf.Session() as sess: 7 | sess.run(init) 8 | print("v's value is:") 9 | print(sess.run(v)) 10 | print("reshaped value is:") 11 | print(sess.run(reshaped_v)) 12 | -------------------------------------------------------------------------------- /Chapter3/3.2/3.2.2/variable_dim.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | value_shape0 = tf.Variable(8672) 4 | value_shape1 = tf.Variable([1.1,2.2,3.3]) 5 | value_shape2 = tf.Variable([[1,2],[3,4],[5,6]]) 6 | value_shape3 = tf.Variable([[[1],[2],[3]],[[4],[5],[6]]]) 7 | 8 | print(value_shape0.get_shape()) 9 | print(value_shape1.get_shape()) 10 | print(value_shape2.get_shape()) 11 | print(value_shape3.get_shape()) 12 | -------------------------------------------------------------------------------- /Chapter3/3.2/3.2.3/share_variables.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | # 获取训练数据和测试数据 6 | def get_data(number): 7 | list_x = [] 8 | list_label = [] 9 | for i in range(number): 10 | x = np.random.randn(1) 11 | # 这里构建数据的分布满足 y = 2 * x + 10 12 | label = 2 * x + np.random.randn(1) * 0.01 + 10 13 | list_x.append(x) 14 | list_label.append(label) 15 | return list_x, list_label 16 | 17 | def inference(x): 18 | weight = tf.Variable(0.01, name="weight") 19 | bias = tf.Variable(0.01, name="bias") 20 | y = x * weight + bias 21 | return y 22 | 23 | train_x = tf.placeholder(tf.float32) 24 | train_label = tf.placeholder(tf.float32) 25 | test_x = tf.placeholder(tf.float32) 26 | test_label = tf.placeholder(tf.float32) 27 | 28 | with tf.variable_scope("inference"): 29 | train_y = inference(train_x) 30 | test_y = inference(test_x) 31 | 32 | train_loss = tf.square(train_y - train_label) 33 | test_loss = tf.square(test_y - test_label) 34 | opt = tf.train.GradientDescentOptimizer(0.002) 35 | train_op = opt.minimize(train_loss) 36 | 37 | init = tf.global_variables_initializer() 38 | 39 | train_data_x, train_data_label = get_data(1000) #读取训练数据的函数 40 | test_data_x, test_data_label = get_data(1) 41 | 42 | with tf.Session() as sess: 43 | sess.run(init) 44 | for i in range(1000): 45 | sess.run(train_op, feed_dict={train_x: train_data_x[i], 46 | train_label:train_data_label[i]}) 47 | if i % 10 == 0: 48 | test_loss_value = sess.run(test_loss, 49 | feed_dict={test_x:test_data_x[0], 50 | test_label: test_data_label[0]}) 51 | print("step %d eval loss is %.3f" %(i,test_loss_value)) 52 | -------------------------------------------------------------------------------- /Chapter3/3.2/3.2.3/share_variables2.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | # 获取训练数据和测试数据 6 | def get_data(number): 7 | list_x = [] 8 | list_label = [] 9 | for i in range(number): 10 | x = np.random.randn(1) 11 | # 这里构建数据的分布满足 y = 2 * x + 10 12 | label = 2 * x + np.random.randn(1) * 0.01 + 10 13 | list_x.append(x) 14 | list_label.append(label) 15 | return list_x, list_label 16 | 17 | def inference(x): 18 | weight = tf.get_variable("weight",[1]) 19 | bias = tf.get_variable("bias",[1]) 20 | y = x * weight + bias 21 | return y 22 | 23 | train_x = tf.placeholder(tf.float32) 24 | train_label = tf.placeholder(tf.float32) 25 | test_x = tf.placeholder(tf.float32) 26 | test_label = tf.placeholder(tf.float32) 27 | 28 | with tf.variable_scope("inference"): 29 | train_y = inference(train_x) 30 | tf.get_variable_scope().reuse_variables() 31 | test_y = inference(test_x) 32 | 33 | train_loss = tf.square(train_y - train_label) 34 | test_loss = tf.square(test_y - test_label) 35 | opt = tf.train.GradientDescentOptimizer(0.002) 36 | train_op = opt.minimize(train_loss) 37 | 38 | init = tf.global_variables_initializer() 39 | 40 | train_data_x, train_data_label = get_data(1000) #读取训练数据的函数 41 | test_data_x, test_data_label = get_data(1) 42 | 43 | with tf.Session() as sess: 44 | sess.run(init) 45 | for i in range(1000): 46 | sess.run(train_op, feed_dict={train_x: train_data_x[i], 47 | train_label:train_data_label[i]}) 48 | if i % 10 == 0: 49 | test_loss_value = sess.run(test_loss, 50 | feed_dict={test_x:test_data_x[0], 51 | test_label: test_data_label[0]}) 52 | print("step %d eval loss is %.3f" %(i,test_loss_value)) 53 | -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.1/placeholder_example.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import tensorflow as tf 3 | 4 | v1 = tf.placeholder(tf.float32) 5 | v2 = tf.placeholder(tf.float32) 6 | v_mul = tf.multiply(v1,v2) 7 | 8 | with tf.Session() as sess: 9 | while True: 10 | 11 | value1 = input("value1: ") 12 | value2 = input("value2: ") 13 | 14 | mul_result = sess.run(v_mul,feed_dict={v1:value1,v2:value2}) 15 | print(mul_result) 16 | -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.2/file0.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter3/3.5/3.5.2/file0.bin -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.2/file0.csv: -------------------------------------------------------------------------------- 1 | 1.0,2.0 2 | 1.0,3.0 3 | 1.0,4.0 -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.2/file0.tfrecords: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter3/3.5/3.5.2/file0.tfrecords -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.2/file1.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter3/3.5/3.5.2/file1.bin -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.2/file1.csv: -------------------------------------------------------------------------------- 1 | 2.0,2.0 2 | 2.0,3.0 3 | 3.0,4.0 -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.2/file1.tfrecords: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter3/3.5/3.5.2/file1.tfrecords -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.2/gen_binary_data.py: -------------------------------------------------------------------------------- 1 | import struct 2 | import codecs 3 | 4 | filename0 = "file0.bin" 5 | 6 | with codecs.open(filename0,'wb') as fw: 7 | for i in range(10): 8 | str1 = struct.pack('f',i) 9 | fw.write(str1) 10 | 11 | filename1 = "file1.bin" 12 | 13 | with codecs.open(filename1,'wb') as fw: 14 | for i in range(10,20): 15 | str1 = struct.pack('f',i) 16 | fw.write(str1) 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.2/gen_tfrecord_data.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | def _int64_feature(value): 4 | return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) 5 | 6 | def _bytes_feature(value): 7 | return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) 8 | 9 | if __name__ == "__main__": 10 | filename0 = "file0.tfrecords" 11 | print('Writing', filename0) 12 | writer = tf.python_io.TFRecordWriter(filename0) 13 | for index in range(10): 14 | example = tf.train.Example(features=tf.train.Features(feature={ 15 | 'v1': _int64_feature(index), 16 | 'v2': _int64_feature(index + 1)})) 17 | writer.write(example.SerializeToString()) 18 | writer.close() 19 | 20 | filename1 = "file1.tfrecords" 21 | writer = tf.python_io.TFRecordWriter(filename1) 22 | for index in range(10, 20): 23 | example = tf.train.Example(features=tf.train.Features(feature={ 24 | 'v1': _int64_feature(index), 25 | 'v2': _int64_feature(index + 1)})) 26 | writer.write(example.SerializeToString()) 27 | writer.close() 28 | -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.2/read_binary_data.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | #将文件名列表传入 4 | filename_queue = tf.train.string_input_producer(["file0.bin", "file1.bin"],shuffle=True,num_epochs=2) 5 | 6 | # 采用读取固定长度二进制数据的reader,一次读入2个float数 7 | reader = tf.FixedLengthRecordReader(record_bytes=2*4) 8 | key, value = reader.read(filename_queue) 9 | 10 | # 将读入的数据按照float32的大小解码 11 | decode_value = tf.decode_raw(value, tf.float32) 12 | v1 = decode_value[0] 13 | v2 = decode_value[1] 14 | v_mul = tf.multiply(v1,v2) 15 | 16 | init_op = tf.global_variables_initializer() 17 | local_init_op = tf.local_variables_initializer() 18 | 19 | # 创建会话 20 | sess = tf.Session() 21 | 22 | # 初始化变量 23 | sess.run(init_op) 24 | sess.run(local_init_op) 25 | 26 | # 输入数据进入队列 27 | coord = tf.train.Coordinator() 28 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 29 | 30 | try: 31 | while not coord.should_stop(): 32 | value1, value2, mul_result = sess.run([v1,v2,v_mul]) 33 | print("%f\t%f\t%f"%(value1, value2, mul_result)) 34 | 35 | except tf.errors.OutOfRangeError: 36 | print('Done training -- epoch limit reached') 37 | finally: 38 | coord.request_stop() 39 | 40 | # 等待线程结束 41 | coord.join(threads) 42 | sess.close() 43 | 44 | -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.2/read_csv.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | #将文件名列表传入 4 | filename_queue = tf.train.string_input_producer(["file0.csv", "file1.csv"],shuffle=True,num_epochs=2) 5 | 6 | # 采用读文本的reader 7 | reader = tf.TextLineReader() 8 | key, value = reader.read(filename_queue) 9 | 10 | # 默认值是1.0,这里也默认指定了要读入数据的类型是float 11 | record_defaults = [[1.0], [1.0]] 12 | v1, v2 = tf.decode_csv( 13 | value, record_defaults=record_defaults) 14 | v_mul = tf.multiply(v1,v2) 15 | 16 | init_op = tf.global_variables_initializer() 17 | local_init_op = tf.local_variables_initializer() 18 | 19 | # 创建会话 20 | sess = tf.Session() 21 | 22 | # 初始化变量 23 | sess.run(init_op) 24 | sess.run(local_init_op) 25 | 26 | # 输入数据进入队列 27 | coord = tf.train.Coordinator() 28 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 29 | 30 | try: 31 | while not coord.should_stop(): 32 | value1, value2, mul_result = sess.run([v1,v2,v_mul]) 33 | print("%f\t%f\t%f"%(value1, value2, mul_result)) 34 | 35 | except tf.errors.OutOfRangeError: 36 | print('Done training -- epoch limit reached') 37 | finally: 38 | coord.request_stop() 39 | 40 | # 等待线程结束 41 | coord.join(threads) 42 | sess.close() 43 | 44 | -------------------------------------------------------------------------------- /Chapter3/3.5/3.5.2/read_tfrecord_data.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | #将文件名列表传入 4 | filename_queue = tf.train.string_input_producer(["file0.tfrecords", "file1.tfrecords"],shuffle=True,num_epochs=2) 5 | 6 | # 使用TFRecorder来读取 7 | reader = tf.TFRecordReader() 8 | _, serialized_example = reader.read(filename_queue) 9 | features = tf.parse_single_example( 10 | serialized_example, 11 | features={ 12 | 'v1': tf.FixedLenFeature([], tf.int64), 13 | 'v2': tf.FixedLenFeature([], tf.int64), 14 | }) 15 | 16 | v1 = tf.cast(features['v1'], tf.int32) 17 | v2 = tf.cast(features['v2'], tf.int32) 18 | v_mul = tf.multiply(v1,v2) 19 | print(v1.get_shape()) 20 | 21 | batch_v1, batch_v2 = tf.train.shuffle_batch([v1,v2], 22 | batch_size=10, #batch的大小 23 | num_threads=16, #处理线程数 24 | capacity=100, #队列大小 25 | min_after_dequeue=20 #出队后队列最少保留样本数 26 | ) 27 | print(batch_v1.get_shape()) 28 | 29 | init_op = tf.global_variables_initializer() 30 | local_init_op = tf.local_variables_initializer() 31 | 32 | # 创建会话 33 | sess = tf.Session() 34 | 35 | # 初始化变量 36 | sess.run(init_op) 37 | sess.run(local_init_op) 38 | 39 | # 输入数据进入队列 40 | coord = tf.train.Coordinator() 41 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 42 | 43 | try: 44 | while not coord.should_stop(): 45 | value1, value2, mul_result = sess.run([v1,v2,v_mul]) 46 | print("%f\t%f\t%f"%(value1, value2, mul_result)) 47 | 48 | except tf.errors.OutOfRangeError: 49 | print('Done training -- epoch limit reached') 50 | finally: 51 | coord.request_stop() 52 | 53 | # 等待线程结束 54 | coord.join(threads) 55 | sess.close() 56 | 57 | -------------------------------------------------------------------------------- /Chapter3/3.6/3.6.1/README.md: -------------------------------------------------------------------------------- 1 | ## 运行 2 | python graph_display.py 3 | 4 | 5 | 6 | ## tensorboard 查看 7 | 8 | tensorboard --logdir=./calc_graph -------------------------------------------------------------------------------- /Chapter3/3.6/3.6.1/graph_display.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import tensorflow as tf 3 | 4 | x = tf.constant(1.0, name="input") 5 | w = tf.Variable(0.5, name="weight") 6 | b = tf.Variable(0.1, name="biase") 7 | y = tf.add(tf.mul(x,w, name="mul_op"), b, name="add_op") 8 | 9 | summary_writer = tf.summary.FileWriter('./calc_graph' ) 10 | graph = tf.get_default_graph() 11 | summary_writer.add_graph(graph) 12 | summary_writer.flush() 13 | 14 | -------------------------------------------------------------------------------- /Chapter3/3.6/3.6.2/visual_learning.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | # 构建图 6 | x = tf.placeholder(tf.float32) 7 | y = tf.placeholder(tf.float32) 8 | weight = tf.get_variable("weight", [], tf.float32, initializer=tf.random_normal_initializer()) 9 | biase = tf.get_variable("biase", [], tf.float32, initializer=tf.random_normal_initializer()) 10 | pred = tf.add(tf.multiply(x, weight, name="mul_op"), biase, name="add_op") 11 | 12 | #损失函数 13 | loss = tf.square(y - pred, name="loss") 14 | #优化函数 15 | optimizer = tf.train.GradientDescentOptimizer(0.01) 16 | #计算梯度,应用梯度操作 17 | grads_and_vars = optimizer.compute_gradients(loss) 18 | train_op = optimizer.apply_gradients(grads_and_vars) 19 | 20 | #收集值的操作 21 | tf.summary.scalar("weight", weight) 22 | tf.summary.scalar("biase", biase) 23 | tf.summary.scalar("loss", loss[0]) 24 | 25 | merged_summary = tf.summary.merge_all() 26 | 27 | summary_writer = tf.summary.FileWriter('./log_graph' ) 28 | summary_writer.add_graph(tf.get_default_graph()) 29 | init_op = tf.global_variables_initializer() 30 | 31 | with tf.Session() as sess: 32 | sess.run(init_op) 33 | for step in range(500): 34 | train_x = np.random.randn(1) 35 | train_y = 2 * train_x + np.random.randn(1) * 0.01 + 10 36 | _, summary = sess.run([train_op, merged_summary], feed_dict={x:train_x, y:train_y}) 37 | summary_writer.add_summary(summary, step) 38 | 39 | -------------------------------------------------------------------------------- /Chapter3/3.6/3.6.2/visual_learning2.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import numpy as np 3 | import tensorflow as tf 4 | import random 5 | 6 | # 构建图 7 | x = tf.placeholder(tf.float32, shape=[2,3]) 8 | y = tf.placeholder(tf.float32, shape=[2,2]) 9 | weight = tf.get_variable("weight", [3,2], tf.float32, initializer=tf.random_normal_initializer()) 10 | biase = tf.get_variable("biase", [2,2], tf.float32, initializer=tf.random_normal_initializer()) 11 | mul_op = tf.matmul(x, weight, name="mul_op") 12 | pred = tf.add(mul_op, biase, name="add_op") 13 | 14 | #损失函数 15 | loss = tf.square(y - pred, name="loss") 16 | #优化函数 17 | optimizer = tf.train.GradientDescentOptimizer(0.003) 18 | #计算梯度,应用梯度操作 19 | grads_and_vars = optimizer.compute_gradients(loss) 20 | train_op = optimizer.apply_gradients(grads_and_vars) 21 | 22 | #收集值的操作 23 | tf.summary.histogram("weight", weight) 24 | tf.summary.histogram("biase", biase) 25 | 26 | merged_summary = tf.summary.merge_all() 27 | 28 | summary_writer = tf.summary.FileWriter('./log_graph2' ) 29 | summary_writer.add_graph(tf.get_default_graph()) 30 | init_op = tf.global_variables_initializer() 31 | 32 | with tf.Session() as sess: 33 | sess.run(init_op) 34 | for step in range(7000): 35 | train_x = [ [random.uniform(1, 10),random.uniform(1, 10) ,random.uniform(1, 10)], 36 | [random.uniform(1, 10), random.uniform(1, 10), random.uniform(1, 10)]] 37 | 38 | y1 = train_x[0][0] * 1 + train_x[0][1] * 2 + train_x[0][2] * 3 + 10 39 | y2 = train_x[0][0] * 4 + train_x[0][1] * 5 + train_x[0][2] * 6 + 20 40 | y3 = train_x[1][0] * 1 + train_x[1][1] * 2 + train_x[1][2] * 3 + 30 41 | y4 = train_x[1][0] * 4 + train_x[1][1] * 5 + train_x[1][2] * 6 + 40 42 | 43 | train_y = [[ y1, y2],[y3,y4]] 44 | _, summary,weight_value = sess.run([train_op, merged_summary, weight], feed_dict={x:train_x, y:train_y}) 45 | summary_writer.add_summary(summary, step) 46 | if step % 1000 == 0: 47 | for w in weight_value: 48 | print(w) 49 | print("") 50 | 51 | -------------------------------------------------------------------------------- /Chapter3/3.7/test.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import math 3 | import tensorflow as tf 4 | 5 | 6 | def inference(x): 7 | with tf.name_scope('hidden1'): 8 | weights = tf.get_variable("weight", [5], tf.float32, initializer=tf.random_normal_initializer()) 9 | biases = tf.get_variable("biase", [5], tf.float32, initializer=tf.random_normal_initializer()) 10 | hidden1 = tf.nn.relu(tf.matmul(x, weights) + biases) 11 | 12 | with tf.name_scope('hidden2'): 13 | weights = tf.get_variable("weight", [5,5], tf.float32, initializer=tf.random_normal_initializer()) 14 | biases = tf.get_variable("biase", [5], tf.float32, initializer=tf.random_normal_initializer()) 15 | hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases) 16 | 17 | with tf.name_scope('linear'): 18 | weights = tf.get_variable("weight", [5,1], tf.float32, initializer=tf.random_normal_initializer()) 19 | biases = tf.get_variable("biase", [1], tf.float32, initializer=tf.random_normal_initializer()) 20 | logits = tf.matmul(hidden2, weights) + biases 21 | 22 | return logits 23 | 24 | # 构建图 25 | x = tf.placeholder(tf.float32, shape=[5]) 26 | label = tf.placeholder(tf.float32, shape=[1]) 27 | 28 | y = inference(x) 29 | 30 | #损失函数 31 | loss = tf.square(y - label, name="loss") 32 | #优化函数 33 | optimizer = tf.train.GradientDescentOptimizer(0.01) 34 | #计算梯度,应用梯度操作 35 | grads_and_vars = optimizer.compute_gradients(loss) 36 | train_op = optimizer.apply_gradients(grads_and_vars) 37 | 38 | init_op = tf.global_variables_initializer() 39 | 40 | with tf.Session() as sess: 41 | sess.run(init_op) 42 | for step in range(500): 43 | train_x = np.random.randn(1) 44 | train_y = math.sin(train_x) 45 | loss_value, _ = sess.run([loss,train_op], feed_dict={x:train_x, label:train_y}) 46 | print("step: %d loss %f" %(step, loss_value)) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Chapter4/4.7/sin_example.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | 3 | import tensorflow as tf 4 | import math 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | import types 8 | import pylab 9 | 10 | ''' 11 | 用tensorflow来拟合一个正弦函数 12 | 13 | ''' 14 | 15 | def draw_correct_line(): 16 | ''' 17 | 绘制标准的sin的曲线 18 | ''' 19 | x = np.arange(0, 2 * np.pi, 0.01) 20 | x = x.reshape((len(x), 1)) 21 | y = np.sin(x) 22 | 23 | pylab.plot(x,y, label = '标准sin曲线') 24 | plt.axhline(linewidth=1, color='r') 25 | 26 | 27 | 28 | def get_train_data(): 29 | '''返回一个训练样本(train_x, train_y), 30 | 其中train_x是随机的自变量, train_y是train_x的sin函数值 31 | ''' 32 | train_x = np.random.uniform(0.0, 2 * np.pi, (1)) 33 | train_y = np.sin(train_x) 34 | return train_x, train_y 35 | 36 | 37 | def inference(input_data): 38 | ''' 39 | 定义前向计算的网络结构 40 | Args: 41 | 输入的x的值,单个值 42 | ''' 43 | with tf.variable_scope('hidden1'): 44 | #第一个隐藏层,采用16个隐藏节点 45 | weights = tf.get_variable("weight", [1, 16], tf.float32, 46 | initializer=tf.random_normal_initializer(0.0, 1)) 47 | biases = tf.get_variable("biase", [1, 16], tf.float32, 48 | initializer=tf.random_normal_initializer(0.0, 1)) 49 | hidden1 = tf.sigmoid(tf.multiply(input_data, weights) + biases) 50 | 51 | with tf.variable_scope('hidden2'): 52 | #第二个隐藏层,采用16个隐藏节点 53 | weights = tf.get_variable("weight", [16, 16], tf.float32, 54 | initializer=tf.random_normal_initializer(0.0, 1)) 55 | biases = tf.get_variable("biase", [16], tf.float32, 56 | initializer=tf.random_normal_initializer(0.0, 1)) 57 | mul = tf.matmul(hidden1, weights) 58 | hidden2 = tf.sigmoid(mul + biases) 59 | 60 | with tf.variable_scope('hidden3'): 61 | #第三个隐藏层,采用16个隐藏节点 62 | weights = tf.get_variable("weight", [16, 16], tf.float32, 63 | initializer=tf.random_normal_initializer(0.0, 1)) 64 | biases = tf.get_variable("biase", [16], tf.float32, 65 | initializer=tf.random_normal_initializer(0.0, 1)) 66 | hidden3 = tf.sigmoid(tf.matmul(hidden2, weights) + biases) 67 | 68 | with tf.variable_scope('output_layer'): 69 | #输出层 70 | weights = tf.get_variable("weight", [16, 1], tf.float32, 71 | initializer=tf.random_normal_initializer(0.0, 1)) 72 | biases = tf.get_variable("biase", [1], tf.float32, 73 | initializer=tf.random_normal_initializer(0.0, 1)) 74 | output = tf.matmul(hidden3, weights) + biases 75 | 76 | return output 77 | 78 | 79 | def train(): 80 | #学习率 81 | learning_rate = 0.01 82 | 83 | x = tf.placeholder(tf.float32) 84 | y = tf.placeholder(tf.float32) 85 | 86 | net_out = inference(x) 87 | 88 | #定义损失函数的op 89 | loss = tf.square(net_out - y) 90 | 91 | #采用随机梯度下降的优化函数 92 | opt = tf.train.GradientDescentOptimizer(learning_rate) 93 | train_op = opt.minimize(loss) 94 | 95 | init = tf.global_variables_initializer() 96 | 97 | with tf.Session() as sess: 98 | sess.run(init) 99 | print("start traing....") 100 | for i in range(1000000): 101 | train_x, train_y = get_train_data() 102 | sess.run(train_op, feed_dict={x: train_x, y: train_y}) 103 | 104 | if i % 10000 == 0: 105 | times = int(i / 10000) 106 | test_x_ndarray = np.arange(0, 2 * np.pi, 0.01) 107 | test_y_ndarray = np.zeros([len(test_x_ndarray)]) 108 | ind = 0 109 | for test_x in test_x_ndarray: 110 | test_y = sess.run(net_out, feed_dict={x: test_x, y: 1}) 111 | np.put(test_y_ndarray, ind, test_y) 112 | ind += 1 113 | # 先绘制标准的sin函数的曲线, 114 | # 再用虚线绘制我们计算出来模拟sin函数的曲线 115 | draw_correct_line() 116 | pylab.plot(test_x_ndarray,test_y_ndarray,'--', label = str(times) + 'times' ) 117 | pylab.show() 118 | 119 | if __name__ == "__main__": 120 | train() 121 | 122 | -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/0/001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/0/001.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/0/002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/0/002.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/0/003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/0/003.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/0/004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/0/004.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/0/005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/0/005.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/0/006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/0/006.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/0/007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/0/007.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/0/008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/0/008.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/0/009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/0/009.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/0/010.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/0/010.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/1/1000.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/1/1000.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/1/1001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/1/1001.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/1/1002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/1/1002.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/1/1003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/1/1003.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/1/1004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/1/1004.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/1/1005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/1/1005.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/1/1006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/1/1006.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/1/1007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/1/1007.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/1/1008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/1/1008.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/1/1009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/1/1009.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/test/1/1010.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/test/1/1010.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/000001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/000001.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000010.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000010.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000011.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000011.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000012.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000012.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000013.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000013.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000014.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000014.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000015.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000015.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000016.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000016.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000017.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000017.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000018.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000019.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000019.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/000002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/000002.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000020.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000020.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000021.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000021.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000022.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000022.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000023.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000023.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000024.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000025.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000025.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000026.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000026.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000027.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000027.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000028.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000028.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000029.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000029.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/000003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/000003.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000030.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000030.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000031.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000031.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000032.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000032.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000033.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000033.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000034.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000034.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000035.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000035.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000036.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000036.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000037.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000037.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000038.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000038.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000039.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000039.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/000004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/000004.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000040.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000040.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000041.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000041.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000042.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000042.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000043.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000043.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000044.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000044.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000045.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000045.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000046.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000046.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000047.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000047.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000048.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000048.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000049.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000049.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/000005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/000005.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/0000050.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/0000050.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/000006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/000006.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/000007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/000007.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/000008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/000008.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/0/000009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/0/000009.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100001.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100002.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100003.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100004.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100005.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100006.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100007.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100008.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100009.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100010.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100010.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100011.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100011.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100012.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100012.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100013.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100013.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100014.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100014.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100015.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100015.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100016.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100016.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100017.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100017.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100018.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100019.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100019.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100020.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100020.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100021.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100021.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100022.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100022.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100023.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100023.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100024.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100025.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100025.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100026.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100026.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100027.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100027.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100028.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100028.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100029.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100029.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100030.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100030.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100031.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100031.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100032.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100032.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100033.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100033.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100034.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100034.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100035.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100035.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100036.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100036.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100037.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100037.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100038.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100038.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100039.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100039.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100040.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100040.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100041.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100041.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100042.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100042.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100043.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100043.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100044.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100044.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100045.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100045.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100046.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100046.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100047.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100047.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100048.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100048.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100049.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100049.png -------------------------------------------------------------------------------- /Chapter5/5.5/data/train/1/100050.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/data/train/1/100050.png -------------------------------------------------------------------------------- /Chapter5/5.5/display.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | 3 | import tensorflow as tf 4 | import os 5 | 6 | 7 | FLAGS = tf.app.flags.FLAGS 8 | tf.app.flags.DEFINE_float('gpu_memory_fraction', 0.02, 'gpu占用内存比例') 9 | tf.app.flags.DEFINE_integer('batch_size', 10, 'batch_size大小') 10 | tf.app.flags.DEFINE_string('model_dir', "./model/", '保存模型的文件夹') 11 | tf.app.flags.DEFINE_string('event_dir', "./event/", '保存模型的文件夹') 12 | 13 | def weight_init(shape, name): 14 | ''' 15 | 获取某个shape大小的参数 16 | ''' 17 | return tf.get_variable(name, shape, initializer=tf.random_normal_initializer(mean=0.0, stddev=0.1)) 18 | 19 | def bias_init(shape, name): 20 | return tf.get_variable(name, shape, initializer=tf.constant_initializer(0.0)) 21 | 22 | def conv2d(x,conv_w): 23 | return tf.nn.conv2d(x, conv_w, strides=[1, 1, 1, 1], padding='VALID') 24 | 25 | def max_pool(x, size): 26 | return tf.nn.max_pool(x, ksize=[1,size,size,1], strides = [1,size,size,1], padding='VALID') 27 | 28 | 29 | 30 | def read_and_decode(filename_queue): 31 | reader = tf.TFRecordReader() 32 | _, serialized_example = reader.read(filename_queue) 33 | features = tf.parse_single_example( 34 | serialized_example, 35 | features={ 36 | 'height': tf.FixedLenFeature([], tf.int64), 37 | 'width': tf.FixedLenFeature([], tf.int64), 38 | 'channels': tf.FixedLenFeature([], tf.int64), 39 | 'image_data': tf.FixedLenFeature([], tf.string), 40 | 'label': tf.FixedLenFeature([], tf.int64), 41 | }) 42 | 43 | image = tf.decode_raw(features['image_data'], tf.uint8) 44 | image = tf.reshape(image, [100, 100, 3]) 45 | 46 | image = tf.cast(image, tf.float32) 47 | 48 | label = tf.cast(features['label'], tf.int32) 49 | 50 | return image, label 51 | 52 | 53 | def inputs(filename, batch_size): 54 | with tf.name_scope('input'): 55 | filename_queue = tf.train.string_input_producer( 56 | [filename], num_epochs=2000) 57 | 58 | image, label = read_and_decode(filename_queue) 59 | 60 | images, labels = tf.train.batch( 61 | [image, label], batch_size=batch_size, num_threads=1, 62 | capacity=4) 63 | 64 | return images, labels 65 | 66 | 67 | def inference(input_data): 68 | ''' 69 | 定义网络结构、向前计算过程 70 | ''' 71 | with tf.name_scope('conv1'): 72 | w_conv1 = weight_init([10,10,3,16], 'conv1_w') 73 | b_conv1 = bias_init([16], 'conv1_b') 74 | 75 | #卷积之后图片大小变成100-10+1 = 91 76 | h_conv1 = tf.nn.relu(conv2d(input_data, w_conv1) + b_conv1) 77 | #池化之后图片大小变成45 78 | h_pool1 = max_pool(h_conv1, 2) #140 79 | 80 | with tf.name_scope('conv2'): 81 | w_conv2 = weight_init([5,5,16,16], 'conv2_w') 82 | b_conv2 = bias_init([16], 'conv2_b') 83 | 84 | #卷积之后图片大小变成 45 -5+1 = 41 85 | h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2) 86 | #池化之后图片大小变成20 87 | h_pool2 = max_pool(h_conv2, 2) #68 88 | 89 | with tf.name_scope('conv3'): 90 | w_conv3 = weight_init([5,5,16,16], 'conv3_w') 91 | b_conv3 = bias_init([16], 'conv3_b') 92 | 93 | #卷积之后图片大小变成 20 -5+1 = 16 94 | h_conv3 = tf.nn.relu(conv2d(h_pool2, w_conv3) + b_conv3) 95 | #池化之后图片大小变成8 96 | h_pool3 = max_pool(h_conv3, 2) # 32 97 | 98 | with tf.name_scope('fc1'): 99 | w_fc1 = weight_init([8*8*16, 128], 'fc1_w') 100 | b_fc1 = bias_init([128], 'fc1_b') 101 | 102 | h_fc = tf.nn.relu(tf.matmul(tf.reshape(h_pool3,[-1,8*8*16]), w_fc1) + b_fc1) 103 | 104 | #keep_prob = 0.8 105 | #h_fc_drop = tf.nn.dropout(h_fc,keep_prob) 106 | with tf.name_scope('fc2'): 107 | w_fc2 = weight_init([128, 2], 'fc2_w') 108 | b_fc2 = bias_init([2], 'fc2_b') 109 | 110 | h_fc2 = tf.matmul(h_fc, w_fc2) + b_fc2 111 | 112 | return h_fc2, h_conv1, h_conv2, h_conv3, h_pool3, h_fc 113 | 114 | 115 | def train(): 116 | ''' 117 | 训练过程 118 | ''' 119 | batch_size = FLAGS.batch_size 120 | train_images, train_labels = inputs("./tfrecord_data/display.tfrecord", batch_size ) 121 | 122 | with tf.variable_scope("inference") as scope: 123 | train_y_conv, h_conv1, h_conv2, h_conv3, h_pool3, h_fc1 = inference(train_images) 124 | 125 | for i in range(16): 126 | image_channel = tf.slice(h_conv1, [0,0,0,i], [10, 91, 91,1]) 127 | tf.summary.image("conv1_image", image_channel, 2) 128 | for i in range(16): 129 | image_channel = tf.slice(h_conv2, [0,0,0,i], [10, 41, 41,1]) 130 | tf.summary.image("conv2_image", image_channel, 2) 131 | for i in range(16): 132 | image_channel = tf.slice(h_conv3, [0,0,0,i], [10, 16, 16,1]) 133 | tf.summary.image("conv3_image", image_channel, 2) 134 | for i in range(16): 135 | image_channel = tf.slice(h_pool3, [0,0,0,i], [10, 8, 8,1]) 136 | tf.summary.image("pool3_image", image_channel, 2) 137 | summary_op = tf.summary.merge_all() 138 | 139 | 140 | init_op = tf.global_variables_initializer() 141 | local_init_op = tf.local_variables_initializer() 142 | 143 | saver = tf.train.Saver() 144 | 145 | gpu_options = tf.GPUOptions( 146 | per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) 147 | config = tf.ConfigProto(gpu_options=gpu_options) 148 | 149 | with tf.Session(config=config) as sess: 150 | if not os.path.exists(FLAGS.model_dir): 151 | print("model dir not exists. %s" % FLAGS.model_dir) 152 | return 153 | 154 | ckpt = tf.train.get_checkpoint_state(FLAGS.model_dir) 155 | saver.restore(sess, ckpt.model_checkpoint_path) 156 | save_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]) 157 | print("reload model from %s, save_step = %d" % (ckpt.model_checkpoint_path, save_step)) 158 | 159 | 160 | sess.run(local_init_op) 161 | 162 | summary_writer = tf.summary.FileWriter(FLAGS.event_dir, sess.graph) 163 | coord = tf.train.Coordinator() 164 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 165 | 166 | for i in range(20): 167 | h_fc1_value, summary_str = sess.run([train_y_conv, summary_op]) 168 | summary_writer.add_summary(summary_str, i) 169 | print(h_fc1_value) 170 | 171 | summary_writer.close() 172 | coord.request_stop() 173 | 174 | coord.join(threads) 175 | 176 | #conv1_data = sess.run([tf.shape(h_conv1)]) 177 | #print(conv1_data) 178 | 179 | 180 | if __name__ == "__main__": 181 | if not os.path.exists("./tfrecord_data/train.tfrecord") or \ 182 | not os.path.exists("./tfrecord_data/test.tfrecord"): 183 | gen_tfrecord_data("./data/train", "./data/test/") 184 | 185 | os.environ["CUDA_VISIBLE_DEVICES"] = "0" 186 | train() 187 | -------------------------------------------------------------------------------- /Chapter5/5.5/gen_tfrecord.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | 3 | import tensorflow as tf 4 | import os 5 | import cv2 6 | 7 | 8 | ''' 9 | 本文件由图片生成tfrecord文件,保存的信息有图片的像素信息,图片的高和宽,图片的通道数, 10 | 图片对应的label,也就是图片是1还是0. 11 | 12 | 会生成训练的tfrecord文件和验证的tfrecord文件, 13 | 还会生成一个用来展示卷积过程中图片经过卷积变化的图片的tfrecord文件。 14 | ''' 15 | 16 | def gen_tfrecord(zero_dir, one_dir, output_tfrecord_file): 17 | ''' 18 | 读取文件夹中的图片数据, 生成tfrecord格式的文件 19 | 20 | Args: 21 | zero_dir: 保存图片0的文件夹 22 | one_dir: 保存图片1的文件夹 23 | output_tfrecord_file: 输出的tfrecord文件 24 | 25 | Return: 26 | 27 | ''' 28 | tf_writer = tf.python_io.TFRecordWriter(output_tfrecord_file) 29 | 30 | #为数字0的数据 31 | for file in os.listdir(zero_dir): 32 | file_path = os.path.join(zero_dir, file) 33 | image_data = cv2.imread(file_path) 34 | image_bytes = image_data.tostring() 35 | rows = image_data.shape[0] 36 | cols = image_data.shape[1] 37 | channels = image_data.shape[2] 38 | label_data = 0 39 | 40 | example = tf.train.Example() 41 | 42 | feature = example.features.feature 43 | feature['height'].int64_list.value.append(rows) 44 | feature['width'].int64_list.value.append(cols) 45 | feature['channels'].int64_list.value.append(channels) 46 | feature['image_data'].bytes_list.value.append(image_bytes) 47 | feature['label'].int64_list.value.append(label_data) 48 | 49 | tf_writer.write(example.SerializeToString()) 50 | 51 | #为数字1的数据 52 | for file in os.listdir(one_dir): 53 | file_path = os.path.join(one_dir, file) 54 | image_data = cv2.imread(file_path) 55 | image_bytes = image_data.tostring() 56 | rows = image_data.shape[0] 57 | cols = image_data.shape[1] 58 | channels = image_data.shape[2] 59 | label_data = 1 60 | 61 | example = tf.train.Example() 62 | 63 | feature = example.features.feature 64 | feature['height'].int64_list.value.append(rows) 65 | feature['width'].int64_list.value.append(cols) 66 | feature['channels'].int64_list.value.append(channels) 67 | feature['image_data'].bytes_list.value.append(image_bytes) 68 | feature['label'].int64_list.value.append(label_data) 69 | 70 | tf_writer.write(example.SerializeToString()) 71 | 72 | tf_writer.close() 73 | 74 | 75 | def gen_display_tfrecord(data_dir, output_tfrecord_file): 76 | ''' 77 | 读取文件夹中的图片数据, 生成tfrecord格式的文件. 78 | 这个函数用来生成单独tfrecord文件, 79 | 在演示卷积过程的时候单独使用 80 | 81 | 82 | Args: 83 | data_dir: 保存图片的文件夹 84 | output_tfrecord_file: 输出的tfrecord文件 85 | 86 | Return: 87 | 88 | ''' 89 | tf_writer = tf.python_io.TFRecordWriter(output_tfrecord_file) 90 | 91 | #为数字0的数据 92 | for file in os.listdir(data_dir): 93 | file_path = os.path.join(data_dir, file) 94 | image_data = cv2.imread(file_path) 95 | image_bytes = image_data.tostring() 96 | rows = image_data.shape[0] 97 | cols = image_data.shape[1] 98 | channels = image_data.shape[2] 99 | label_data = 0 100 | 101 | example = tf.train.Example() 102 | 103 | feature = example.features.feature 104 | feature['height'].int64_list.value.append(rows) 105 | feature['width'].int64_list.value.append(cols) 106 | feature['channels'].int64_list.value.append(channels) 107 | feature['image_data'].bytes_list.value.append(image_bytes) 108 | feature['label'].int64_list.value.append(label_data) 109 | 110 | tf_writer.write(example.SerializeToString()) 111 | tf_writer.close() 112 | 113 | 114 | 115 | def gen_tfrecord_data(train_data_dir, test_data_dir): 116 | ''' 117 | 生成训练和测试的tfrecord格式的数据 118 | 119 | Args: 120 | train_data_dir: 训练图片数据的文件夹 121 | test_data_dir: 测试图片数据的文件夹 122 | 123 | Return: 124 | ''' 125 | train_data_zero_dir = os.path.join(train_data_dir, "0") 126 | train_data_one_dir = os.path.join(train_data_dir, "1") 127 | 128 | 129 | test_data_zero_dir = os.path.join(test_data_dir, "0") 130 | test_data_one_dir = os.path.join(test_data_dir, "1") 131 | 132 | output_dir = "./tfrecord_data" 133 | if not os.path.exists(output_dir): 134 | os.makedirs(output_dir) 135 | train_tfrecord_file = os.path.join(output_dir, "train.tfrecord") 136 | test_tfrecord_file = os.path.join(output_dir, "test.tfrecord") 137 | 138 | gen_tfrecord(train_data_zero_dir, train_data_one_dir, train_tfrecord_file) 139 | gen_tfrecord(test_data_zero_dir, test_data_one_dir, test_tfrecord_file) 140 | 141 | 142 | if __name__ == "__main__": 143 | gen_tfrecord_data("./data/train", "./data/test/") 144 | 145 | 146 | #生成展示卷积网络中间过程图形化的测试数据 147 | display_test_pic_dir = "./data/display" 148 | gen_display_tfrecord(display_test_pic_dir, "./tfrecord_data/display.tfrecord") 149 | print("generate tfrecord data complete!") 150 | -------------------------------------------------------------------------------- /Chapter5/5.5/onezero.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | 3 | import tensorflow as tf 4 | import os 5 | 6 | 7 | FLAGS = tf.app.flags.FLAGS 8 | tf.app.flags.DEFINE_float('gpu_memory_fraction', 0.02, 'gpu占用内存比例') 9 | tf.app.flags.DEFINE_integer('batch_size', 10, 'batch_size大小') 10 | tf.app.flags.DEFINE_integer('reload_model', 0, '是否reload之前训练好的模型') 11 | tf.app.flags.DEFINE_string('model_dir', "./model/", '保存模型的文件夹') 12 | tf.app.flags.DEFINE_string('event_dir', "./event/", '保存event数据的文件夹,给tensorboard展示用') 13 | 14 | def weight_init(shape, name): 15 | ''' 16 | 获取某个shape大小的参数 17 | ''' 18 | return tf.get_variable(name, shape, initializer=tf.random_normal_initializer(mean=0.0, stddev=0.1)) 19 | 20 | def bias_init(shape, name): 21 | return tf.get_variable(name, shape, initializer=tf.constant_initializer(0.0)) 22 | 23 | def conv2d(x,conv_w): 24 | return tf.nn.conv2d(x, conv_w, strides=[1, 1, 1, 1], padding='VALID') 25 | 26 | def max_pool(x, size): 27 | return tf.nn.max_pool(x, ksize=[1,size,size,1], strides = [1,size,size,1], padding='VALID') 28 | 29 | 30 | 31 | def read_and_decode(filename_queue): 32 | reader = tf.TFRecordReader() 33 | _, serialized_example = reader.read(filename_queue) 34 | features = tf.parse_single_example( 35 | serialized_example, 36 | features={ 37 | 'height': tf.FixedLenFeature([], tf.int64), 38 | 'width': tf.FixedLenFeature([], tf.int64), 39 | 'channels': tf.FixedLenFeature([], tf.int64), 40 | 'image_data': tf.FixedLenFeature([], tf.string), 41 | 'label': tf.FixedLenFeature([], tf.int64), 42 | }) 43 | 44 | image = tf.decode_raw(features['image_data'], tf.uint8) 45 | image = tf.reshape(image, [100, 100, 3]) 46 | 47 | image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 48 | #image = tf.cast(image, tf.float32) 49 | 50 | label = tf.cast(features['label'], tf.int32) 51 | 52 | return image, label 53 | 54 | 55 | def inputs(filename, batch_size): 56 | with tf.name_scope('input'): 57 | filename_queue = tf.train.string_input_producer( 58 | [filename], num_epochs=2000) 59 | 60 | image, label = read_and_decode(filename_queue) 61 | 62 | images, labels = tf.train.shuffle_batch( 63 | [image, label], batch_size=batch_size, num_threads=1, 64 | capacity=4, 65 | min_after_dequeue=2) 66 | 67 | return images, labels 68 | 69 | 70 | def inference(input_data): 71 | ''' 72 | 定义网络结构、向前计算过程 73 | ''' 74 | with tf.name_scope('conv1'): 75 | w_conv1 = weight_init([10,10,3,16], 'conv1_w') 76 | b_conv1 = bias_init([16], 'conv1_b') 77 | 78 | #卷积之后图片大小变成100-10+1 = 91 79 | h_conv1 = tf.nn.relu(conv2d(input_data, w_conv1) + b_conv1) 80 | #池化之后图片大小变成45 81 | h_pool1 = max_pool(h_conv1, 2) #140 82 | 83 | with tf.name_scope('conv2'): 84 | w_conv2 = weight_init([5,5,16,16], 'conv2_w') 85 | b_conv2 = bias_init([16], 'conv2_b') 86 | 87 | #卷积之后图片大小变成 45 -5+1 = 41 88 | h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2) 89 | #池化之后图片大小变成20 90 | h_pool2 = max_pool(h_conv2, 2) #68 91 | 92 | with tf.name_scope('conv3'): 93 | w_conv3 = weight_init([5,5,16,16], 'conv3_w') 94 | b_conv3 = bias_init([16], 'conv3_b') 95 | 96 | #卷积之后图片大小变成 20 -5+1 = 16 97 | h_conv3 = tf.nn.relu(conv2d(h_pool2, w_conv3) + b_conv3) 98 | #池化之后图片大小变成8 99 | h_pool3 = max_pool(h_conv3, 2) # 32 100 | 101 | with tf.name_scope('fc1'): 102 | w_fc1 = weight_init([8*8*16, 128], 'fc1_w') 103 | b_fc1 = bias_init([128], 'fc1_b') 104 | 105 | h_fc = tf.nn.relu(tf.matmul(tf.reshape(h_pool3,[-1,8*8*16]), w_fc1) + b_fc1) 106 | 107 | #keep_prob = 0.8 108 | #h_fc_drop = tf.nn.dropout(h_fc,keep_prob) 109 | with tf.name_scope('fc2'): 110 | w_fc2 = weight_init([128, 2], 'fc2_w') 111 | b_fc2 = bias_init([2], 'fc2_b') 112 | 113 | h_fc2 = tf.matmul(h_fc, w_fc2) + b_fc2 114 | 115 | return h_fc2 116 | 117 | 118 | def train(): 119 | ''' 120 | 训练过程 121 | ''' 122 | global_step = tf.get_variable('global_step', [], 123 | initializer=tf.constant_initializer(0), 124 | trainable=False, dtype=tf.int32) 125 | 126 | batch_size = FLAGS.batch_size 127 | train_images, train_labels = inputs("./tfrecord_data/train.tfrecord", batch_size ) 128 | test_images, test_labels = inputs("./tfrecord_data/train.tfrecord", batch_size ) 129 | 130 | 131 | train_labels_one_hot = tf.one_hot(train_labels, 2, on_value=1.0, off_value=0.0) 132 | test_labels_one_hot = tf.one_hot(test_labels, 2, on_value=1.0, off_value=0.0) 133 | 134 | #因为任务比较简单,故意把学习率调小了,以拉长训练过程。 135 | learning_rate = 0.000001 136 | 137 | 138 | with tf.variable_scope("inference") as scope: 139 | train_y_conv = inference(train_images) 140 | scope.reuse_variables() 141 | test_y_conv = inference(test_images) 142 | 143 | cross_entropy = tf.reduce_mean( 144 | tf.nn.softmax_cross_entropy_with_logits(labels=train_labels_one_hot, logits=train_y_conv)) 145 | optimizer = tf.train.GradientDescentOptimizer(learning_rate) 146 | train_op = optimizer.minimize(cross_entropy, global_step=global_step) 147 | 148 | train_correct_prediction = tf.equal(tf.argmax(train_y_conv, 1), tf.argmax(train_labels_one_hot, 1)) 149 | train_accuracy = tf.reduce_mean(tf.cast(train_correct_prediction, tf.float32)) 150 | 151 | test_correct_prediction = tf.equal(tf.argmax(test_y_conv, 1), tf.argmax(test_labels_one_hot, 1)) 152 | test_accuracy = tf.reduce_mean(tf.cast(test_correct_prediction, tf.float32)) 153 | 154 | 155 | init_op = tf.global_variables_initializer() 156 | local_init_op = tf.local_variables_initializer() 157 | 158 | saver = tf.train.Saver() 159 | tf.summary.scalar('cross_entropy_loss', cross_entropy) 160 | tf.summary.scalar('train_acc', train_accuracy) 161 | summary_op = tf.summary.merge_all() 162 | 163 | gpu_options = tf.GPUOptions( 164 | per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) 165 | config = tf.ConfigProto(gpu_options=gpu_options) 166 | 167 | with tf.Session(config=config) as sess: 168 | if FLAGS.reload_model == 1: 169 | ckpt = tf.train.get_checkpoint_state(FLAGS.model_dir) 170 | saver.restore(sess, ckpt.model_checkpoint_path) 171 | save_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]) 172 | print("reload model from %s, save_step = %d" % (ckpt.model_checkpoint_path, save_step)) 173 | else: 174 | print("Create model with fresh paramters.") 175 | sess.run(init_op) 176 | sess.run(local_init_op) 177 | 178 | summary_writer = tf.summary.FileWriter(FLAGS.event_dir, sess.graph) 179 | 180 | coord = tf.train.Coordinator() 181 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 182 | try: 183 | while not coord.should_stop(): 184 | _, g_step = sess.run([train_op, global_step]) 185 | if g_step % 2 == 0: 186 | summary_str = sess.run(summary_op) 187 | summary_writer.add_summary(summary_str, g_step) 188 | 189 | if g_step % 100 == 0: 190 | train_accuracy_value, loss = sess.run([train_accuracy, cross_entropy]) 191 | print("step %d training_acc is %.2f, loss is %.4f" % (g_step, train_accuracy_value, loss)) 192 | if g_step % 1000 == 0: 193 | test_accuracy_value = sess.run(test_accuracy) 194 | print("step %d test_acc is %.2f" % (g_step, test_accuracy_value)) 195 | 196 | if g_step % 2000 == 0: 197 | #保存一次模型 198 | print("save model to %s" % FLAGS.model_dir + "model.ckpt." + str(g_step) ) 199 | saver.save(sess, FLAGS.model_dir + "model.ckpt", global_step=global_step) 200 | 201 | except tf.errors.OutOfRangeError: 202 | pass 203 | finally: 204 | coord.request_stop() 205 | 206 | coord.join(threads) 207 | 208 | 209 | if __name__ == "__main__": 210 | if not os.path.exists("./tfrecord_data/train.tfrecord") or \ 211 | not os.path.exists("./tfrecord_data/test.tfrecord"): 212 | gen_tfrecord_data("./data/train", "./data/test/") 213 | 214 | os.environ["CUDA_VISIBLE_DEVICES"] = "0" 215 | #test() 216 | if not os.path.exists(FLAGS.model_dir): 217 | os.makedirs(FLAGS.model_dir) 218 | train() 219 | -------------------------------------------------------------------------------- /Chapter5/5.5/tfrecord_data/test.tfrecord: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/tfrecord_data/test.tfrecord -------------------------------------------------------------------------------- /Chapter5/5.5/tfrecord_data/train.tfrecord: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter5/5.5/tfrecord_data/train.tfrecord -------------------------------------------------------------------------------- /Chapter6/asr_lstm_ctc/asr_lstm_ctc.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import time 3 | 4 | import tensorflow as tf 5 | import scipy.io.wavfile as wav 6 | import numpy as np 7 | 8 | from six.moves import xrange as range 9 | 10 | try: 11 | from python_speech_features import mfcc 12 | except ImportError: 13 | print("Failed to import python_speech_features.\n Try pip install python_speech_features.") 14 | raise ImportError 15 | 16 | 17 | 18 | # 常量 19 | SPACE_TOKEN = '' 20 | SPACE_INDEX = 0 21 | FIRST_INDEX = ord('a') - 1 # 0 is reserved to space 22 | 23 | # mfcc默认提取出来的一帧13个特征 24 | num_features = 13 25 | # 26个英文字母 + 1个空白 + 1个no label = 28 label个数 26 | num_classes = ord('z') - ord('a') + 1 + 1 + 1 27 | 28 | # 迭代次数 29 | num_epochs = 200 30 | # lstm隐藏单元数 31 | num_hidden = 40 32 | # 2层lstm网络 33 | num_layers = 1 34 | # batch_size设置为1 35 | batch_size = 1 36 | # 初始学习率 37 | initial_learning_rate = 0.01 38 | 39 | # 样本个数 40 | num_examples = 1 41 | # 一个epoch有多少个batch 42 | num_batches_per_epoch = int(num_examples/batch_size) 43 | 44 | 45 | def sparse_tuple_from(sequences, dtype=np.int32): 46 | """得到一个list的稀疏表示,为了直接将数据赋值给tensorflow的tf.sparse_placeholder稀疏矩阵 47 | Args: 48 | sequences: 序列的列表 49 | Returns: 50 | 一个三元组,和tensorflow的tf.sparse_placeholder同结构 51 | """ 52 | indices = [] 53 | values = [] 54 | 55 | for n, seq in enumerate(sequences): 56 | indices.extend(zip([n]*len(seq), range(len(seq)))) 57 | values.extend(seq) 58 | 59 | indices = np.asarray(indices, dtype=np.int64) 60 | values = np.asarray(values, dtype=dtype) 61 | shape = np.asarray([len(sequences), np.asarray(indices).max(0)[1]+1], dtype=np.int64) 62 | 63 | return indices, values, shape 64 | 65 | 66 | def get_audio_feature(): 67 | ''' 68 | 获取wav文件提取mfcc特征之后的数据 69 | ''' 70 | 71 | audio_filename = "audio.wav" 72 | 73 | #读取wav文件内容,fs为采样率, audio为数据 74 | fs, audio = wav.read(audio_filename) 75 | 76 | #提取mfcc特征 77 | inputs = mfcc(audio, samplerate=fs) 78 | # 对特征数据进行归一化,减去均值除以方差 79 | feature_inputs = np.asarray(inputs[np.newaxis, :]) 80 | feature_inputs = (feature_inputs - np.mean(feature_inputs))/np.std(feature_inputs) 81 | 82 | #特征数据的序列长度 83 | feature_seq_len = [feature_inputs.shape[1]] 84 | 85 | return feature_inputs, feature_seq_len 86 | 87 | def get_audio_label(): 88 | ''' 89 | 将label文本转换成整数序列,然后再换成稀疏三元组 90 | ''' 91 | target_filename = 'label.txt' 92 | 93 | with open(target_filename, 'r') as f: 94 | #原始文本为“she had your dark suit in greasy wash water all year” 95 | line = f.readlines()[0].strip() 96 | targets = line.replace(' ', ' ') 97 | # 放入list中,空格用''代替 98 | #['she', '', 'had', '', 'your', '', 'dark', '', 'suit', '', 'in', '', 'greasy', '', 'wash', '', 'water', '', 'all', '', 'year'] 99 | targets = targets.split(' ') 100 | 101 | # 每个字母作为一个label,转换成如下: 102 | #['s' 'h' 'e' '' 'h' 'a' 'd' '' 'y' 'o' 'u' 'r' '' 'd' 103 | # 'a' 'r' 'k' '' 's' 'u' 'i' 't' '' 'i' 'n' '' 'g' 'r' 104 | # 'e' 'a' 's' 'y' '' 'w' 'a' 's' 'h' '' 'w' 'a' 't' 'e' 'r' 105 | #'' 'a' 'l' 'l' '' 'y' 'e' 'a' 'r'] 106 | targets = np.hstack([SPACE_TOKEN if x == '' else list(x) for x in targets]) 107 | 108 | # 将label转换成整数序列表示: 109 | # [19 8 5 0 8 1 4 0 25 15 21 18 0 4 1 18 11 0 19 21 9 20 0 9 14 110 | # 0 7 18 5 1 19 25 0 23 1 19 8 0 23 1 20 5 18 0 1 12 12 0 25 5 111 | # 1 18] 112 | targets = np.asarray([SPACE_INDEX if x == SPACE_TOKEN else ord(x) - FIRST_INDEX 113 | for x in targets]) 114 | 115 | # 将列表转换成稀疏三元组 116 | train_targets = sparse_tuple_from([targets]) 117 | return train_targets 118 | 119 | 120 | 121 | def inference(inputs, seq_len): 122 | ''' 123 | 2层双向LSTM的网络结构定义 124 | 125 | Args: 126 | inputs: 输入数据,形状是[batch_size, 序列最大长度,一帧特征的个数13] 127 | 序列最大长度是指,一个样本在转成特征矩阵之后保存在一个矩阵中, 128 | 在n个样本组成的batch中,因为不同的样本的序列长度不一样,在组成的3维数据中, 129 | 第2维的长度要足够容纳下所有的样本的特征序列长度。 130 | seq_len: batch里每个样本的有效的序列长度 131 | ''' 132 | 133 | #定义一个向前计算的LSTM单元,40个隐藏单元 134 | cell_fw = tf.contrib.rnn.LSTMCell(num_hidden, 135 | initializer=tf.random_normal_initializer( 136 | mean=0.0, stddev=0.1), 137 | state_is_tuple=True) 138 | 139 | # 组成一个有2个cell的list 140 | cells_fw = [cell_fw] * num_layers 141 | # 定义一个向后计算的LSTM单元,40个隐藏单元 142 | cell_bw = tf.contrib.rnn.LSTMCell(num_hidden, 143 | initializer=tf.random_normal_initializer( 144 | mean=0.0, stddev=0.1), 145 | state_is_tuple=True) 146 | # 组成一个有2个cell的list 147 | cells_bw = [cell_bw] * num_layers 148 | 149 | # 将前面定义向前计算和向后计算的2个cell的list组成双向lstm网络 150 | # sequence_length为实际有效的长度,大小为batch_size, 151 | # 相当于表示batch中每个样本的实际有用的序列长度有多长。 152 | # 输出的outputs宽度是隐藏单元的个数,即num_hidden的大小 153 | outputs, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn(cells_fw, 154 | cells_bw, 155 | inputs, 156 | dtype=tf.float32, 157 | sequence_length=seq_len) 158 | 159 | #获得输入数据的形状 160 | shape = tf.shape(inputs) 161 | batch_s, max_timesteps = shape[0], shape[1] 162 | 163 | # 将2层LSTM的输出转换成宽度为40的矩阵 164 | # 后面进行全连接计算 165 | outputs = tf.reshape(outputs, [-1, num_hidden]) 166 | 167 | W = tf.Variable(tf.truncated_normal([num_hidden, 168 | num_classes], 169 | stddev=0.1)) 170 | 171 | b = tf.Variable(tf.constant(0., shape=[num_classes])) 172 | 173 | # 进行全连接线性计算 174 | logits = tf.matmul(outputs, W) + b 175 | 176 | # 将全连接计算的结果,由宽度40变成宽度80, 177 | # 即最后的输入给CTC的数据宽度必须是26+2的宽度 178 | logits = tf.reshape(logits, [batch_s, -1, num_classes]) 179 | 180 | # 转置,将第一维和第二维交换。 181 | # 变成序列的长度放第一维,batch_size放第二维。 182 | # 也是为了适应Tensorflow的CTC的输入格式 183 | logits = tf.transpose(logits, (1, 0, 2)) 184 | 185 | return logits 186 | 187 | def main(): 188 | # 输入特征数据,形状为:[batch_size, 序列长度,一帧特征数] 189 | inputs = tf.placeholder(tf.float32, [None, None, num_features]) 190 | 191 | # 输入数据的label,定义成稀疏sparse_placeholder会生成稀疏的tensor:SparseTensor 192 | # 这个结构可以直接输入给ctc求loss 193 | targets = tf.sparse_placeholder(tf.int32) 194 | 195 | # 序列的长度,大小是[batch_size]大小 196 | # 表示的是batch中每个样本的有效序列长度是多少 197 | seq_len = tf.placeholder(tf.int32, [None]) 198 | 199 | # 向前计算网络,定义网络结构,输入是特征数据,输出提供给ctc计算损失值。 200 | logits = inference(inputs, seq_len) 201 | 202 | # ctc计算损失 203 | # 参数targets必须是一个值为int32的稀疏tensor的结构:tf.SparseTensor 204 | # 参数logits是前面lstm网络的输出 205 | # 参数seq_len是这个batch的样本中,每个样本的序列长度。 206 | loss = tf.nn.ctc_loss(targets, logits, seq_len) 207 | 208 | # 计算损失的平均值 209 | cost = tf.reduce_mean(loss) 210 | 211 | # 采用冲量优化方法 212 | optimizer = tf.train.MomentumOptimizer(initial_learning_rate, 0.9).minimize(cost) 213 | 214 | # 还有另外一个ctc的函数:tf.contrib.ctc.ctc_beam_search_decoder 215 | # 本函数会得到更好的结果,但是效果比ctc_beam_search_decoder低 216 | # 返回的结果中,decode是ctc解码的结果,即输入的数据解码出结果序列是什么 217 | decoded, _ = tf.nn.ctc_greedy_decoder(logits, seq_len) 218 | 219 | # 采用计算编辑距离的方式计算,计算decode后结果的错误率。 220 | ler = tf.reduce_mean(tf.edit_distance(tf.cast(decoded[0], tf.int32), 221 | targets)) 222 | config = tf.ConfigProto() 223 | config.gpu_options.allow_growth = True 224 | 225 | with tf.Session(config=config) as session: 226 | # 初始化变量 227 | tf.global_variables_initializer().run() 228 | 229 | for curr_epoch in range(num_epochs): 230 | train_cost = train_ler = 0 231 | start = time.time() 232 | 233 | for batch in range(num_batches_per_epoch): 234 | #获取训练数据,本例中只去一个样本的训练数据 235 | train_inputs, train_seq_len = get_audio_feature() 236 | # 获取这个样本的label 237 | train_targets = get_audio_label() 238 | feed = {inputs: train_inputs, 239 | targets: train_targets, 240 | seq_len: train_seq_len} 241 | 242 | # 一次训练,更新参数 243 | batch_cost, _ = session.run([cost, optimizer], feed) 244 | # 计算累加的训练的损失值 245 | train_cost += batch_cost * batch_size 246 | # 计算训练集的错误率 247 | train_ler += session.run(ler, feed_dict=feed)*batch_size 248 | 249 | train_cost /= num_examples 250 | train_ler /= num_examples 251 | 252 | # 打印每一轮迭代的损失值,错误率 253 | log = "Epoch {}/{}, train_cost = {:.3f}, train_ler = {:.3f}, time = {:.3f}" 254 | print(log.format(curr_epoch+1, num_epochs, train_cost, train_ler, 255 | time.time() - start)) 256 | # 在进行了1200次训练之后,计算一次实际的测试,并且输出 257 | # 读取测试数据,这里读取的和训练数据的同一个样本 258 | test_inputs, test_seq_len = get_audio_feature() 259 | test_targets = get_audio_label() 260 | test_feed = {inputs: test_inputs, 261 | targets: test_targets, 262 | seq_len: test_seq_len} 263 | d = session.run(decoded[0], feed_dict=test_feed) 264 | # 将得到的测试语音经过ctc解码后的整数序列转换成字母 265 | str_decoded = ''.join([chr(x) for x in np.asarray(d[1]) + FIRST_INDEX]) 266 | # 将no label转换成空 267 | str_decoded = str_decoded.replace(chr(ord('z') + 1), '') 268 | # 将空白转换成空格 269 | str_decoded = str_decoded.replace(chr(ord('a') - 1), ' ') 270 | # 打印最后的结果 271 | print('Decoded:\n%s' % str_decoded) 272 | 273 | if __name__ == "__main__": 274 | main() 275 | -------------------------------------------------------------------------------- /Chapter6/asr_lstm_ctc/audio.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thewintersun/tensorflowbook/ac39e3d59c94dc7c4a8b2b7e5703a5dbce601ed6/Chapter6/asr_lstm_ctc/audio.wav -------------------------------------------------------------------------------- /Chapter6/asr_lstm_ctc/label.txt: -------------------------------------------------------------------------------- 1 | she had your dark suit in greasy wash water all year 2 | -------------------------------------------------------------------------------- /Chapter6/wordseg/wordlstmseg.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | import os 8 | import sys 9 | import time 10 | 11 | import codecs 12 | import re 13 | import numpy as np 14 | import tensorflow as tf 15 | 16 | import random 17 | 18 | flags = tf.app.flags 19 | flags.DEFINE_integer("batch_size", 10, "Numbers of training examples each step processes ") 20 | flags.DEFINE_integer("embedding_size", 200, " ") 21 | 22 | flags.DEFINE_string("word2vec_path", "./embeding.txt", " ") 23 | flags.DEFINE_string("train_file", "./msr_training.utf8", " ") 24 | flags.DEFINE_string("test_file", "./msr_test_gold.utf8", " ") 25 | flags.DEFINE_string("model_save_path", "./save/", " ") 26 | 27 | flags.DEFINE_float("dropout", 0.75, " ") 28 | flags.DEFINE_float("gpu_memory_fraction", 0.1, " ") 29 | FLAGS = flags.FLAGS 30 | 31 | 32 | INITIAL_LEARNING_RATE = 0.003 33 | 34 | 35 | class ChineseSegLSTM(object): 36 | ''' 37 | 参考复旦的论文: 38 | Long Short-Term Memory Neural Networks for chinese word segmentation的中文分词 39 | ''' 40 | def __init__(self): 41 | os.environ["CUDA_VISIBLE_DEVICES"] = "0" 42 | self._word2vec_dict={} 43 | self._word2vec_num=0 44 | self._word2vec_size=FLAGS.embedding_size 45 | 46 | self._train_list = [] 47 | self._label_list = [] 48 | 49 | self._test_list = [] 50 | self._test_label_list = [] 51 | self._test_index_in_epoch = 0 #取test batch的时候记录位置 52 | self._train_index_in_epoch = 0 #取test train batch的时候记录位置 53 | 54 | self._batch_size = FLAGS.batch_size 55 | self._embedding_size = FLAGS.embedding_size 56 | self._window_size = 5 57 | 58 | self._init_word2vec_dict(FLAGS.word2vec_path) 59 | self._max_feature_len = 600 60 | 61 | def run_training(self): 62 | ''' 63 | 训练过程 64 | ''' 65 | self._read_train_file() 66 | self._read_test_file() 67 | print("read file complete") 68 | 69 | global_step = tf.get_variable('global_step', [],initializer=tf.constant_initializer(0), trainable=False) 70 | opt = tf.train.GradientDescentOptimizer(INITIAL_LEARNING_RATE) 71 | input_data = tf.placeholder(tf.float32, [self._batch_size, self._max_feature_len, self._window_size * self._embedding_size]) 72 | labels = tf.placeholder(tf.int32, [self._batch_size, self._max_feature_len]) 73 | seq_len = tf.placeholder(tf.int32, [self._batch_size]) 74 | logits = self.inference( input_data, seq_len) 75 | cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=labels, logits=logits, name="cross_entropy") 76 | cross_entropy_mean = tf.reduce_mean(cross_entropy, name='xentropy_mean') 77 | 78 | grads = opt.compute_gradients(cross_entropy_mean) 79 | apply_gradient_op = opt.apply_gradients(grads, global_step=global_step) 80 | 81 | train_op = tf.group(apply_gradient_op ) 82 | 83 | total_correct = 0 84 | for i in range(self._batch_size): 85 | per_logits_reshape = logits[i][:seq_len[i]] 86 | per_labels_reshape = labels[i][:seq_len[i]] 87 | correct = tf.nn.in_top_k(per_logits_reshape, per_labels_reshape, 1) 88 | eval_correct = tf.reduce_sum(tf.cast(correct, tf.int32)) 89 | total_correct += eval_correct 90 | self.saver = tf.train.Saver() 91 | 92 | init = tf.initialize_all_variables() 93 | gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) 94 | with tf.Session(config=tf.ConfigProto( 95 | allow_soft_placement=True, 96 | log_device_placement=False, 97 | gpu_options=gpu_options)) as sess: 98 | 99 | sess.run(init) 100 | print("init variable complete") 101 | 102 | for i in range(10000000): 103 | train_batch, label_batch, seq_len_list = self.next_train_batch(self._batch_size ) 104 | 105 | 106 | 107 | start_time = time.time() 108 | feed_dict ={input_data:train_batch, labels:label_batch, seq_len: seq_len_list} 109 | _, loss_value = sess.run([train_op,cross_entropy_mean],feed_dict=feed_dict) 110 | duration = time.time() - start_time 111 | if i % 50 == 0 and i > 0: 112 | print("step: %d loss %f" % (i, loss_value)) 113 | if i % 100 == 0 and i > 0: 114 | test_batch, test_label_batch, test_seq_len_list = self.next_test_batch(self._batch_size ) 115 | feed_dict ={input_data:test_batch, labels:test_label_batch, seq_len: test_seq_len_list} 116 | _, loss_value,true_count = sess.run([train_op,cross_entropy_mean, total_correct],feed_dict=feed_dict) 117 | total_count = 0 118 | for k in range(len(test_seq_len_list)): 119 | total_count += test_seq_len_list[k] 120 | print("test: %d loss %f correct_ratio %f" % (i, loss_value, true_count/total_count)) 121 | if i % 1000 == 0 and i > 0: 122 | self.saver.save(sess, os.path.join(FLAGS.model_save_path, "model.ckpt"), global_step=i) 123 | print("save model at %s, step: %d" %( os.path.join(FLAGS.model_save_path, "model.ckpt"), i)) 124 | 125 | 126 | def inference(self, input_data, seq_len): 127 | ''' 向前计算过程描述 128 | Args: 129 | layer_number:表示训练的层数, 用来在进行逐层训练的时候指定 130 | ''' 131 | num_hidden = 120 132 | num_layers = 2 133 | 134 | w3 = tf.get_variable("w3", [num_hidden*2, 256], 135 | initializer=tf.random_normal_initializer(stddev=0.1)) 136 | b3 = tf.get_variable("b3", [256], 137 | initializer=tf.constant_initializer(0.0)) 138 | w2 = tf.get_variable("w2", [256, 4], 139 | initializer=tf.random_normal_initializer(stddev=0.1)) 140 | b2 = tf.get_variable("b2", [4], 141 | initializer=tf.constant_initializer(0.0)) 142 | 143 | fw_cell = tf.contrib.rnn.LSTMCell( 144 | num_hidden, initializer=tf.random_normal_initializer(stddev=0.1)) 145 | bw_cell = tf.contrib.rnn.LSTMCell( 146 | num_hidden, initializer=tf.random_normal_initializer(stddev=0.1)) 147 | 148 | fw_cells = [fw_cell] * num_layers 149 | bw_cells = [bw_cell] * num_layers 150 | 151 | outputs, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn( 152 | fw_cells, bw_cells, input_data, sequence_length=seq_len, dtype=tf.float32 153 | ) 154 | outputs = tf.reshape(outputs, [-1, num_hidden * 2]) 155 | 156 | fc3 = tf.nn.relu(tf.nn.xw_plus_b(outputs, w3, b3)) 157 | #再做一个线性变化 158 | logits = tf.nn.xw_plus_b(fc3,w2 , b2) 159 | logits = tf.reshape(logits, [self._batch_size, -1, 4]) 160 | return logits 161 | 162 | 163 | def loss(self,logits,placeholder_id ): 164 | 165 | labels = tf.cast(self._label_data_holdplace[placeholder_id], tf.int64) 166 | cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels, name="cross_entropy") 167 | cross_entropy_mean = tf.reduce_mean(cross_entropy, name='xentropy_mean') 168 | tf.add_to_collection('losses', cross_entropy_mean) 169 | 170 | return tf.add_n(tf.get_collection('losses'), name='total_loss') 171 | 172 | 173 | 174 | def _reset_test_index(self): 175 | '''将测试数据集合设置从头开始读取''' 176 | self._test_index_in_epoch = 0 177 | 178 | 179 | def _reset_test_train_index(self): 180 | '''将训练数据集合设置从头开始读取''' 181 | self._train_index_in_epoch = 0 182 | 183 | 184 | def next_train_batch(self, batch_size): 185 | '''读取测试训练数据本身的数据''' 186 | start = self._train_index_in_epoch 187 | 188 | context_size = self._window_size // 2 189 | data_set = [] 190 | label_set = [] 191 | seq_len_list = [] 192 | 193 | getsize = 0 194 | while getsize < batch_size: 195 | seq_len = 0 196 | if start >= len(self._train_list): 197 | start = 0 198 | 199 | char_list = self._train_list[start] 200 | 201 | char_vector_list = [] 202 | char_label_list = [] 203 | 204 | for i in range(len(char_list)): 205 | char = char_list[i] 206 | if char == "PAD": 207 | continue 208 | 209 | word_context=[] 210 | for k in range(-context_size, -context_size + self._window_size): 211 | char = char_list[i+k] 212 | word_context.extend(self._get_vector(char)) 213 | 214 | 215 | char_vector_list.append(word_context) 216 | 217 | char_label_list.append(self._gen_label_value(self._label_list[start][i])) 218 | seq_len += 1 219 | 220 | 221 | #补0 222 | pad_vector = [] 223 | for k in range(self._window_size): 224 | pad_vector.extend(self._get_vector("PAD")) 225 | 226 | 227 | pad_number = self._max_feature_len - seq_len 228 | 229 | char_vector_list = char_vector_list + [pad_vector] * pad_number 230 | char_label_list = char_label_list + [0] * pad_number 231 | 232 | if seq_len > self._max_feature_len: 233 | start += 1 234 | continue 235 | 236 | data_set.append(char_vector_list) 237 | label_set.append(char_label_list) 238 | seq_len_list.append(seq_len) 239 | start += 1 240 | getsize += 1 241 | 242 | self._train_index_in_epoch = start 243 | return data_set, label_set, seq_len_list 244 | 245 | 246 | def next_test_batch(self, batch_size): 247 | ''' 读取训练数据一个batch ,得到字向量 ''' 248 | start = self._test_index_in_epoch 249 | 250 | context_size = int(self._window_size / 2) 251 | data_set = [] 252 | label_set = [] 253 | seq_len_list = [] 254 | 255 | getsize = 0 256 | while getsize < batch_size: 257 | seq_len = 0 258 | if start >= len(self._test_list): 259 | start = 0 260 | 261 | char_list = self._test_list[start] 262 | 263 | char_vector_list = [] 264 | char_label_list = [] 265 | 266 | for i in range(len(char_list)): 267 | char = char_list[i] 268 | if char == "PAD": 269 | continue 270 | 271 | word_context=[] 272 | for k in range(-context_size, -context_size + self._window_size): 273 | char = char_list[i + k] 274 | word_context.extend(self._get_vector(char)) 275 | 276 | 277 | char_vector_list.append(word_context) 278 | 279 | char_label_list.append(self._gen_label_value(self._test_label_list[start][i])) 280 | 281 | seq_len += 1 282 | 283 | 284 | #补0 285 | pad_vector = [] 286 | for k in range(self._window_size): 287 | pad_vector.extend(self._get_vector("0")) 288 | 289 | pad_number = self._max_feature_len - seq_len 290 | 291 | char_vector_list = char_vector_list + [pad_vector] * pad_number 292 | char_label_list = char_label_list + [0] * pad_number 293 | 294 | if seq_len > self._max_feature_len: 295 | start += 1 296 | continue 297 | 298 | data_set.append(char_vector_list) 299 | label_set.append(char_label_list) 300 | seq_len_list.append(seq_len) 301 | start += 1 302 | getsize += 1 303 | 304 | self._test_index_in_epoch = start 305 | return data_set, label_set, seq_len_list 306 | 307 | 308 | 309 | def _read_test_file(self): 310 | ''' 读取一个测试文件, 读取测试数据''' 311 | converted_file = "./msr_test_gold.convert" 312 | #self._character_tagging(FLAGS.test_file,converted_file) 313 | self._test_list, self._test_label_list = self._load_input_and_label(converted_file) 314 | 315 | 316 | def _gen_label_value(self,k): 317 | s = [1,0,0,0] 318 | b = [0,1,0,0] 319 | m = [0,0,1,0] 320 | e = [0,0,0,1] 321 | 322 | if k=='S': 323 | return 0 324 | if k=='B': 325 | return 1 326 | if k=='M': 327 | return 2 328 | if k=='E': 329 | return 3 330 | return 0 331 | 332 | 333 | def _get_zero_list(self,embedding_size): 334 | value=[] 335 | for i in range(embedding_size): 336 | value.append(0) 337 | return value 338 | 339 | 340 | def _read_train_file(self): 341 | ''' 342 | 读取训练文件, 343 | 首先根据训练文件中分词的情况, 344 | 将训练文件中每个字做一个S/B/M/E的标记 345 | 然后将数据的正确输入和label保存到内存中 346 | ''' 347 | converted_file = "./msr_training.convert" 348 | #self._character_tagging(FLAGS.train_file, converted_file) 349 | self._train_list, self._label_list = self._load_input_and_label(converted_file) 350 | 351 | 352 | def _load_input_and_label(self, file_path): 353 | ''' 354 | 将数据读入内存。 355 | 每一句话的前面用三个PAD填充 356 | Args: 357 | file_path: 输入的经过转换的文件 358 | ''' 359 | all_data_list = [] 360 | all_label_list = [] 361 | 362 | with codecs.open(file_path, 'r', 'utf-8') as input_data: 363 | 364 | for line in input_data: 365 | char_list = [] 366 | label_list = [] 367 | 368 | word_list = line.strip().split() 369 | 370 | if len(word_list) ==0: 371 | continue 372 | 373 | char_list.append("PAD") 374 | char_list.append("PAD") 375 | char_list.append("PAD") 376 | label_list.append("PAD") 377 | label_list.append("PAD") 378 | label_list.append("PAD") 379 | 380 | for word in word_list: 381 | word_array = word.split('/') 382 | char_list.append(word_array[0]) 383 | label_list.append(word_array[1]) 384 | 385 | char_list.append("PAD") 386 | char_list.append("PAD") 387 | char_list.append("PAD") 388 | label_list.append("PAD") 389 | label_list.append("PAD") 390 | label_list.append("PAD") 391 | 392 | all_data_list.append(char_list) 393 | all_label_list.append(label_list) 394 | 395 | return all_data_list, all_label_list 396 | 397 | 398 | def _init_word2vec_dict(self, word2vec_path): 399 | ''' 400 | 将word2vec训练好的字向量加载到内存 401 | ''' 402 | if not os.path.exists(word2vec_path): 403 | print("file %s not exist" % word2vec_path) 404 | return 405 | with codecs.open(word2vec_path, "r","utf-8") as f: 406 | #第一行是大小值,先略过 407 | isfirstline = True 408 | for line in f: 409 | list = line.strip().split(' ') 410 | if isfirstline == True: 411 | isfirstline=False 412 | self._word2vec_num=int(list[0]) 413 | self._word2vec_size=int(list[1]) 414 | continue 415 | 416 | key=list[0] 417 | value=list[1:] 418 | value=self.stringlist2floatlist(value) 419 | self._word2vec_dict[key]=value 420 | 421 | 422 | def _get_vector(self,key): 423 | ''' 424 | 通过一个字获取这个字的字向量, 425 | 如果没有的话, 就随机生成一个, 并且将随机生成的保存 426 | ''' 427 | 428 | if key=="PAD": 429 | return self._get_zero_list(self._word2vec_size) 430 | 431 | if key not in self._word2vec_dict: 432 | #如果没有这个key,就生成一个随机的, 放到字典里 433 | value=[] 434 | for i in range(self._word2vec_size): 435 | value.append(random.uniform(-1,1)) 436 | self._word2vec_dict[key]=value 437 | self._word2vec_num+=1 438 | return value 439 | return self._word2vec_dict[key] 440 | 441 | 442 | 443 | def _character_tagging(self,input_file, output_file): 444 | ''' 445 | 对训练数据进行预处理, 446 | 将每个字根据分词的情况,标记S,B,M,E的标记 447 | S表示单独一个字; 448 | B代表一个词的开始 449 | M代表一个词的中间的字 450 | E代表一个词的最后一个字 451 | 452 | Agrs: 453 | input_file: 输入文本文件 454 | output_file: 输出做完标记后的文本文件 455 | ''' 456 | print("_character_tagging: ", input_file) 457 | with codecs.open(input_file, 'r', 'utf-8') as input_data, \ 458 | codecs.open(output_file, 'w', 'utf-8') as output_data: 459 | for line in input_data.readlines(): 460 | word_list = line.strip().split() 461 | 462 | for word in word_list: 463 | word = self._replace_number_alphabet(word) 464 | if len(word) == 1: 465 | output_data.write(word + "/S ") 466 | else: 467 | if len(word) == 3 and self._is_special_word(word)>0: 468 | output_data.write(word + "/S ") 469 | continue 470 | index =0 471 | if len(word)>3 and self._is_special_word(word[:3])>0: 472 | output_data.write(word[:3] + "/B ") 473 | index = 3 474 | else: 475 | output_data.write(word[0] + "/B ") 476 | index =1 477 | 478 | while index < (len(word)-3): 479 | if self._is_special_word(word[index:index+3])>0: 480 | output_data.write(word[index:index+3] + "/M ") 481 | index += 3 482 | else: 483 | output_data.write(word[index] + "/M ") 484 | index +=1 485 | if index==(len(word)-3) and self._is_special_word(word[index:index+3])>0: 486 | output_data.write(word[index:index+3] + "/E ") 487 | else: 488 | while index < len(word)-1: 489 | output_data.write(word[index] + "/M ") 490 | index +=1 491 | output_data.write(word[index] + "/E ") 492 | output_data.write("\n") 493 | 494 | def _replace_number_alphabet(self,word): 495 | ''' 496 | 将一个单词内的连续的数字, 497 | 或者连续的字母用特殊符号来代替 498 | ''' 499 | special_word = "$E$" 500 | special_number = "$N$" 501 | word = re.sub('([a-zA-Z]+)', special_word, word) 502 | word = re.sub('([0-9]+)', special_number, word) 503 | return word 504 | 505 | def _is_special_word(self,w1): 506 | if w1=="$E$": 507 | return 1 508 | if w1 == "$N$": 509 | return 2 510 | return 0 511 | 512 | def get_char_list(self,sentence): 513 | ''' 514 | 输入一句话,返回每个字的list, 515 | 遇到$E$, $N$这种特殊字符的时候, 516 | 作为一个字保存为list的一个元素 517 | ''' 518 | char_list = [] 519 | index=0 520 | while index0: 522 | char_list.append(sentence[index:index+3]) 523 | index+=3 524 | continue 525 | char_list.append(sentence[index]) 526 | index+=1 527 | return char_list 528 | 529 | def stringlist2floatlist(self,list): 530 | floatlist=[] 531 | for i in range(len(list)): 532 | floatlist.append(float(list[i])) 533 | return floatlist 534 | 535 | def main(_): 536 | wordseg = ChineseSegLSTM() 537 | #sess, maxindex = wordseg.reload_network() 538 | string="千呼万唤始出来,等了好久,之前仅对 Nexus 5 开放的功能,终于正式上线了。" 539 | #string=string.replace(' ','') 540 | #print(wordseg.sentence_segment(sess, maxindex,string)) 541 | wordseg.run_training() 542 | #wordseg.eval() 543 | 544 | if __name__ == "__main__": 545 | tf.app.run() 546 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 《TensorFlow入门与实战》一书,每个章节的代码实例。 2 | --------------------------------------------------------------------------------