TensorFlow基本計算ユニットと基本操作

4474 ワード

深く学ぶなどの知識を学ぶ前に、まず有名なフレームワークTensorFlowの中のいくつかの基礎知識を理解しなければなりません.次に、まずこのフレームワークの基本的な使い方を見てみましょう.
import tensorflow as tf
a = 3  # Python          

# Create a variable.
w = tf.Variable([[0.5, 1.0]]) # tensorflow      
x = tf.Variable([[2.0], [1.0]])

y = tf.matmul(w, x) #            
print(y) # tensor          

# variables have to be explicitly initialized before you can run Ops
#         w,x,y
init_op = tf.global_variables_initializer()
#    
with tf.Session() as sess:
    sess.run(init_op)
    print(y.eval()) #             

結果は次のとおりです.
Tensor("MatMul_2:0", shape=(1, 1), dtype=float32)
[[2.]]

上から分かるように、ただ簡単な行列の乗算で、私たちはこんなに多くのコードを書いて、面倒に見えますが、仕方がありません.このフレームワークを使うにはその使い方に従わなければなりませんが、このフレームワークで深く勉強しているコードを書くのは複雑ではありません.上記のコードはTensorFlowフレームワークの基本的な使い方を示しており、ライブラリ、変数定義、初期化変数、Session操作をインポートしてから、具体的な操作を行うことができます.
次にTensorFlowフレームワークの関数の使い方を学び、numpyライブラリの関数と比較して学ぶことができます.
from numpy import int32
# float32  TensorFlow        

#     0   
tf.zeros([3, 4], int32)  # ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
#       
tf.zeros_like(tensor)  # ==> [[0, 0, 0], [0, 0, 0]]

#       1
tf.ones([2, 3], int32)  # ==> [[1, 1, 1], [1, 1, 1]]
tf.ones_like(tensor)  # ==> [[1, 1, 1], [1, 1, 1]]

# Constant 1-D Tensor populated with value list.
#       ,        
tensor = tf.constant([1, 2, 3, 4, 5, 6, 7])  # => [1 2 3 4 5 6 7]

# Constant 2-D tensor populated with scalar value -1.
#         
tensor = tf.constant(-1.0, shape=[2, 3])  # => [[-1. -1. -1.]
                                            #     [-1. -1. -1.]]

#       
tf.linspace(10.0, 12.0, 3, name="linspace")  # => [ 10.0  11.0  12.0]

# 'start' is 3
# 'limit' is 18
# 'delta' is 3
# tf.range(start, limit, delta)  
tf.range(3, 18, 3)# ==> [3, 6, 9, 12, 15]

TensorFlowのいくつかの関数とnumpyの中の用法の差が少ないことがわかります.次にTensorFlowのランダム数のいくつかの用法を見てみましょう.
#                   
norm = tf.random_normal([2, 3], mean=-1, stddev=4)

# Shuffle the first dimension of a tensor
c = tf.constant([[1, 2], [3, 4], [5, 6]])

# shuffle  
shuff = tf.random_shuffle(c)

# Each time we run these ops, different results are generated
#           。      With  
sess = tf.Session()
print(sess.run(norm))
print(sess.run(shuff))

実行結果は
[[-2.4004993   5.356218    0.51297414]
 [-4.353016    2.234075   -4.2948236 ]]
[[1 2]
 [3 4]
 [5 6]]

次はTensorFlowを使って0から4までの数字を印刷するという小さな栗を見てみましょう.原生Pythonでは簡単ですが、主にTensorFlowでの使い方を見てみましょう.
#   0 4     
state = tf.Variable(0) #      0
new_value = tf.add(state, tf.constant(1)) #    1  
update = tf.assign(state, new_value)  #  new_value  state

# Session    
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(state))    
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

得られた結果は
0
1
2
3

変数の作成時にnumpyのフォーマットをtensorフォーマットに変換する方法を見てみましょうが、この方法は推奨されません.
import numpy as np
a = np.zeros((3,3))

#  numpy        tensor  ,          
#              
ta = tf.convert_to_tensor(a) 
with tf.Session() as sess:
     print(sess.run(ta))

得られた結果は
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

次にTensorFlowでのプレースホルダの使い方を見てみましょう
#      ,         。
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2) #       
with tf.Session() as sess:
    print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))

得られた結果は
[array([14.], dtype=float32)]

まとめて、このブログにはTensorFlowのフレームワークでよく見られる使い方が含まれていますが、多くの詳細が書かれていないに違いありません.