如何将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