如何拿到tf.contrib.rnn.BasicLSTMCell内部的权值矩阵或者权值矩阵的名称?

新手上路,请多包涵

我的项目:影评情感分类
使用工具及平台:anaconda/tensorflow/python3/RNN

神经网络结构:一层LSTM,然后接上两层普通神经网络层。

遇到的本质问题:过拟合现象严重
目前已经使用的解决方法: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新手上路,求诸位大佬指点,感激不尽。

阅读 6.3k
1 个回答
新手上路,请多包涵

您好,请问现在解决了吗?求解,拜托了