gpt4 book ai didi

python - 递归使用函数会导致 tensorflow 中模型大小的增加

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:46 25 4
gpt4 key购买 nike

我使用一个函数来计算每个时期tf.Session()内的均方误差。但是,如果我在每一步都保存模型,则保存的文件的大小会随着步长的增加而增加。

当我使用函数get_mse()时,每个纪元后保存的check_points的大小都会增加。但是,当我使用一个简单的语句来计算 mseMSE 时,它不会发生。我做错了什么?

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=....相反。

希望对你有帮助

关于python - 递归使用函数会导致 tensorflow 中模型大小的增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55903132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com