歡迎您光臨本站 註冊首頁

解決TensorFlow調用Keras庫函數存在的問題

←手機掃碼閱讀     techdo @ 2020-07-07 , reply:0

tensorflow在1.4版本引入了keras,封裝成庫。現想將keras版本的GRU代碼移植到TensorFlow中,看到TensorFlow中有Keras庫,大喜,故將神經網絡定義部分使用Keras的Function API方式進行定義,訓練部分則使用TensorFlow來進行編寫。一頓操作之後,運行,沒有報錯,不由得一喜。但是輸出結果,發現,和預期的不一樣。難道是欠擬合?故採用正弦波預測餘弦來驗證算法模型。

部分調用keras庫代碼如上圖所示,用正弦波預測餘弦波,出現如下現象:

  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

 

波形是斷斷續續的。而且最後不收斂。

運行N久。。。之後

基本斷定是程序本身的問題,於是通過排查,發現應該是GRU的initial_state沒有進行更新導致的。導致波形是斷斷續續的,沒有學習到前一次網絡的輸出。於是,決定不使用Keras庫實現一遍:

部分代碼如下:

  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的正確打開方式啊~):

再回頭看之前寫的調用keras,既然知道了是initial_state沒有更新,那麼如何進行更新呢?

網上查找了大量的資料,說要加上

  update_ops = []  for old_value, new_value in layers.updates:    update_ops.append(tf.assign(old_value, new_value))

 

但是加上去沒有效果,是我加錯了還是其他的,大家歡迎指出來

以下是我做的一些嘗試,就不一一詳細說明了,大家看一下,具體不再展開,有問題大家交流一下,有解決方法的,能夠分享出來,感激不盡~

  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

 

補充知識:TensorFlow和Keras常用方法(避坑)

TensorFlow

在TensorFlow中,除法運算:

1.tensor除法會使結果的精度高一級,可能會導致後面計算類型不匹配,如float32 / float32 = float64。

2.除法需要分子分母同類型,否則報錯。

產生類似錯誤提示如下:

-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) # 轉換成同類型即可

tf.boolean_mask

K.gather

K.argmax

K.max



[techdo ] 解決TensorFlow調用Keras庫函數存在的問題已經有240次圍觀

http://coctec.com/docs/python/shhow-post-241562.html