自己開發了一個股票智能分析軟件,功能很強大,需要的點擊下面的鏈接獲取:
https://www.cnblogs.com/bclshuai/p/11380657.html
1.1 變量Variable
1.1.1 變量的聲明和使用
變量,它和占位符的不同是它在定義時需要賦值,而且它的數值是可以在圖的計算過程中隨時改變的。因此,占位符通常用作圖的輸入(即訓練數據),而變量用作圖中可以被“訓練”或“學習”的那些tensor,例如y=ax+b中的a和b。
通過變量類Variable來定義,使用之前需要初始化。想要將所有圖變量進行集體初始化時應該使用tf.global_variables_initializer,或者單個初始化。
_init_(self,
initial_value=None,#初始值,張量或者能轉化為張量的python對象
trainable=None,#True時,GradientTape梯度帶自動觀察這個變量的使用
validate_shape=True,是否驗證初始值的shape,True驗證,False不驗證
caching_device=None,可選填的設備字符串描述符,需要緩存讀時設置,默認為變量設備
name=None,
variable_def=None,
dtype=None,
import_scope=None,
constraint=None,
synchronization=VariableSynchronization.AUTO,
aggregation=VariableAggregation.NONE,
shape=None):
rand_t=tf.random.uniform([50,50],0,10,seed=0)#創建隨機常量數組50行50列,最大值為10,最小值為0,隨機種子是0。以這個矩陣數組作為變量的初始值。
t_a=tf.Variable(rand_t)
t_b= tf.Variable(rand_t)
r = tf.Variable(tf.random_normal([20, 10], stddev=0.35)) #以標准差0.35的正太分布初始化一個形狀為[20,10]的張量
z = tf.Variable(tf.zeros([20])) #定義變量,一個形狀為[20]的張量, 里面的元素值全部為0.
#sess.run(tf.global_variables_initializer()) #所有變量初始化
sess.run(x.initializer) #單個變量初始化
1.1.2 變量的初始化
(1)常量聲明變量並初始化
tensorflow所有的變量在使用前都需要初始化。
下面這條語句用常量1賦值聲明了變量,但是沒有初始化。
import tensorflow as tf
ss = tf.compat.v1.Session()
d60 = tf.Variable(1, dtype=tf.float32, name='number1')
如果調用下面的語句,則會報錯,因為變量d60沒有初始化
d61 = tf.tan(d60)
print(ss.run(d61))
所以我們需要顯示初始化變量d60,才能輸出正確的數據。
ss.run(d60.initializer)
d61 = tf.tan(d60)
print(ss.run(d61))#輸出1.557
d60是定義的變量,而initializer是變量的初始化操作通過tensorflow的Session來執行這個操作,我們可以print(d60.initializer)來查看這個操作是什么?操作中包含了變量的d60的名稱,作為輸入,還有d60的類型。ss.run(d60.initializer)的作用就是執行變量d60的初始化操作,來初始化變量d60。感覺這個有點麻煩。
name: "number1/Assign"
op: "AssignVariableOp"
input: "number1"
input: "number1/Initializer/initial_value"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
(2)變量聲明變量並初始化
聲明一個變量d62,以變量d60賦值。
d60 = tf.Variable(1, dtype=tf.float32, name='number1')
ss.run(d60.initializer)
d62 = tf.Variable(d60, dtype=tf.float32)#
d63=tf.tan(d62)
同樣我們還要執行變量d62的初始化操作來初始化變量d62,否則執行d63還是會報錯。同樣d60的初始化操作ss.run(d60.initializer)仍然不能省略,否則會報錯。
print(ss.run(d62.initializer))
print(ss.run(d63)) 上面d60初始化要加,否則會報錯。
補充信息:
如果要省略ss.run(d60.initializer)這一句,聲明變量d62時,可以采用下面的聲明方式
d62 = tf.Variable(d60.initialized_value(), dtype=tf.float32)
我們看看initialized_value函數的說明,意思就是返回初始化后的變量值。初始化另外一個變量時,使用這個來代替變量本身。
def initialized_value(self):
"""Returns the value of the initialized variable.
You should use this instead of the variable itself to initialize another
variable with a value that depends on the value of this variable.
但是使用這個函數,會有下面的警告。意思就是這個函數是tensorflow1.0的,后續可能會被廢棄。建議使用tensorflow2.0的函數read_value.
WARNING:tensorflow:From D:/Project/python/tensorflow/venv/src/caculate.py:72: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.
用tensorflow2.0的函數read_value來初始化另外一個變量。2.X中得到變量都是自動初始化。改成read_value()
d62 = tf.Variable(d60.read_value(), dtype=tf.float32)
d63=tf.tan(d62.read_value())
print(d63)
報如下錯誤,本地的容器不存在或者變量未初始化,所以導致了失敗。
Error while reading resource variable number1 from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist.
需要加上初始化語句才行
init_op = tf.compat.v1.global_variables_initializer()
print(ss.run(init_op))