繁体   English   中英

Tensorflow 2.0:尝试打印最后一个结果时,tf.function 中不支持无值错误

[英]Tensorflow 2.0 : None values are not supported error in tf.function while trying to print last result

我正在尝试在 tf.function 中打印最后一批的结果

import tensorflow as tf

def small_data():
    for i in range(10):
        yield 3, 2

data = tf.data.Dataset.from_generator(
    small_data, (tf.int32, tf.int32), )

def result(data):
    """
    Psuedo code for a model which outputs multiple layer outputs
    :param data:
    :return:
    """
    return tf.random.normal(shape=[1, 2]), tf.random.normal(shape=[1, 2]),data[0]

@tf.function
def train(dataset):
    batch_result = None
    for batch in dataset:
        batch_result = result(data)
    tf.print("Final batch result is", batch_result)


train(dataset=data)


错误

 raise ValueError("None values not supported.")

    ValueError: None values not supported.

结果函数实际上是一个 Keras 模型,它导致不同形状的层输出。 如果我删除batch_result=None分配并将 tf.print 移动到循环内,它会为每个批次打印。 我只想打印最后一批的结果。 另外,我不确定馈入循环的记录数。 我也尝试了多种变体,但没有任何效果。 我如何在 tensorflow 2.0 中实现这一点。

您必须模仿batch_result 的预期形式。 这有效:

@tf.function
def train(dataset):
    batch_result = result(dataset.take(1))
    for batch in dataset:
        batch_result = result(data)
    tf.print("Final batch result is", batch_result)

有点hackish,但这可能有效:

@tf.function
def train(dataset):
    batch_result = result(next(dataset.__iter__()))
    for batch in dataset:
        batch_result = result(data)
    tf.print("Final batch result is", batch_result)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM