i使用一个函数来计算每个时期内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)
每次调用get_mse((时,您都会构建另一个计算图节点。IE。每当您创建另一个版本的predictions
,labels
和losses
。
调用get_mse((一次,然后将结果存储到变量中,例如 mse_computer
。然后使用sess.run(mse_computer, feed_dict=....
。
希望它有帮助
您的代码应该如下:
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)
在您的情况下,在每次迭代中,您都会在图形上添加其他操作和张量,因此它的尺寸会增长。