为BasicLSTMCell指定name参数报错,构造函数无该参数的技术咨询
解决BasicLSTMCell添加name参数触发TypeError的问题
嘿,我之前也碰到过这个坑!你用的应该是TensorFlow 1.x的早期版本,这个阶段的BasicLSTMCell构造函数确实没设计name这个参数,所以才会弹出TypeError: __init__() got an unexpected keyword argument 'name'的报错。
两种可行的解决办法
根据你的需求,这里有两个靠谱的方案:
换用支持name参数的LSTM类
要是你只是想给LSTM单元起个名字区分不同实例,直接换成tf.nn.rnn_cell.LSTMCell就行——这个类在你当前的版本里应该已经支持name参数了,代码示例:tf.nn.rnn_cell.LSTMCell(num_units=self.config.num_lstm_units, state_is_tuple=True, name="some_basic_lstm")用variable_scope给LSTM变量加命名前缀
要是你非得用BasicLSTMCell,可以借助tf.variable_scope来给这个LSTM的所有变量统一加前缀,一样能区分不同的LSTM实例,代码这么写:with tf.variable_scope("some_basic_lstm"): lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=self.config.num_lstm_units, state_is_tuple=True)这样这个LSTM里的所有变量都会带上
some_basic_lstm/的前缀,和其他LSTM的变量彻底分开,效果和指定name参数差不多。
额外说明
你提到的BasicLSTMCell是基于论文《Long Short-Term Memory Recurrent Neural Network Architectures for Large Scale Acoustic Modeling》实现的,早期TensorFlow版本确实没给它加name参数,这个参数是后来在其他LSTM实现里才补上的。
内容的提问来源于stack exchange,提问作者piratesailor
相关产品推荐
相关产品推荐

