首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TensorFlow:并不是所有的变量都会被恢复

TensorFlow:并不是所有的变量都会被恢复
EN

Stack Overflow用户
提问于 2017-03-03 07:07:29
回答 1查看 204关注 0票数 0

我有另一个TensorFlow查询:

我正在训练一个回归模型,保存权重和偏差,然后恢复它们,以便在不同的数据集上重新运行模型。至少,这是我想要做的。并不是我所有的体重都恢复了。以下是用于保存我的变量的代码:

代码语言:javascript
复制
# Add ops to save and restore all the variables.
saver = tf.train.Saver({**weights, **biases})

# Save the variables to disk.
save_path = saver.save(sess, "Saved_Vars.ckpt")

下面是我恢复和运行模型的全部代码:

代码语言:javascript
复制
# Network Parameters
n_hidden_1 = 9
n_hidden_2 = 56
n_hidden_3 = 8
n_input = 9
n_classes = 1

# TensorFlow Graph Input
x = tf.placeholder("float", [None, n_input])

# Create Multilayer Model
def multilayer_perceptron(x, weights, biases):
    # First hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)

    # Second hidden layer with RELU activation
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)

    # Second hidden layer with RELU activation
    layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])
    layer_3 = tf.nn.relu(layer_3)

    # Last output layer with linear activation
    out_layer = tf.matmul(layer_3, weights['out']) + biases['out']
    return out_layer

# weights and biases
weights = {
        'h1': tf.Variable(tf.zeros([n_input, n_hidden_1])),
        'h2': tf.Variable(tf.zeros([n_hidden_1, n_hidden_2])),
        'h3': tf.Variable(tf.zeros([n_hidden_2, n_hidden_3])),
        'out': tf.Variable(tf.zeros([n_hidden_3, n_classes]))
}

biases = {
        'b1' : tf.Variable(tf.zeros([n_hidden_1])),
        'b2': tf.Variable(tf.zeros([n_hidden_2])),
        'b3': tf.Variable(tf.zeros([n_hidden_3])),
        'out': tf.Variable(tf.zeros([n_classes]))
}

# Construct Model
pred = multilayer_perceptron(x, weights, biases)
pred = tf.transpose(pred)

# Initialize variables
init = tf.global_variables_initializer()

# RUNNING THE SESSION

# launch the session
sess = tf.InteractiveSession()


# Initialize all the variables
sess.run(init)

# Add ops to save and restore all the variables.
saver = tf.train.Saver({**weights, **biases})

# Restore variables from disk.
saver.restore(sess, "Saved_Vars.ckpt")

# Use the restored model to predict the target values
prediction = sess.run(pred, feed_dict={x:dataVar_scaled}) #pred.eval(feed_dict={x:X})

现在,这就是让我困惑/沮丧/烦恼的地方。根据权重,我可以恢复“h1”、“h2”和“h3”,但不能恢复“out”。为什么不“出去”呢?我有什么地方做错了吗?你能花几分钟时间帮我一下吗?

非常感谢

我直接在Windows10上运行Python3.5和Spyder 0.12,我使用的是TensorFlow IDE。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-03 07:28:59

看起来你正在用这个字典构造函数重写一个'out‘键:

代码语言:javascript
复制
{**weights, **biases}

例如:

代码语言:javascript
复制
weights = {'h1':1, 'h2':2, 'out':3}
biases = {'b1':4, 'b2':5, 'out':6}
print({**weights, **biases})

{'h2': 2, 'out': 6, 'b2': 5, 'b1': 4, 'h1': 1}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42567704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档