gettf.shape(a)和a.get_shape()比較
相同:都可以得到
tensor a
的尺寸不同:
·tf.shape()中a
資料的型別可以是
tensor, list, array
返回的是乙個
tensor
時。要想知道是多少,必須通過
sess.run()
tensor.get_shape()
返回的是元組,不能放到
sess.run()
裡面,這個裡面只 能放
operation
和tensor
a.get_shape().as_list()返回乙個元組,需要通過as_list()的操作轉換成list.
tf.add_to_collection():把變數放入乙個集合,把很多變數變成乙個列表
tf.get_collection():
從乙個集合中取出全部變數,是乙個列表
tf.add_n:
把乙個列表的東西都依次加起來
python的global語句使用
python
中定義函式時,若想在函式內部對函式外的變數進行操作,就需要在函式內部宣告其為global。
tf.constant_initializer()也可以簡寫為
tf.constant()
初始化為常數,這個非常有用,通常偏置項就是用它初始化的。
tf.argmax(values,axis):
返回values
在axis
維度最大值的索引
tf. reduce_max(values,axis):
返回values
在axis
維度的最大值,結果維度為
values的維
度-1,且shape
為去掉axis
之後values
的shape,
順序不變。
tf.assign(a, new_number):
這個函式的功能主要是把
a的值變為
new_number
tf.assign(a, b):
通過增加
b來更新
a的值,即
a = a + b
tf.ceil(x, name=none) x
張量,上述操作返回不小於
x 的元素最小整數.
tf.control_dependencies(control_inputs)用法解析:
tf.control_dependencies(control_inputs):
ops表示首先執行
control_inputs
表示的op
再執行依賴於此op的
ops
例如:
import numpy as np
import tensorflow as tf
x = tf.variable(0)
x_add_1 = tf.assign_add(x, 1) #
with tf.control_dependencies([x_add_1]):
y = x + 2
with tf.session() as sess:
sess.run(tf.global_variables_initializer())
for _ in range(5):
print(sess.run(y))
3 4 5 6 7
tf.identity(input,name=none)返回乙個具有相同形狀張量和內容作為輸入;
hasattr(object, name)用於判斷物件(前者)是否包含對應的屬性(後者)。
object --
物件。
name --
字串,屬性名。
如果物件有該屬性返回
true
,否則返回
false。
class coordinate:
x = 10 #
包含屬性
x,y,z
三個屬性
y = -5
z = 0
point1 = coordinate()
print(hasattr(point1, 'x'))
print(hasattr(point1, 'y'))
print(hasattr(point1, 'z'))
print(hasattr(point1, 'no')) #
沒有該屬性
執行結果:
true
true
true
false
注意tf.cast()強制型別轉換的規則
import tensorflow as tf
x1 = 1.3
x2 = 1.8
x3= -1.9
x4 =-0.5
with tf.session() as sess:
print(sess.run(tf.cast(x1,tf.int32)))
print(sess.run(tf.cast(x2,tf.int32)))
print(sess.run(tf.cast(x3,tf.int32)))
print(sess.run(tf.cast(x4,tf.int32)))
執行結果:
1 1 -1 0
總而言之,
tf.cast()
強制轉換為整數時,只保留整數部分,小數部分捨棄。這樣當
x>0
時,數字變小;當
x<0
數字變大
tf.clip_by_value(a, min, max)
輸入乙個張量a,把a中的每乙個元素的值都壓縮在min和max之間。小於min的讓它等於min,大於max的元素的值等於max。
tensorflow常用函式
1.variable 主要在於一些可訓練變數 trainable variables 比如模型的權重 weights 或者偏執值 bias 1 宣告時,必須提供初始值 2 在真實訓練時,其值是會改變的,自然事先需要指定初始值 weights tf.variable tf.random normal ...
TensorFlow常用函式
使用tensorflow計算流程 1 prepare train data 2 define model and graph 3 choose optimizer 4 create a session to run import tensorflow as tf 1.tf.reduce mean i...
Tensorflow常用函式
w1 tf.variable tf.random normal 2,3 sttdev 1,seed 1 生成矩陣的均值為0,方差為2。使用seed 1指定隨機種子,可以保證每次執行得到的結果相同。sess.run w1.initializer 初始化w1 sess.run tf.initiable ...