如何只保存产生误差最小的权值(以十为单位)

2024-10-01 17:32:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python3.5和tensorflow1.4创建不同类型的神经网络。我使用成本函数来训练/优化这些网络。在培训期间,所产生的成本可能会在一段时间内保持在一个水平上,甚至会上升,但以后可能会下降。所以可能的问题是,在我的训练结束时(由固定数量的历元给出),我的模型可能会返回比训练期间的其他点更糟糕的结果。你知道吗

为了总是在训练结束时获得最为人所知的模型权重,我当前在训练期间将变量(tf.Saver)保存到一个文件中,只要成本低于之前的最低成本。你知道吗

不幸的是,这大大减慢了培训过程。有没有办法将所有模型变量保存到一个python变量中,而不是保存到磁盘并在以后恢复它们?我试着用tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)来做这个,但是我不知道如何在我的张量流模型中得到它们。你知道吗

提前谢谢!你知道吗



更新:

我找到了解决办法的一部分。我没有将权重保存为python变量,而是在tensorflow图中创建备份变量。就我所知,这有一个缺点:由于我实现了许多不同的图,我必须调整我的解决方案以适应每一个图。下面是多层感知器的解决方案。但我不知道如何用tf.contrib.rnn.BasicLSTMCell来做这个例子。所以我更希望有一个解决方案,只保存所有变量,让我以后恢复它们。你知道吗

多层感知器的电流解

# Prepare inputData
[...]

# Placeholder for tensorflow input
X = tf.placeholder("float", [None, len(netInputVariables)])
Y = tf.placeholder("float", [None, len(netOutputVariables)])

# Weights and Biases are stored in a dictionary
Weights = {
    'hidden1': tf.Variable(tf.random_normal([len(netInputVariables), 20])),
    'output': tf.Variable(tf.random_normal([20, len(netOutputVariables)]))
}

Biases = {
    'hidden1': tf.Variable(tf.random_normal([20])),
    'output': tf.Variable(tf.random_normal([len(netOutputVariables)]))
}

# Building the graph
hiddenLayer1 = tf.tanh(tf.add(tf.matmul(x, Weights['hidden1']), Biases['hidden1']))
yOut= tf.matmul(hiddenLayer1, Weights['output']) + Biases['output']

# Mean Square Error
CostFunction = tf.reduce_mean(tf.square(Y - YOut))

# Training Operation
Optimizer = tf.train.AdadeltaOptimizer(learning_rate=0.1)
TrainingFunction = Optimizer.minimize(CostFunction)

# Save optimal weights
WeightsBackup = {
    'hidden1': tf.Variable(tf.zeros([len(netInputVariables), 20])),
    'output': tf.Variable(tf.zeros([20, len(netOutputVariables)]))
}
BiasesBackup = {
    'hidden1': tf.Variable(tf.zeros([20])),
    'output': tf.Variable(tf.zeros([len(netOutputVariables)]))
}

newCostLower = [WeightsBackup[key].assign(Weights[key]) for key in Weights] + [BiasesBackup[key].assign(Biases[key]) for key in Biases]
BackupWeights = tf.group(*newCostLower)

# Restore Operation
loadOptimalWeights = [Weights[key].assign(WeightsBackup[key]) for key in Weights] + [Biases[key].assign(BiasesBackup[key]) for key in Biases]
LoadOptimalWeights = tf.group(*loadOptimalWeights)

# Training
with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    lowestCosts = 100

    for epoch in range(10000):
        _, costs = session.run([TrainingFunction, CostFunction], feed_dict={X: xData, Y: yData})

        # Save weights if costs have decreased
        if costs < lowestCosts:
            lowestCosts = costs
            session.run(BackupWeights)

# Restore bests weights at the end of training
session.run(LoadOptimalWeights)

# Do some kind of evaluation
[...]

Tags: keyin模型foroutputlensessiontf

热门问题