1. 程式人生 > >GitHub超過4700星的TensorFlow(Amirsina Torfi博士)程式碼學習筆記(一)

GitHub超過4700星的TensorFlow(Amirsina Torfi博士)程式碼學習筆記(一)

用TensorFlow的應該都知道,git上的一個大神弗吉尼亞理工博士Amirsina Torfi在GitHub上貢獻了一個新的教程,星星數當天就破千,現在已經4721了,估計這個文章寫完又得漲點。
完整程式碼連結(1積分):https://download.csdn.net/download/qq_32166779/10737966
現在針對博士給的程式碼進行簡答的分析一下,下載後共有6個資料夾。
在這裡插入圖片描述
第一個檔案(1_Introduction)就三個python檔案:
在這裡插入圖片描述
首先是basic_eager_api.py檔案,我覺得,這篇主要就是想告訴大家善用eager模組而已。當寫下語句"c = a + b"後(以及其他任何tf開頭的函式),就會直接執行相應的操作並得到值,而不再像之前那樣,生成一個Tensor,通過sess.run()才能拿到值。注意:這種Eager模式一旦被開啟就不能被關閉。

from __future__ import absolute_import, division, print_function
import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe
# Set Eager API
print("Setting Eager mode...")
tfe.enable_eager_execution()
# Define constant tensors
print("Define constant tensors")
a = tf.constant(2)
print("a = %i" % a)
b = tf.constant(3)
print("b = %i" % b)
# Run the operation without the need for tf.Session
print("Running operations, without tf.Session")
c = a + b
print("a + b = %i" % c)
d = a * b
print("a * b = %i" % d)
# Full compatibility with Numpy
print("Mixing operations with Tensors and Numpy Arrays")
# Define constant tensors
a = tf.constant([[2., 1.],
                 [1., 0.]], dtype=tf.float32)
print("Tensor:\n a = %s" % a)
b = np.array([[3., 0.],
                [5., 1.]], dtype=np.float32)
print("NumpyArray:\n b = %s" % b)
# Run the operation without the need for tf.Session
print("Running operations, without tf.Session")
c = a + b
print("a + b = %s"

第二個檔案basic_operations.py
主要是常量和變數區別:
常量:
a = tf.constant(2)
b = tf.constant(3)
sess.run(a+b)
變數:
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
add = tf.add(a, b)
sess.run(add, feed_dict={a: 2, b: 3})

from __future__ import print_function



import tensorflow as tf



# Basic constant operations

# The value returned by the constructor represents the output

# of the Constant op.

a = tf.constant(2)

b = tf.constant(3)



# Launch the default graph.

with tf.Session() as sess:

    print("a=2, b=3")
    print("Addition with constants: %i" % sess.run(a+b))
    print("Multiplication with constants: %i" % sess.run(a*b))


# Basic Operations with variable as graph input

# The value returned by the constructor represents the output

# of the Variable op. (define as input when running session)

# tf Graph input

a = tf.placeholder(tf.int16)

b = tf.placeholder(tf.int16)



# Define some operations

add = tf.add(a, b)

mul = tf.multiply(a, b)



# Launch the default graph.

with tf.Session() as sess:

    # Run every operation with variable input
    print("Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3}))
    print("Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 

第三個檔案helloworld.py
簡單的一逼,,,,,

from __future__ import print_function



import tensorflow as tf



# Simple hello world using TensorFlow



# Create a Constant op

# The op is added as a node to the default graph.

#

# The value returned by the constructor represents the output

# of the Constant op.

hello = tf.constant('Hello, TensorFlow!')



# Start tf session

sess = tf.Session()



# Run the op

print(sess.run(hello))