首页 > 解决方案 > Tensorflow:动态地将值附加到张量

问题描述

我一直在使用 Tensorflow 中的 CNN,并希望在每次训练迭代时跟踪损失值如何在运行时将值连接到张量,并在每次迭代时输出损失值?这是python代码的模拟版本 - 这会引发错误

# mocked up example to mimick 4 iterations
sample=[1,2,3,4]
# this is a dummy function to return loss values
def check(x):
 return x*2
# the code below iteratively feeds loss values to concatenate in the loss_list tensor
def tf_check():
    with tf.device(device):
        loss = tf.placeholder(tf.float32)
        loss_np = check(loss)
        loss_list = tf.placeholder(tf.float32,shape=[])
        
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for t in sample:
            feed_dict = {loss:t}
            y1=sess.run(loss_np,feed_dict)
            loss_list=sess.run(tf.stack([loss_list,y1],axis=0))
tf_check()

标签: pythontensorflowdeep-learning

解决方案


推荐阅读