python_Tensorflow學習(三):TensorFlow學習基礎


一、矩陣的基本操作

import tensorflow as tf

 

# 1.1矩陣操作

sess = tf.InteractiveSession()

x = tf.ones([2, 3], "float32")

print("tf.ones():", sess.run(x))

 

tensor = [[1, 2, 3], [4, 5, 6]]

x = tf.ones_like(tensor)

print("ones_like給定的tensor類型大小一致的tensor,其所有元素為10", sess.run(x))

 

print("創建一個形狀大小為shapetensor,其初始值為value", sess.run(tf.fill([2, 3], 2)))

 

"""

tf.constant(value,dtype=None,shape=None,name='Const')

創建一個常量tensor,按照給出value來賦值,可以用shape來指定其形狀。value可以是一個數,也可以是一個list

如果是一個數,那么這個常亮中所有值的按該數來賦值。

如果是list,那么len(value)一定要小於等於shape展開后的長度。賦值時,先將value中的值逐個存入。不夠的部分,則全部存入value的最后一個值。

"""

a = tf.constant(2, shape=[2])

b = tf.constant(2, shape=[2, 2])

c = tf.constant([1, 2, 3], shape=[6])

d = tf.constant([1, 2, 3], shape=[3, 2])

 

print("constant的常量:", sess.run(a))

print("constant的常量:", sess.run(b))

print("constant的常量:", sess.run(c))

print("constant的常量:", sess.run(d))

 

"""

f.random_normal | tf.truncated_normal | tf.random_uniform

 

tf.random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)

tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

tf.random_uniform(shape,minval=0,maxval=None,dtype=tf.float32,seed=None,name=None)

這幾個都是用於生成隨機數tensor的。尺寸是shape

random_normal: 正太分布隨機數,均值mean,標准差stddev

truncated_normal:截斷正態分布隨機數,均值mean,標准差stddev,不過只保留[mean-2*stddev,mean+2*stddev]范圍內的隨機數

random_uniform:均勻分布隨機數,范圍為[minval,maxval]

"""

