Äú¿ÉÒÔ¾èÖú£¬Ö§³ÖÎÒÃǵĹ«ÒæÊÂÒµ¡£

1Ôª 10Ôª 50Ôª





ÈÏÖ¤Â룺  ÑéÖ¤Âë,¿´²»Çå³þ?Çëµã»÷Ë¢ÐÂÑéÖ¤Âë ±ØÌî



  ÇóÖª ÎÄÕ ÎÄ¿â Lib ÊÓÆµ iPerson ¿Î³Ì ÈÏÖ¤ ×Éѯ ¹¤¾ß ½²×ù Model Center   Code  
»áÔ±   
   
 
     
   
 ¶©ÔÄ
  ¾èÖú
Google TensorFlow »úÆ÷ѧϰ¿ò¼Ü½éÉܺÍʹÓÃ
 
  3778  次浏览      27
 2018-4-9 
 
±à¼­ÍƼö:

±¾ÎÄÓÚcsdn,Ö÷ҪΪTensorFlowÊÇʲô£¬TensorFlowÌØµã¡¢°²×°ºÍʹÓá£Êý¾Ý»ñÈ¡ºÍÊäÈëµÈ¡£

TensorFlowÊÇʲô£¿

TensorFlowÊÇGoogle¿ªÔ´µÄµÚ¶þ´úÓÃÓÚÊý×Ö¼ÆË㣨numerical computation£©µÄÈí¼þ¿â¡£ËüÊÇ»ùÓÚÊý¾ÝÁ÷ͼµÄ´¦Àí¿ò¼Ü£¬Í¼ÖеĽڵã±íʾÊýѧÔËË㣨mathematical operations£©£¬±ß±íʾÔËËã½ÚµãÖ®¼äµÄÊý¾Ý½»»¥¡£TensorFlow´Ó×ÖÃæÒâÒåÉÏÀ´½²ÓÐÁ½²ãº¬Ò壬һ¸öÊÇTensor£¬Ëü´ú±íµÄÊǽڵãÖ®¼ä´«µÝµÄÊý¾Ý£¬Í¨³£Õâ¸öÊý¾ÝÊÇÒ»¸ö¶àά¶È¾ØÕó£¨multidimensional data arrays£©»òÕßһάÏòÁ¿£»µÚ¶þ²ãÒâ˼Flow,Ö¸µÄÊÇÊý¾ÝÁ÷£¬ÐÎÏóÀí½â¾ÍÊÇÊý¾Ý°´ÕÕÁ÷µÄÐÎʽ½øÈëÊý¾ÝÔËËãͼµÄ¸÷¸ö½Úµã¡£

TensorFlowÊÇÒ»¸ö·Ç³£Áé»îµÄ¿ò¼Ü£¬ËüÄܹ»ÔËÐÐÔÚ¸öÈ˵çÄÔ»òÕß·þÎñÆ÷µÄµ¥¸ö»ò¶à¸öCPUºÍGPUÉÏ£¬ÉõÖÁÊÇÒÆ¶¯É豸ÉÏ¡£TensorFlow×îÔçÊÇGoogle´óÄÔÍŶÓΪÁËÑо¿»úÆ÷ѧϰºÍÉî¶ÈÉñ¾­ÍøÂç¶ø¿ª·¢µÄ£¬µ«ºóÀ´·¢ÏÖÕâ¸öϵͳ×㹻ͨÓã¬Äܹ»Ö§³Ö¸ü¼Ó¹ã·ºµÄÓ¦Óá£ÖÁÓÚΪʲô¹È¸èÒª¿ªÔ´Õâ¸ö¿ò¼Ü£¬ËüÊÇÕâÑù»ØÓ¦µÄ£º

¡°If TensorFlow is so great, why open source it rather than keep it proprietary? Theanswer is simpler than you might think : We believe that machine learning is akey ingredient to the innovative products and technologies of the future . Research in this area is global and growing fast , but lacks standard tools. Bysharing what we believe to be one of the best machine learning toolboxes in theworld , we hope to create an open standard for exchanging research ideas and putting machine learning in products. Google engineers really do use Tensor Flowin user-facing products and services, and our research group intends to share TensorFlow implementations along side many of our research publications.¡±

TensorFlowÌØµã

1. Áé»î£¨Deep Flexibility£©

Ëü²»½öÊÇ¿ÉÒÔÓÃÀ´×öÉñ¾­ÍøÂçËã·¨Ñо¿£¬Ò²¿ÉÒÔÓÃÀ´×öÆÕͨµÄ»úÆ÷ѧϰËã·¨£¬ÉõÖÁÊÇÖ»ÒªÄãÄܹ»°Ñ¼ÆËã±íʾ³ÉÊý¾ÝÁ÷ͼ£¬¶¼¿ÉÒÔÓÃTensorFlow¡£

2. ±ãЯ£¨True Portability£©

Õâ¸ö¹¤¾ß¿ÉÒÔ²¿ÊðÔÚ¸öÈËPCÉÏ£¬µ¥CPU£¬¶àCPU£¬µ¥GPU£¬¶àGPU£¬µ¥»ú¶àGPU£¬¶à»ú¶àCPU£¬¶à»ú¶àGPU£¬AndroidÊÖ»úÉϵȣ¬¼¸ºõº­¸Ç¸÷ÖÖ³¡¾°µÄ¼ÆËãÉ豸¡£

3. Ñо¿ºÍ²úÆ·µÄÇÅÁº£¨Connect Research andProduction£©

Ôڹȸ裬Ñо¿¿ÆÑ§¼Ò¿ÉÒÔÓÃTensorFlowÑо¿ÐµÄËã·¨£¬²úÆ·ÍŶӿÉÒÔÓÃËüÀ´ÑµÁ·Êµ¼ÊµÄ²úÆ·Ä£ÐÍ£¬¸üÖØÒªµÄÊÇÕâÑù¾Í¸üÈÝÒ×½«Ñо¿³É¹ûת»¯µ½Êµ¼ÊµÄ²úÆ·¡£ÁíÍâGoogleÔÚ°×Æ¤ÊéÉÏ˵µÀ£¬¼¸ºõËùÓеIJúÆ·¶¼Óõ½ÁËTensorFlow£¬±ÈÈçËÑË÷ÅÅÐò£¬ÓïÒôʶ±ð£¬¹È¸èÏà²á£¬×ÔÈ»ÓïÑÔ´¦ÀíµÈ¡£

4. ×Ô¶¯×ö΢·ÖÔËË㣨Auto-Differentiation£©

»úÆ÷ѧϰÖеĺܶàËã·¨¶¼Óõ½ÁËÌݶȣ¬Ê¹ÓÃTensorFlow£¬Ëü½«×Ô¶¯°ïÄãÇó³öÌݶȣ¬Ö»ÒªÄ㶨ÒåºÃÄ¿±êº¯Êý£¬Ôö¼ÓÊý¾Ý¾ÍºÃÁË¡£ÌýÉÏÈ¥ºÜÓÕÈË£¬ÔÝʱ²»ÖªµÀ¾ßÌåզʵÏֵġ£

5. ÓïÑÔÁé»î£¨Language Options£©

TensorFlowʹÓÃC++ʵÏֵģ¬È»ºóÓÃPython·â×°£¬ÔÝʱֻ֧³ÖÕâÁ½ÖÖÓïÑÔ£¬¹È¸èºÅÕÙÉçÇøÍ¨¹ýSWIG¿ª·¢¸ü¶àµÄÓïÑÔ½Ó¿ÚÀ´Ö§³ÖTensorFlow¡£

6. ×î´ó»¯ÐÔÄÜ£¨Maximize Performance£©

ͨ¹ý¶ÔỊ̈߳¬¶ÓÁкÍÒì²½¼ÆËãµÄÖ§³Ö£¨first-class support£©£¬TensorFlow¿ÉÒÔÔËÐÐÔÚ¸÷ÖÖÓ²¼þÉÏ£¬Í¬Ê±¸ù¾Ý¼ÆËãµÄÐèÒª£¬ºÏÀí½«ÔËËã·ÖÅäµ½ÏàÓ¦µÄÉ豸£¬±ÈÈç¾í»ý¾Í·ÖÅäµ½GPUÉÏ¡£

TensorFlow°²×°

¾ßÌå°²×°¿ÉÒԲο´Õâ¸öÁ´½Ó£º

https://www.tensorflow.org /versions /master/get_started/os_setup.html

Ô´Âë¿ÉÒÔ´ÓÕâ¸öÁ´½ÓÏÂÔØ£º

https://github.com /tensorflow/tensorflow

