在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/人工智能/ 如何拿到tf.contrib.rnn.BasicLSTMCell內部的權值矩陣或

如何拿到tf.contrib.rnn.BasicLSTMCell內部的權值矩陣或者權值矩陣的名稱?

我的項目:影評情感分類
使用工具及平臺:anaconda/tensorflow/python3/RNN

神經網絡結構:一層LSTM,然后接上兩層普通神經網絡層。

遇到的本質問題:過擬合現(xiàn)象嚴重
目前已經使用的解決方法:1,dropout;2,對后面兩層普通神經網絡層權值w進行了L2 regularization.

新問題:想要嘗試對LSTM單元內部的權值矩陣進行L2 regularization.因為使用了tensorflow內置的模塊,tf.contrib.rnn.BasicLSTMCell,我不知道怎么拿到這個LSTM單元的內部權值矩陣,或者它的名稱。求大神講解。
我的lstm層代碼如下:

def lstm_cell(dropout_keep_prob,scope):
    with tf.variable_scope(scope):
    lstm_layer = tf.contrib.rnn.BasicLSTMCell(hidden_size,forget_bias=1,state_is_tuple=True,activation=tf.nn.softsign)
    lstm = tf.contrib.rnn.DropoutWrapper(lstm_layer, output_keep_prob=dropout_keep_prob)
return lstm

# lstm layer.
lstm_layer1 = lstm_cell(dropout_keep_prob,'lstm_1')
#init state
state_1 = lstm_layer1.zero_state(batch_size, tf.float32)
# get lstm rnn output.
lstm_rnn_1_in = tf.unstack(input_data_lookup, num=time_steps, axis=1)
lstm_rnn_1_out, _ =tf.contrib.rnn.static_rnn(lstm_layer1,lstm_rnn_1_in,initial_state=state_1,scope='lstm_1')

我目前的嘗試方案:

tv = tf.trainable_variables()
                 
l2_term = 0.01 * sum(
    tf.nn.l2_loss(tf_var)
        for tf_var in tv
        if tf_var.name == 'lstm_1/basic_lstm_cell/kernel:0'
)
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_predict,labels=labels))
regularization = tf.nn.l2_loss(dnn_weights) + tf.nn.l2_loss(out_weights)
loss = tf.reduce_mean(loss + 0.01 * regularization + l2_term, name = 'loss')

這樣好像不太對,而且那個LSTM內部的權值矩陣名字也會變,不一定是'lstm_1/basic_lstm_cell/kernel:0',可能是'RNN/basic_lstm_cell/Matrix:0',并且,這個權值矩陣的shape也很奇怪,跟我設置的lstm的hidden_size,time_step都沒有關系。
綜上所述:
問題一:lstm單元內部的shape是怎么來的,怎么解釋?
問題二:如何給Lstm單元權值矩陣改名字,比如命名為lstm_weigths,這樣就可以確保我用tf_var.name == lstm_weights能找到它?
問題三:我的L2 regularization寫錯了嗎?

tensorlow新手上路,求諸位大佬指點,感激不盡。

回答
編輯回答
只愛你

您好,請問現(xiàn)在解決了嗎?求解,拜托了

2017年8月26日 20:22