x = tf.random_normal(shape=[1, 5], mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

print("打印正太分布隨機數:", sess.run(x))

 

x = tf.truncated_normal(shape=[1, 5], mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

print("截斷正態分布隨機數:[mean-2*stddev,mean+2*stddev]", sess.run(x))

 

x = tf.random_uniform(shape=[1, 5], minval=0, maxval=None, dtype=tf.float32, seed=None, name=None)

print("均勻分布隨機數:[minval,maxval]", sess.run(x))

 

# 1.2 矩陣變換

 

labels = [1, 2, 3]

shape = tf.shape(labels)

print(shape)

print("返回張量的形狀:", sess.run(shape))

 

"""

tf.expand_dims(Tensor, dim)

為張量+1維。官網的例子:'t' is a tensor of shape [2]

shape(expand_dims(t, 0)) ==> [1, 2]

shape(expand_dims(t, 1)) ==> [2, 1]

shape(expand_dims(t, -1)) ==> [2, 1]

"""

labels = [1, 2, 3]

x = tf.expand_dims(labels, 0)

print("為張量+1維,但是X執行的維度維0,則不更改", sess.run(x))

x = tf.expand_dims(labels, 1)

print("為張量+1維,X執行的維度維1,則增加一維度", sess.run(x))

x = tf.expand_dims(labels, -1)

print("為張量+1維,但是X執行的維度維-1,則不更改", sess.run(x))

 

"""

tf.pack(values, axis=0, name="pack")

Packs a list of rank-R tensors into one rank-(R+1) tensor

將一個R維張量列表沿着axis軸組合成一個R+1維的張量。

"""

# x = [1, 4]

# y = [2, 5]

# z = [3, 6]

# a = tf.pack([x, y, z])

# b = tf.pack([x, y, z], axis=1)

#

# print(sess.run(a))

# print(sess.run(b))

 

 

"""

tf.concat

 

tf.concat(concat_dim, values, name="concat")

Concatenates tensors along one dimension.

將張量沿着指定維數拼接起來。個人感覺跟前面的pack用法類似

"""

t1 = [[1, 2, 3], [4, 5, 6]]

t2 = [[7, 8, 9], [10, 11, 12]]

print("tf.concat 將張量沿着指定維數進行拼接起來", sess.run(tf.concat([t1, t2], 0)))

print("tf.concat 將張量沿着指定維數進行拼接起來", sess.run(tf.concat([t1, t2], 1)))

 

"""

tf.sparse_to_dense

 

稀疏矩陣轉密集矩陣

定義為:

 

def sparse_to_dense(sparse_indices,

output_shape,

sparse_values,

default_value=0,

validate_indices=True,

name=None):

 

幾個參數的含義:

sparse_indices: 元素的坐標[[0,0],[1,2]] 表示(0,0),和(1,2)處有值

output_shape: 得到的密集矩陣的shape

sparse_values: sparse_indices坐標表示的點的值,可以是0D或者1D張量。若0D,則所有稀疏值都一樣。若是1D,則len(sparse_values)應該等於len(sparse_indices)

default_values: 缺省點的默認值

"""

 

"""

tf.random_shuffle

 

tf.random_shuffle(value,seed=None,name=None)

沿着value的第一維進行隨機重新排列

 

"""

a = [[1, 2], [3, 4], [5, 6]]

print("沿着value的第一位進行隨機的重新排列:", sess.run(tf.random_shuffle(a)))

 

"""

tf.argmax | tf.argmin

 

tf.argmax(input=tensor,dimention=axis)

找到給定的張量tensor中在指定軸axis上的最大值/最小值的位置。

"""

a = tf.get_variable(name="a", shape=[3, 4], dtype=tf.float32,

initializer=tf.random_uniform_initializer(minval=-1, maxval=1))

b = tf.argmax(input=a, dimension=0)

c = tf.argmax(input=a, dimension=1)

sess.run(tf.global_variables_initializer())

print("默認的初始化矩陣", sess.run(a))

print("0維度的最大值的位置", sess.run(b))

print("1維度的最大值的位置", sess.run(c))

 

"""

tf.equal

 

tf.equal(x, y, name=None):

判斷兩個tensor是否每個元素都相等。返回一個格式為booltensor

"""

 

"""

tf.cast

 

cast(x, dtype, name=None)

x的數據格式轉化成dtype.例如,原來x的數據格式是bool

那么將其轉化成float以后,就能夠將其轉化成01的序列。反之也可以

"""

a = tf.Variable([1, 0, 0, 1, 1])

b = tf.cast(a, dtype=tf.bool)

sess.run(tf.global_variables_initializer())

print("float的數值轉化維Bool的類型:", sess.run(b))

 

"""

tf.matmul

 

用來做矩陣乘法。若al*m的矩陣,bm*n的矩陣,那么通過tf.matmul(a,b) 結果就會得到一個l*n的矩陣

不過這個函數還提供了很多額外的功能。我們來看下函數的定義:

matmul(a, b,

transpose_a=False, transpose_b=False,

a_is_sparse=False, b_is_sparse=False,

name=None):

 

可以看到還提供了transposeis_sparse的選項。

如果對應的transpose項為True,例如transpose_a=True,那么a在參與運算之前就會先轉置一下。

而如果a_is_sparse=True,那么a會被當做稀疏矩陣來參與運算。

"""

 

"""

 

tf.reshape

 

reshape(tensor, shape, name=None)

顧名思義,就是將tensor按照新的shape重新排列。一般來說,shape有三種用法:

如果 shape=[-1], 表示要將tensor展開成一個list

如果 shape=[a,b,c,…] 其中每個a,b,c,..>0,那么就是常規用法

如果 shape=[a,-1,c,…] 此時b=-1a,c,..依然>0。這表示tf會根據tensor的原尺寸,自動計算b的值。

"""

t = [1, 2, 3, 4, 5, 6, 7, 8, 9]

sess.run(tf.global_variables_initializer())

r = tf.reshape(t, [3, 3])

print("重置為3X3", sess.run(r))

v = tf.reshape(r, [-1])

print("重置回1X9", sess.run(v))

 

h = [[[1, 1, 1],

[2, 2, 2]],

[[3, 3, 3],

[4, 4, 4]],

[[5, 5, 5],

[6, 6, 6]]]

# -1 被變成了't'

print("重置list", sess.run(tf.reshape(h, [-1])))

# -1 inferred to be 9:

print("重置2", sess.run(tf.reshape(h, [2, -1])))

# -1當前被推到維 2 : (-1 is inferred to be 2)

print("重置2", sess.run(tf.reshape(h, [-1, 9])))

# -1 inferred to be 3:

print("重置3", sess.run(tf.reshape(h, [2, -1, 3])))

 

"""

2. 神經網絡相關操作

tf.nn.embedding_lookup

 

embedding_lookup(params, ids, partition_strategy="mod", name=None,

validate_indices=True):

 

簡單的來講,就是將一個數字序列ids轉化成embedding序列表示。

假設params.shape=[v,h], ids.shape=[m], 那么該函數會返回一個shape=[m,h]的張量。用數學來表示,就是

那么這個有什么用呢?如果你了解word2vec的話,就知道我們可以根據文檔來對每個單詞生成向量。

單詞向量可以進一步用來測量單詞的相似度等等。那么假設我們現在已經獲得了每個單詞的向量,都存在param中。

那么根據單詞id序列ids,就可以通過embedding_lookup來獲得embedding表示的序列。

"""

 

"""

tf.trainable_variables

 

返回所有可訓練的變量。

在創造變量(tf.Variable, tf.get_variable 等操作)時,都會有一個trainable的選項,表示該變量是否可訓練。這個函數會返回圖中所有trainable=True的變量。

tf.get_variable(…), tf.Variable(…)的默認選項是True, tf.constant(…)只能是False

 

"""

from pprint import pprint

 

# j = tf.get_variable('a', shape=[5, 2]) # 默認 trainable=True

# k = tf.get_variable('b', shape=[2, 5], trainable=False)

# l = tf.constant([1, 2, 3], dtype=tf.int32, shape=[8], name='c') # 因為是常量,所以trainable=false

# o = tf.Variable(tf.random_uniform(shape=[3, 3]), name='d')

tvar = tf.trainable_variables()

tvar_name = [x.name for x in tvar]

print(tvar)

print(tvar_name)

sess.run(tf.global_variables_initializer())

pprint(sess.run(tvar))

 

"""

tf.gradients

 

用來計算導數。該函數的定義如下所示

 

def gradients(ys,

xs,

grad_ys=None,

name="gradients",

colocate_gradients_with_ops=False,

gate_gradients=False,

aggregation_method=None):

 

雖然可選參數很多,但是最常使用的還是ysxs。根據說明得知,

ysxs都可以是一個tensor或者tensor列表。而計算完成以后,

該函數會返回一個長為len(xs)tensor列表,

列表中的每個tensorys中每個值對xs[i]求導之和。

"""

 

"""

tf.clip_by_global_norm

 

修正梯度值,用於控制梯度爆炸的問題。梯度爆炸和梯度彌散的原因一樣,都是因為鏈式法則求導的關系,導致梯度的指數級衰減。為了避免梯度爆炸,需要對梯度進行修剪。

先來看這個函數的定義:

 

def clip_by_global_norm(t_list, clip_norm, use_norm=None, name=None):

 

輸入參數中:t_list為待修剪的張量, clip_norm 表示修剪比例(clipping ratio).

 

函數返回2個參數: list_clipped,修剪后的張量,以及global_norm,一個中間計算量。當然如果你之前已經計算出了global_norm值,你可以在use_norm選項直接指定global_norm的值。

 

那么具體如何計算呢?根據源碼中的說明,可以得到

list_clipped[i]=t_list[i] * clip_norm / max(global_norm, clip_norm),其中

global_norm = sqrt(sum([l2norm(t)**2 for t in t_list]))

 

"""

 

"""

tf.nn.dropout

 

dropout(x, keep_prob, noise_shape=None, seed=None, name=None)

按概率來將x中的一些元素值置零,並將其他的值放大。用於進行dropout操作,一定程度上可以防止過擬合

x是一個張量,而keep_prob是一個(0,1]之間的值。x中的各個元素清零的概率互相獨立,為1-keep_prob,而沒有清零的元素

,則會統一乘以1/keep_prob, 目的是為了保持x的整體期望值不變。

"""

I = tf.random_uniform(shape=[2, 5], minval=-1, maxval=1, dtype=tf.float32)

U = I

a_drop = tf.nn.dropout(U, 0.8)

print("下降的高爐", sess.run(I))

print("下降的高爐", sess.run(a_drop))

 

"""

tf.linspace | tf.range

 

tf.linspace(start,stop,num,name=None)

tf.range(start,limit=None,delta=1,name='range')

這兩個放到一起說,是因為他們都用於產生等差數列,不過具體用法不太一樣。

tf.linspace[start,stop]范圍內產生num個數的等差數列。不過注意,startstop要用浮點數表示,不然會報錯

tf.range[start,limit)范圍內以步進值delta產生等差數列。注意是不包括limit在內的。

"""

 

x = tf.linspace(start=1.0, stop=10.0, num=5, name=None)

y = tf.range(start=1, limit=10, delta=2)

print("linspace:", sess.run(x))

print("range:", sess.run(y))

# ==> [ 1. 3.25 5.5 7.75 10. ]

# ==> [1 3 5 7 9]

 

"""

tf.assign

 

assign(ref, value, validate_shape=None, use_locking=None, name=None)

tf.assign是用來更新模型中變量的值的。ref是待賦值的變量,value是要更新的值。即效果等同於 ref = value

 

"""

a = tf.Variable(0.0)

b = tf.placeholder(dtype=tf.float32, shape=[])

op = tf.assign(a, b)

 

sess.run(tf.global_variables_initializer())

print("assign:", sess.run(a))

print("assign:", sess.run(op, feed_dict={b: 5.}))

 

"""

tf.variable_scope

 

簡單的來講,就是為變量添加命名域

 

with tf.variable_scope("foo"):

with tf.variable_scope("bar"):

v = tf.get_variable("v", [1])

assert v.name == "foo/bar/v:0"

 

函數的定義為

 

def variable_scope(name_or_scope, reuse=None, initializer=None,

regularizer=None, caching_device=None, partitioner=None,

custom_getter=None):

 

各變量的含義如下:

name_or_scope: string or VariableScope: the scope to open.

reuse: True or None; if True, we Go into reuse mode for this scope as well as all sub-scopes; if None, we just inherit the parent scope reuse. 如果reuse=True, 那么就是使用之前定義過的name_scope和其中的變量,

initializer: default initializer for variables within this scope.

regularizer: default regularizer for variables within this scope.

caching_device: default caching device for variables within this scope.

partitioner: default partitioner for variables within this scope.

custom_getter: default custom getter for variables within this scope.

 

tf.get_variable_scope

 

返回當前變量的命名域,返回一個tensorflow.Python.ops.variable_scope.VariableScope變量。

 

"""


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM