【07】邏輯迴歸(鳶尾花)
阿新 • • 發佈:2019-01-03
# Softmax example in TF using the classical Iris dataset # Download iris.data from https://archive.ics.uci.edu/ml/datasets/Iris # Be sure to remove the last empty line of it before running the example # 1、匯入必要的包 import tensorflow as tf import os # 2、定義權重和偏置 # this time weights form a matrix, not a column vector, one "weight vector" per class. W = tf.Variable(tf.zeros([4, 3]), name="weights") # so do the biases, one per class. b = tf.Variable(tf.zeros([3]), name="bias") # 3、定義擬合關係,這裡為線性函式 def combine_inputs(X): return tf.matmul(X, W) + b # 4、定義啟用函式,是在擬合函式的結果上塞入啟用函式(獲得分類結果) def inference(X): return tf.nn.softmax(combine_inputs(X)) # 5、定義損失函式 def loss(X, Y): return tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=combine_inputs(X), labels=Y)) # 6、定義讀取CSV工具函式,tensorflow的資料輸入 # 關於讀取問題的詳細介紹 https://blog.csdn.net/zzk1995/article/details/54292859 def read_csv(batch_size, file_name, record_defaults): filename_queue = tf.train.string_input_producer([os.path.dirname(os.path.abspath(__file__)) + "/" + file_name]) reader = tf.TextLineReader() key, value = reader.read(filename_queue) # decode_csv will convert a Tensor from type string (the text line) in # a tuple of tensor columns with the specified defaults, which also # sets the data type for each column decoded = tf.decode_csv(value, record_defaults=record_defaults) # batch actually reads the file and loads "batch_size" rows in a single tensor return tf.train.shuffle_batch(decoded, batch_size=batch_size, capacity=batch_size * 50, min_after_dequeue=batch_size) # 7、讀取檔案內容 def inputs(): sepal_length, sepal_width, petal_length, petal_width, label =\ read_csv(100, "iris.data", [[0.0], [0.0], [0.0], [0.0], [""]]) # convert class names to a 0 based class index. label_number = tf.to_int32(tf.argmax(tf.to_int32(tf.stack([ tf.equal(label, ["Iris-setosa"]), tf.equal(label, ["Iris-versicolor"]), tf.equal(label, ["Iris-virginica"]) ])), 0)) # Pack all the features that we care about in a single matrix; # We then transpose to have a matrix with one example per row and one feature per column. features = tf.transpose(tf.stack([sepal_length, sepal_width, petal_length, petal_width])) return features, label_number # 8、訓練模型 def train(total_loss): learning_rate = 0.01 return tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss) # 9、評估模型 def evaluate(sess, X, Y): predicted = tf.cast(tf.arg_max(inference(X), 1), tf.int32) print(sess.run(tf.reduce_mean(tf.cast(tf.equal(predicted, Y), tf.float32)))) # 10、執行呼叫整個過程 # Launch the graph in a session, setup boilerplate with tf.Session() as sess: tf.initialize_all_variables().run() X, Y = inputs() total_loss = loss(X, Y) train_op = train(total_loss) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) # actual training loop training_steps = 1000 for step in range(training_steps): sess.run([train_op]) # for debugging and learning purposes, see how the loss gets decremented thru training steps if step % 10 == 0: print("loss: ", sess.run([total_loss])) evaluate(sess, X, Y) coord.request_stop() coord.join(threads) sess.close()