我使用一个函数来计算每个时期tf.Session()
内的均方误差。但是,如果我在每一步都保存模型,则保存文件的大小会随着步长的增加而增加。
当我使用函数get_mse()
时,每个时期之后保存的check_points
的大小会增加。但是,当我使用简单的语句来计算mse
即MSE
时,却不会发生。我在做什么错了?
import os
import numpy as np
import tensorflow as tf
from tensorflow.python.ops.losses.losses_impl import Reduction, compute_weighted_loss
from tensorflow.python.framework import ops
from tensorflow.python.ops import math_ops
def get_mse(
labels, predictions, weights=1.0, name="mse", scope=None,
loss_collection=ops.GraphKeys.LOSSES,
reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
with ops.name_scope(scope, name,
(predictions, labels, weights)) as scope:
predictions = math_ops.to_float(predictions)
labels = math_ops.to_float(labels)
predictions.get_shape().assert_is_compatible_with(labels.get_shape())
losses = math_ops.squared_difference(predictions, labels)
return compute_weighted_loss(
losses, weights, scope, loss_collection, reduction=reduction)
true = np.random.random((100,1))
pred = np.random.random((100,1))
variable_to_save = tf.Variable(true)
true_ph = tf.placeholder(tf.float32, shape = [None, 1], name='labels')
pred_ph = tf.placeholder(tf.float32, shape = [None, 1], name='predictions')
MSE = tf.reduce_mean(tf.square(pred_ph - true_ph), name='get_mse')
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
saver = tf.train.Saver(max_to_keep=100)
for epoch in range(10):
mse = sess.run(get_mse(true_ph, pred_ph), feed_dict={'labels:0': true, 'predictions:0':pred}) #this increases size of checkpoints after each epoch
#mse = sess.run(MSE, feed_dict={'labels:0': true, 'predictions:0': pred}) #running this does not increases size of check_points
saver.save(sess, os.getcwd(), global_step=epoch)
答案 0 :(得分:0)
每次调用get_mse()时,都会构建另一堆计算图节点。即每次您创建predictions
,labels
和losses
的另一个版本时。
调用一次get_mse()并将结果存储到一个变量中,例如mse_computer
。然后改用sess.run(mse_computer, feed_dict=....
。
希望有帮助
答案 1 :(得分:0)
您的代码应如下所示:
import os
import numpy as np
import tensorflow as tf
from tensorflow.python.ops.losses.losses_impl import Reduction, compute_weighted_loss
from tensorflow.python.framework import ops
from tensorflow.python.ops import math_ops
def get_mse(
labels, predictions, weights=1.0, name="mse", scope=None,
loss_collection=ops.GraphKeys.LOSSES,
reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
with ops.name_scope(scope, name,
(predictions, labels, weights)) as scope:
predictions.get_shape().assert_is_compatible_with(labels.get_shape())
losses = math_ops.squared_difference(predictions, labels)
return compute_weighted_loss(
losses, weights, scope, loss_collection, reduction=reduction)
true = np.random.random(size=(100,1))
pred = np.random.random(size=(100,1))
true_ph = tf.placeholder(tf.float32, shape=[None, 1], name='labels')
pred_ph = tf.placeholder(tf.float32, shape=[None, 1], name='predictions')
mse_tensor = get_mse(true_ph, pred_ph)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
saver = tf.train.Saver(max_to_keep=100)
for epoch in range(10):
mse_val = sess.run(mse_tensor, feed_dict={true_ph: true, pred_ph:pred}) #this increases size of checkpoints after each epoch
saver.save(sess, os.getcwd(), global_step=epoch)
对于您的情况,在每次迭代中,您都需要为图形添加其他操作和张量,因此其大小会增大。