°²×°ºÜ¼òµ¥£¬Ã»Óг¢ÊÔGPU£º

# Ubuntu/Linux 64-bit
$ sudo apt-get install python-pip python-dev
# Mac OS X
$ sudo easy_install pip
Install TensorFlow:

# Ubuntu/Linux 64-bit, CPU only:
$ sudo pip install --upgrade https: // storage.googleapis.com /tensorflow /linux/cpu /tensorflow-0.6.0- cp27 -none-linux_ x86_64 .whl

# Ubuntu/Linux 64-bit, GPU enabled:
$ sudo pip install --upgrade https:// storage.googleapis.com /tensorflow/linux/gpu/ tensorflow-0.6.0 - cp27- none- linux_x 86_64.whl
# Mac OS X, CPU only:
$ sudo easy_install --upgrade six
$ sudo pip install --upgrade https:// storage. googleapis .com / tensorflow /mac/ tensorflow- 0.6.0 - py2- none- any.whl

°²×°Íê³Éºó£¬ÊäÈëÈçÏÂÃüÁ¼ì²âÊÇ·ñ°²×°³É¹¦¡£

$ python
...
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>>

Èç¹û±¨ÎÞ·¨»ñÈ¡

CPU

ÐÅÏ¢´íÎ󣬿ÉÒÔÔÚÉÏÊö´úÂëÖУ¬ÐÞ¸ÄÌí¼ÓÈçϼ¸ÐУº

NUM_CORES = 4 # Choose how many cores to use.
sess = tf.Session(
config=tf.ConfigProto (inter_op_ parallelism_threads = NUM_CORES,
intra_op_ parallelism_ threads = NUM_CORES))

»ù´¡Ê¹ÓÃ

ÔÚʹÓÃTensorFlow֮ǰ£¬ÓбØÒªÁ˽âÈçϼ¸¸ö¸ÅÄ

1. ¼ÆËãÊÇÓÃͼµÄÐÎʽ±íʾµÄ¡£

2. SessionsÊÇÖ´ÐеÄÈë¿Ú£¬ÀàËÆÓÚSparkContext¡£

3. Êý¾ÝÊÇÓÃtensor±íʾµÄ¡£

4. VariablesÓÃÀ´±íʾ¿É±ä״̬£¬±ÈÈçÄ£ÐͲÎÊý¡£

5. ʹÓÃfeedsºÍfetches´ÓÔËËã½ÚµãÊäÈëºÍÊä³öÊý¾Ý¡£

TensorFlow¼ÆËã¿ò¼ÜÒªÇóËùÓеļÆËã¶¼±íʾ³Éͼ£¬½ÚµãÔÚͼÖб»³ÆÎªÔËËãop(operation¼ò³Æ)¡£Ò»¸öÔËËã¿ÉÒÔ»ñµÃÁã¸ö»òÕß¶à¸öTensors£¬¾­¹ý¼ÆË㣬¿ÉÒÔ²úÉúÁã¸ö»òÕß¶à¸öTensors¡£Ò»¸öTensorÊÇÒ»¸ö¶àάÊý×飬¾Ù¸öÀý×Ó£¬¿ÉÒÔ°ÑÒ»ÅúͼÏñÊý¾Ý±íʾ³ÉÒ»¸ö4ά¸¡µãÊý×é[batch, height, width, channels]

¼ÆËãͼÊÇͨ¹ýSessionÌá½»£¬Ò»¸öSession¾ö¶¨Í¼ÖеÄÔËËã¸Ãµ½ÄǸöÉ豸ÉÏÈ¥¼ÆË㣬±ÈÈçÊÇÑ¡CPU»¹ÊÇCPU¡£ÔËËãop²úÉúµÄ½á¹ûÔÚpythonÖÐÊÇÒ»¸önumpy.ndarrayÊý×é¶ÔÏó£¬ÔÚCºÍC++ÖÐÊÇtensorflow::TensorʵÀý¡£

¹¹½¨Ò»¸ö¼ÆËãͼ

¹¹½¨Í¼Ê×ÏÈÓй¹½¨ÔËËãop£¬op²»Ò»¶¨ÒªÓÐÊäÈ룬¿ÉÒÔÓÃConstantÉÌÁ¿À´¹¹½¨Ò»¸öÔËËã½Úµã£¬È»ºó°ÑËüµÄ½á¹ûÊä³öµ½ÁíÍâµÄÔËËãÖС£ÈçÏÂÊÇÀûÓÃConstantÉú³ÉÒ»¸ö¼òµ¥µÄÔËË㣬²¢ÇÒÒԴ˹¹½¨ÁËÒ»¸ö¼ÆËãͼ¡£

