无法使用自定义var_list初始化tensorflow AdamOptimizer

时间:2017-09-28 20:39:52

标签: tensorflow neural-network

这是更复杂模型的一部分(这是自动编码器部分):

autoweights = {
    'encoder_h1': tf.Variable(tf.random_normal([num_input, num_hidden_1])),
    'encoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_hidden_2])),
    'decoder_h1': tf.Variable(tf.random_normal([num_hidden_2, num_hidden_1])),
    'decoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_input])),
}
autobiases = {
    'encoder_b1': tf.Variable(tf.random_normal([num_hidden_1])),
    'encoder_b2': tf.Variable(tf.random_normal([num_hidden_2])),
    'decoder_b1': tf.Variable(tf.random_normal([num_hidden_1])),
    'decoder_b2': tf.Variable(tf.random_normal([num_input])),
}
然后我收集我的变量:

aut_params = [ k for k in autoweights] + [ k for k in autobiases]

将它们传递给AdamOptimizer

optimizer = tf.train.AdamOptimizer(
    learning_rate=1e-4, 
    beta1=0.5, 
    beta2=0.9
).minimize(loss, var_list=aut_params)

然后我得到一个奇怪的错误,我不明白:

~/Documents/ML/OutlierGAN/outlierganv1/env/lib/python3.6/site-packages/tensorflow/python/training/optimizer.py in _get_processor(v)
    149 def _get_processor(v):
    150   """The processor of v."""
--> 151   if v.op.type == "VarHandleOp":
    152     return _DenseResourceVariableProcessor(v)
    153   if isinstance(v, variables.Variable):

AttributeError: 'str' object has no attribute 'op'

1 个答案:

答案 0 :(得分:2)

由于您的变量有字典,即key:TensorFlowVariable,您使用这些变量的键构建列表,这些变量是字符串而不是值:它们是实际的TensorFlow对象。因此str has no attribute...