解決TensorFlow調(diào)用Keras庫函數(shù)存在的問題
tensorflow在1.4版本引入了keras,封裝成庫?,F(xiàn)想將keras版本的GRU代碼移植到TensorFlow中,看到TensorFlow中有Keras庫,大喜,故將神經(jīng)網(wǎng)絡(luò)定義部分使用Keras的Function API方式進(jìn)行定義,訓(xùn)練部分則使用TensorFlow來進(jìn)行編寫。一頓操作之后,運(yùn)行,沒有報(bào)錯(cuò),不由得一喜。但是輸出結(jié)果,發(fā)現(xiàn),和預(yù)期的不一樣。難道是欠擬合?故采用正弦波預(yù)測(cè)余弦來驗(yàn)證算法模型。
部分調(diào)用keras庫代碼如上圖所示,用正弦波預(yù)測(cè)余弦波,出現(xiàn)如下現(xiàn)象:
def interface(_input): tmp = tf.keras.layers.Dense(10)(_input) vad_gru = tf.keras.layers.GRU(24, return_sequences=True)(tmp) denoise_output = tf.keras.layers.Dense(1)(vad_gru) return denoise_output
波形是斷斷續(xù)續(xù)的。而且最后不收斂。
運(yùn)行N久。。。之后
基本斷定是程序本身的問題,于是通過排查,發(fā)現(xiàn)應(yīng)該是GRU的initial_state沒有進(jìn)行更新導(dǎo)致的。導(dǎo)致波形是斷斷續(xù)續(xù)的,沒有學(xué)習(xí)到前一次網(wǎng)絡(luò)的輸出。于是,決定不使用Keras庫實(shí)現(xiàn)一遍:
部分代碼如下:
def interface(_input): tmp = tf.keras.layers.Dense(10)(_input) gru_cell = tf.nn.rnn_cell.GRUCell(vad_cell_size) with tf.name_scope('initial_state'): cell_init_state = gru_cell.zero_state(batch_size, dtype=tf.float32) cell_outputs, cell_final_state = tf.nn.dynamic_rnn( gru_cell, tmp, initial_state=cell_init_state, time_major=False) denoise_output = tf.keras.layers.Dense(1)(cell_outputs) return denoise_output, cell_init_state, cell_final_state
波形圖如下(這才是GRU的正確打開方式啊~):
再回頭看之前寫的調(diào)用keras,既然知道了是initial_state沒有更新,那么如何進(jìn)行更新呢?
網(wǎng)上查找了大量的資料,說要加上
update_ops = [] for old_value, new_value in layers.updates: update_ops.append(tf.assign(old_value, new_value))
但是加上去沒有效果,是我加錯(cuò)了還是其他的,大家歡迎指出來
以下是我做的一些嘗試,就不一一詳細(xì)說明了,大家看一下,具體不再展開,有問題大家交流一下,有解決方法的,能夠分享出來,感激不盡~
def interface(_input): # input_layer = tf.keras.layers.Input([None, 1]) # input_layer = tf.keras.layers.Input(batch_shape=(50, 20, 1)) tmp = tf.keras.layers.Dense(10)(_input) # tmp = tf.keras.layers.Dense(24)(tmp) # with tf.variable_scope('vad_gru', reuse=tf.AUTO_REUSE): # vad_gru, final_state = tf.keras.layers.GRU(24, return_sequences=True, return_state=True, stateful=True)(tmp) # print(vad_gru) # _initial_state = vad_gru.zero_state(50, tf.float32) # tf.get_variable_scope().reuse_variables() # vad_gru = tf.contrib. # tmp = tf.reshape(tmp, [-1, TIME_STEPS, vad_cell_size]) gru_cell = tf.nn.rnn_cell.GRUCell(vad_cell_size) # gru_cell = tf.keras.layers.GRUCell(self.vad_cell_size) with tf.name_scope('initial_state'): cell_init_state = gru_cell.zero_state(batch_size, dtype=tf.float32) cell_outputs, cell_final_state = tf.nn.dynamic_rnn( gru_cell, tmp, initial_state=cell_init_state, time_major=False) # print(cell_outputs.get_shape().as_list()) # cell_outputs = tf.reshape(cell_outputs, [-1, vad_cell_size]) denoise_output = tf.keras.layers.Dense(1)(cell_outputs) print(denoise_output.get_shape().as_list()) # model = tf.keras.models.Model(input_layer, denoise_output) # update_ops = [] # for old_value, new_value in model.layers[1].updates: # update_ops.append(tf.assign(old_value, new_value)) return denoise_output, cell_init_state, cell_final_state
補(bǔ)充知識(shí):TensorFlow和Keras常用方法(避坑)
TensorFlow
在TensorFlow中,除法運(yùn)算:
1.tensor除法會(huì)使結(jié)果的精度高一級(jí),可能會(huì)導(dǎo)致后面計(jì)算類型不匹配,如float32 / float32 = float64。
2.除法需要分子分母同類型,否則報(bào)錯(cuò)。
產(chǎn)生類似錯(cuò)誤提示如下:
-1.TypeError: x and y must have the same dtype, got tf.float32 != tf.int32
-2.TypeError: Input ‘y' of ‘Mul' Op has type float32 that does not match type float64 of argument ‘x'.
-3.ValueError: Tensor conversion requested dtype float64 for Tensor with dtype float32: ‘Tensor(“Sum:0”, shape=(), dtype=float32)'
-4.ValueError: Incompatible type conversion requested to type ‘int32' for variable of type ‘float32_ref'
解決辦法:
tf.cast(a, tf.float32) # 轉(zhuǎn)換成同類型即可
tf.boolean_mask
K.gather
K.argmax
K.max
以上這篇解決TensorFlow調(diào)用Keras庫函數(shù)存在的問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)html轉(zhuǎn)ubb代碼(html2ubb)
這篇文章主要介紹了python實(shí)現(xiàn)html轉(zhuǎn)ubb代碼(html2ubb),使用正則表達(dá)式寫的一個(gè)函數(shù),需要的朋友可以參考下2014-07-07Python實(shí)現(xiàn)簡(jiǎn)繁體轉(zhuǎn)換
很多時(shí)候簡(jiǎn)繁體轉(zhuǎn)換,掌握了簡(jiǎn)體與繁體的轉(zhuǎn)換,往往能夠事半功倍,本文主要介紹了Python實(shí)現(xiàn)簡(jiǎn)繁體轉(zhuǎn)換,感興趣的可以了解一下2021-06-06python網(wǎng)絡(luò)編程實(shí)例簡(jiǎn)析
這篇文章主要介紹了python網(wǎng)絡(luò)編程,有不錯(cuò)的借鑒價(jià)值,需要的朋友可以參考下2014-09-09OPENAI?API?微調(diào)?GPT-3?的?Ada?模型
這篇文章主要為大家介紹了OPENAI?API?微調(diào)?GPT-3?的?Ada?模型使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04