import tensorflow as tf
# ´´½¨Ò»¸ö1X2µÄ¾ØÕómatrix1
matrix1 = tf.constant([[3., 3.]])
# ´´½¨Ò»¸ö2X1µÄ¾ØÕómatrix2
matrix2 = tf.constant([[2.],[2.]])
# ´´½¨matrix1ºÍmatrix2µÄ³Ë»ýÔËË㣬·µ»Ø¾ØÕóproduct
product = tf.matmul(matrix1, matrix2)

ÉÏÃæÕâ¸öͼÖаüº¬Èý¸öÔËË㣬Á½¸öconstant()ÔËË㣬һ¸ö¾ØÕó³Ë·¨ÔËËãmatmul()¡£Òª»ñµÃ×îÖÕ¾ØÕó³Ë»ýµÄ½á¹û£¬ÐèÒªÓÃsessionÌá½»Õâ¸öͼ¡£

SessionÌá½»¼ÆËãͼ

# ´´½¨session¶ÔÏó sess = tf.Session()
# ͨ¹ýSessionÖÐrun()·½·¨Ìá½»¼ÆËãͼ
result = sess.run(product)
print(result)
# ==> [[ 12.]]
# ÔËÐÐÍê¹Ø±ÕÕâ¸öSession
sess.close()

µ±SessionÔËÐÐÍê³Éºó£¬ÐèÒªÊÖ¶¯¹Ø±Õ£¬µ±È»ÄãÒ²¿ÉÒÔÀûÓá°with¡±¹Ø¼ü×Ö£¬ÔËÐÐÍê×Ô¶¯¹Ø±Õ¡£

with tf.Session() as sess:
result= sess.run([product])
print(result)

ÔÚ·Ö²¼Ê½ÔËÐл·¾³Ï£¬Ò»°ã²»ÐèÒªÊÖ¶¯Ö¸³öÄÄЩÔËËã¸Ã·Åµ½ÄÄЩ»úÆ÷ÉÏ£¬±ÈÈçCPU£¬GPU£¬µ«ÊÇ¿ò¼ÜÌṩÁËÊÖ¶¯ÉèÖõŦÄÜ¡£

with tf.Session() as sess:
withtf.device("/gpu:1"):
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
...

Ŀǰ֧³ÖÕ⼸ÖÖÉ豸£º

"/cpu:0": The CPU of your machine.

"/gpu:0": The GPU of your machine, if you have one.

"/gpu:1": The second GPU of your machine, etc.

½»»¥Ê½Ê¹ÓÃ

TensorFlowÌṩÁËÒ»¸öÀàËÆiPythonµÄ»úÖÆ£¬Í¨¹ý´´½¨InteractiveSeesion¶ÔÏóÀ´ÊµÏÖ¡£

# Enter an interactive TensorFlow Session.
import tensorflow as tf
sess = tf.InteractiveSession()

x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])

# Initialize 'x' using the run() method of itsinitializer op.
x.initializer.run()

# Add an op to subtract 'a' from 'x'. Run it and print the result
sub = tf.sub(x, a)
print(sub.eval())
# ==> [-2. -1.]

# Close the Session when we're done.
sess.close()

±äÁ¿£¨Variables£©

±äÁ¿Î¬»¤Õû¸öÖ´ÐÐͼ¹ý³ÌÖмäµÄ״̬£¬±ÈÈçHadoopÖеļÆÊýÆ÷¾ÍÊÇÒ»¸ö±äÁ¿¡£¾ßÌåÔÚ»úÆ÷ѧϰÈÎÎñÖУ¬µ±ÎÒÃÇѵÁ·Ò»¸öÉî¶ÈÉñ¾­ÍøÂçµÄʱºò£¬Ã¿Ò»²ãµÄ½ÚµãÈ¨ÖØ¾ÍÊÇÓÃVariableÀ´±íʾµÄ£¬ÔÚѵÁ·¹ý³ÌÖУ¬È¨Öػ᲻¶ÏµØ¸üС£¿´ÈçÏÂʾÀý£º

