如何將TensorFlow用作計算框架
更多深度文章,請關注:https://yq.aliyun.com/cloud
Tensorflow可能是最受歡迎,增長最快的機器學習框架。在Github擁有超過70000個點讚,並得到Google的支持,不僅擁有比Linux更多的點讚,還擁有大量的資源。
如果你以前一直在關注機器學習101係列,你會注意到我們已經使用sklearn框架來實現我們的模型。然而,當我們開始勇於進入神經網絡,深度學習和一些算法的內部運作時,我們將開始使用Tensorflow框架,該框架具有訪問更多低級API的能力,為我們提供在模型上更細致的控製。
|Tensorflow
Tensorflow
pip install tensorflow
pip install tensorflow-gpu
# Figure out what devices are available
from tensorflow.python.client import device_lib
def get_devices():
return [x.name for x in device_lib.list_local_devices()]
print (get_devices())
['/cpu:0', '/gpu:0']
有關詳細信息,請參閱安裝頁麵。
Tensorflow
- tf.Variable
- tf.constant
- tf.placeholder
tf.Variable
a = tf.Variable([1,2,3], name="a")
還有幾個選項,但這隻是為了涵蓋基礎知識。與這裏討論的任何事情一樣,你可以在文檔頁麵上閱讀更多信息。
tf.constant
b = tf.constant([1,2,3], name="b")
tf.placeholder
c = tf.placeholder(tf.int32, shape=[1,2], name="myPlaceholder")
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
res = sess.run(c,
feed_dict={
c:[[5,6]]
})
print (res)
[[5 6]]
你可以在這裏找到支持Tensorflow數據類型的完整列表。
a = tf.Variable(3)
b = tf.Variable(4)
c = tf.multiply(a,b)
print (c)
Tensor("Mul:0", shape=(), dtype=int32)
print (a)
print (b)
<tf.Variable 'Variable_4:0' shape=() dtype=int32_ref>
<tf.Variable 'Variable_5:0' shape=() dtype=int32_ref>
with tf.Session() as sess:
sess.run(tf.global_variables_initializer()) # this is important
print (sess.run(c))
12
你也可以使用tf.InteractiveSession,如果你使用像IDLE或者jupyter筆記本這類的,這很有用。 此外,還可以通過聲明sess = tf.Session()來開始一個會話控製,然後使用sess.close()關閉它,但是我不建議這樣做,因為很容易忘記關閉會話會話控製, 使用此方法作為交互式會話可能對性能會有影響,因為Tensorflow真的喜歡占用盡可能多的資源(在這方麵有點像Chrome)。
Tensorflow
a = tf.Variable(3)
b=tf.Variable(4)
c = tf.multiply(a,b)
d = tf.add(a,c)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
c_value = sess.run(c)
d_value = sess.run(d)
print (c_value, d_value)
12 15
Tensorflow遞歸地計算操作的依賴關係以找到其計算值。
12 Tensor("Mul:0", shape=(), dtype=int32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
res = sess.run(c)
print (res,c)
12 Tensor("Mul:0", shape=(), dtype=int32)
with tf.device("/gpu:0"):
# do stuff with GPU
with tf.device("/cpu:0"):
# do some other stuff with CPU
Tensorflow
GPU
|
cluster = tf.train.ClusterSpec({"my_job": ["worker1.ip:2222", "worker2.ip:2222"]})
server = tf.train.Server(cluster, job_name="my_job", task_index=1)
a = tf.Variable(5)
with tf.device("/job:my_job/task:0"):
b = tf.multiply(a, 10)
with tf.device("/job:my_job/task:1"):
c = tf.add(b, a)
with tf.Session("grpc://localhost:2222") as sess:
res = sess.run(c)
print(res)
# Get task number from command line
import sys
task_number = int(sys.argv[1])
import tensorflow as tf
cluster = tf.train.ClusterSpec({"my_job": ["worker1.ip:2222", "worker2.ip:2222"]})
server = tf.train.Server(cluster, job_name="my_job", task_index=task_number)
print("Worker #{}".format(task_number))
server.start()
server.join()
python filename.py 1
要更深入地了解Tensorflow的分布式計算,請參閱文檔。
a = tf.Variable(5)
b = tf.Variable(4, name="my_variable")
# set the value of a to 3
op = tf.assign(a, 3)
# create saver object
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(op)
print ("a:", sess.run(a))
print ("my_variable:", sess.run(b))
# use saver object to save variables
# within the context of the current session
saver.save(sess, "/tmp/my_model.ckpt")
a: 3
my_variable: 4
# Only necessary if you use IDLE or a jupyter notebook
tf.reset_default_graph()
# make a dummy variable
# the value is arbitrary, here just zero
# but the shape must the the same as in the saved model
a = tf.Variable(0)
c = tf.Variable(0, name="my_variable")
saver = tf.train.Saver()
with tf.Session() as sess:
# use saver object to load variables from the saved model
saver.restore(sess, "/tmp/my_model.ckpt")
print ("a:", sess.run(a))
print ("my_variable:", sess.run(c))
INFO:tensorflow:Restoring parameters from /tmp/my_model.ckpt
a: 3
my_variable: 4
fw = tf.summary.FileWriter("/tmp/summary", sess.graph)
a = tf.Variable(5, name="a")
b = tf.Variable(10, name="b")
c = tf.multiply(a,b, name="result")
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print (sess.run(c))
fw = tf.summary.FileWriter("/tmp/summary", sess.graph)
tensorboard --logdir=/tmp/summary
with tf.name_scope('primitives') as scope:
a = tf.Variable(5, name='a')
b = tf.Variable(10, name='b')
with tf.name_scope('fancy_pants_procedure') as scope:
# this procedure has no significant interpretation
# and was purely made to illustrate why you might want
# to work at a higher level of abstraction
c = tf.multiply(a,b)
with tf.name_scope('very_mean_reduction') as scope:
d = tf.reduce_mean([a,b,c])
e = tf.add(c,d)
with tf.name_scope('not_so_fancy_procedure') as scope:
# this procedure suffers from imposter syndrome
d = tf.add(a,b)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print (sess.run(c))
print (sess.run(e))
fw = tf.summary.FileWriter("/tmp/summary", sess.graph)
import random
a = tf.Variable(5, name="a")
b = tf.Variable(10, name="b")
# set the intial value of c to be the product of a and b
# in order to write a summary of c, c must be a variable
init_value = tf.multiply(a,b, name="result")
c = tf.Variable(init_value, name="ChangingNumber")
# update the value of c by incrementing it by a placeholder number
number = tf.placeholder(tf.int32, shape=[], name="number")
c_update = tf.assign(c, tf.add(c,number))
# create a summary to track to progress of c
tf.summary.scalar("ChangingNumber", c)
# in case we want to track multiple summaries
# merge all summaries into a single operation
summary_op = tf.summary.merge_all()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# initialize our summary file writer
fw = tf.summary.FileWriter("/tmp/summary", sess.graph)
# do 'training' operation
for step in range(1000):
# set placeholder number somewhere between 0 and 100
num = int(random.random()*100)
sess.run(c_update, feed_dict={number:num})
# compute summary
summary = sess.run(summary_op)
# add merged summaries to filewriter,
# so they are saved to disk
fw.add_summary(summary, step)
tf.summary.scalar("ChangingNumber", c)
summary_op = tf.summary.merge_all()
summary = tf.summary.merge([summ1, summ2, summ3])
summary = sess.run(summary_op)
fw.add_summary(summary, step)
- 導入數據。
- 構建模型架構。
- 定義一個損失函數進行優化,並有一種優化方法。
- 實際培訓模式。
import tensorflow as tf tf.reset_default_graph() # again, this is not needed if run as a script
from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
Extracting MNIST_data/train-images-idx3-ubyte.gz Extracting MNIST_data/train-labels-idx1-ubyte.gz Extracting MNIST_data/t10k-images-idx3-ubyte.gz Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
# input with tf.name_scope('input') as scope: x = tf.placeholder(tf.float32, [None, 28*28], name="input") # a placeholder to hold the correct answer during training labels = tf.placeholder(tf.float32, [None, 10], name="label") # the probability of a neuron being kept during dropout keep_prob = tf.placeholder(tf.float32, name="keep_prob") with tf.name_scope('model') as scope: with tf.name_scope('fc1') as scope: # fc1 stands for 1st fully connected layer # 1st layer goes from 784 neurons (input) to 500 in the first hidden layer w1 = tf.Variable(tf.truncated_normal([28*28, 500], stddev=0.1), name="weights") b1 = tf.Variable(tf.constant(0.1, shape=[500]), name="biases") with tf.name_scope('softmax_activation') as scope: # softmax activation a1 = tf.nn.softmax(tf.matmul(x, w1) + b1) with tf.name_scope('dropout') as scope: # dropout drop1 = tf.nn.dropout(a1, keep_prob) with tf.name_scope('fc2') as scope: # takes the first hidden layer of 500 neurons to 100 (second hidden layer) w2 = tf.Variable(tf.truncated_normal([500, 100], stddev=0.1), name="weights") b2 = tf.Variable(tf.constant(0.1, shape=[100]), name="biases") with tf.name_scope('relu_activation') as scope: # relu activation, and dropout for second hidden layer a2 = tf.nn.relu(tf.matmul(drop1, w2) + b2) with tf.name_scope('dropout') as scope: drop2 = tf.nn.dropout(a2, keep_prob) with tf.name_scope('fc3') as scope: # takes the second hidden layer of 100 neurons to 10 (which is the output) w3 = tf.Variable(tf.truncated_normal([100, 10], stddev=0.1), name="weights") b3 = tf.Variable(tf.constant(0.1, shape=[10]), name="biases") with tf.name_scope('logits') as scope: # final layer doesn't have dropout logits = tf.matmul(drop2, w3) + b3
with tf.name_scope('train') as scope: with tf.name_scope('loss') as scope: # loss function cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits) # use adam optimizer for training with a learning rate of 0.001 train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy) with tf.name_scope('evaluation') as scope: # evaluation correct_prediction = tf.equal(tf.argmax(logits,1), tf.argmax(labels,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # create a summarizer that summarizes loss and accuracy tf.summary.scalar("Accuracy", accuracy) # add average loss summary over entire batch tf.summary.scalar("Loss", tf.reduce_mean(cross_entropy)) # merge summaries summary_op = tf.summary.merge_all() # create saver object saver = tf.train.Saver()
with tf.Session() as sess: # initialize variables tf.global_variables_initializer().run() # initialize summarizer filewriter fw = tf.summary.FileWriter("/tmp/nn/summary", sess.graph) # train the network for step in range(20000): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, labels: batch_ys, keep_prob:0.2}) if step%1000 == 0: acc = sess.run(accuracy, feed_dict={ x: batch_xs, labels: batch_ys, keep_prob:1}) print("mid train accuracy:", acc, "at step:", step) if step%100 == 0: # compute summary using test data every 100 steps summary = sess.run(summary_op, feed_dict={ x: mnist.test.images, labels: mnist.test.labels, keep_prob:1}) # add merged summaries to filewriter, # so they are saved to disk fw.add_summary(summary, step) print ("Final Test Accuracy:", sess.run(accuracy, feed_dict={ x: mnist.test.images, labels: mnist.test.labels, keep_prob:1})) # save trained model saver.save(sess, "/tmp/nn/my_nn.ckpt")
mid train accuracy: 0.1 at step: 0 mid train accuracy: 0.91 at step: 1000 mid train accuracy: 0.89 at step: 2000 mid train accuracy: 0.91 at step: 3000 [...] mid train accuracy: 0.97 at step: 17000 mid train accuracy: 0.98 at step: 18000 mid train accuracy: 0.97 at step: 19000 Final Test Accuracy: 0.9613
如果你想自己運行這個網絡,你可以訪問Github上的代碼。
另外,如果你能做到這一點,可以給我發私信@kasperfredn。
由於這隻是Tensorflow的一個介紹,我們沒有介紹很多,但現在應該足夠了解API文檔,你可以在其中找到可以納入代碼的模塊。
本文由北郵@愛可可-愛生活老師推薦,阿裏雲雲棲社區組織翻譯。
Introduction to Tensorflow as a Computational FrameworkKasper Fredenslund譯者:董昭男,審校:
文章為簡譯,更為詳細的內容,請查看原文
最後更新:2017-11-01 22:45:07