tensorflow常用函式

2021-08-14 00:26:09 字數 1384 閱讀 8790

1.variable

主要在於一些可訓練變數(trainable variables),比如模型的權重(weights)或者偏執值(bias)

1)宣告時,必須提供初始值

2)在真實訓練時,其值是會改變的,自然事先需要指定初始值

weights = tf.variable(tf.random_normal([in_size,out_size]),name='w')

biases = tf.variable(tf.zeros([1,out_size]) + 0.1,name='b')

2.placeholder

中文意思是佔位符,在tensorflow中類似於函式引數,用於得到傳遞進來的真實的訓練樣本

1)不必指定初始值,可在執行時,通過 session.run 的函式的 feed_dict 引數指定

2)僅僅作為一種佔位符

xs = tf.placeholder(tf.float32,[none,784])
3.cast

cast(x, dtype, name=none) 

將x的資料格式轉化成dtype.例如,原來x的資料格式是bool,

那麼將其轉化成float以後,就能夠將其轉化成0和1的序列。反之也可以

4.truncated_normal

函式產生正太分布,均值和標準差自己設定

shape表示生成張量的維度,

mean是均值,stddev是標準差

init = tf.truncated_normal(shape, mean, stddev)

xx = tf.variable(init)

5.constant

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

tf.constant(value,dtype=none,shape=none,name=』const』)
6.reshpae

函式的作用是將tensor變換為引數shape的形式

其中shape為乙個列表形式,特殊的一點是列表中可以存在-1。-1代表的含義是不用我們自己指定這一維的大小,函式會自動計算,但列表中只能存在乙個-1

//根據shape如何變換矩陣,其實簡單的想就是,

reshape(t, shape) => reshape(t, [-1]) => reshape(t, shape)

//首先將矩陣t變為一維矩陣,然後再對矩陣的形式更改就可以了

7.reduction_indices引數

參考:

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 ...

Tensorflow 常用函式

1.tqdm list 方法可以傳入任意一種list,比如陣列 from tqdm import tqdm for i in tqdm range 1000 do something pass2.w1 tf.variable tf.truncated normal 5,5,1,inflated 0 ...