# Create a Variable, that will be initializedto the scalar value 0.
state = tf.Variable(0,name="counter")
# Create an Op to add one to `state`.
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)
# Variables must be initialized by running an`init` Op after having
# launched the graph. We first have to add the `init` Op to thegraph.
init_op = tf.initialize_all_variables()
# Launch the graph and run the ops.
with tf.Session() as sess:
# Runthe 'init' op
sess.run(init_op)
# Printthe initial value of 'state'
print(sess.run(state))
# Runthe op that updates 'state' and print 'state'.
for _in range(3):
sess.run(update)
print(sess.run(state))
# output:
# 0
# 1
# 2
# 3

assign()ÊǸ³ÖµÔËË㣬ʵ¼ÊµÄÖ´ÐÐÊÇÔÚ run() ±»Ö´ÐеÄʱºò¿ªÊ¼¡£

Êý¾Ý»ñÈ¡£¨Fetches£©

»ñȡһ¸öopµÄÔËËã½á¹û£¬¿ÉÒÔͨ¹ýµ÷ÓÃSessionÖеÄrun£¨£©·½·¨£¬Í¬Ê±¿ÉÒÔ»ñµÃ¶à¸ö½á¹û£¬ËùÓнá¹û»ñȡֻÐèÒªÖ´ÐÐÒ»´ÎrunÇëÇó¡£

input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)
with tf.Session() as sess:
result= sess.run([mul, intermed])
print(result)
# output:
# [array([ 21.], dtype=float32), array([ 7.],dtype=float32)]

Êý¾ÝÊäÈ루Feeds£©

֮ǰ½éÉܵÄÊý¾ÝÊäÈëʹÓÃConstant£¬Ö±½ÓÊäÈëÒ»¸öÃ÷È·µÄ³£Á¿Êý¾Ý£¬TensorFlowͬʱÌṩÁËÒ»ÖÖ¡°Õ¼Î»·û£¨placeholder£©¡±·½Ê½£¬ÓÃÀ´±íʾһ¸öÊý¾Ý£¬È»ºóÔÚµ÷ÓÃrun£¬Í¨¹ý²ÎÊý´«ÈëÅúÁ¿ÊäÈëÊý¾Ý½øÈ¥ºó£¬¾ßÌå»ñÈ¡Êý¾Ý£¬¾ßÌå¿ÉÒÔÈçÏÂʾÀý£º

# ´´½¨Á½¸öÊäÈëinput1ºÍinput2£¬ÕâʱÕâÁ½¸öÊý¾ÝÀïÃæÊ²Ã´¶¼Ã»ÓÐ

# ¿ÉÒÔÀí½âΪÉêÃ÷ÁËÁ½¸öÊäÈë±äÁ¿¡£

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)
> with tf.Session() as sess:
print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))
# output:
<# [array([ 14.], dtype=float32)]

¾ßÌåÊäÈëÊÇfeed_dict¡£

   
3778 ´Îä¯ÀÀ       27
Ïà¹ØÎÄÕÂ

»ùÓÚͼ¾í»ýÍøÂçµÄͼÉî¶Èѧϰ
×Ô¶¯¼ÝÊ»ÖеÄ3DÄ¿±ê¼ì²â
¹¤Òµ»úÆ÷ÈË¿ØÖÆÏµÍ³¼Ü¹¹½éÉÜ
ÏîĿʵս£ºÈçºÎ¹¹½¨ÖªÊ¶Í¼Æ×
 
Ïà¹ØÎĵµ

5GÈ˹¤ÖÇÄÜÎïÁªÍøµÄµäÐÍÓ¦ÓÃ
Éî¶ÈѧϰÔÚ×Ô¶¯¼ÝÊ»ÖеÄÓ¦ÓÃ
ͼÉñ¾­ÍøÂçÔÚ½»²æÑ§¿ÆÁìÓòµÄÓ¦ÓÃÑо¿
ÎÞÈË»úϵͳԭÀí
Ïà¹Ø¿Î³Ì

È˹¤ÖÇÄÜ¡¢»úÆ÷ѧϰ&TensorFlow
»úÆ÷ÈËÈí¼þ¿ª·¢¼¼Êõ
È˹¤ÖÇÄÜ£¬»úÆ÷ѧϰºÍÉî¶Èѧϰ
ͼÏñ´¦ÀíËã·¨·½·¨Óëʵ¼ù