tensorflow2.x中input_signature的说明

本文详细介绍了TensorFlow中@tf.function输入签名的使用方法,包括字典类复核类型、简单数组和多层数组的表示方式。强调了参数结构的注意事项,如属性字段名的小写规则和维度一致性。此外,还展示了多个参数输入的情况。

1、结构为字典类复核类型时的表示方式:

@tf.function(input_signature=({"key1":tf.TensorSpec(shape=(None,),dtype=tf.dtypes.string,name="key1","key2":tf.TensorSpec(shape=(None,),dtype=tf.dtypes.string,name="key2"))},))

表示入参结构如下:

{
 "key1":["a","b"],
 "key2":["e","f"]
}

注意事项:1、key1和key2代表的属性字段名必须小写

                  2、key1和key2对应的维度必须一致

2、输入的结构如果为简单的数组性,或多层数组:

@tf.function(input_signature=(tf.TensorSpec(shape=(None,None), dtype=tf.dtypes.string,name="key1"),))

表示的入参结构如下:

{
"key1":[['a','b'],['c','d']]
}

3、输入参数为多个参数时,

@tf.function(input_signature=(tf.TensorSpec(shape=(None,None), dtype=tf.dtypes.string,name="key1"),tf.TensorSpec(shape=(None,None), dtype=tf.dtypes.string,name="key2")))
{
 "key1":[["a","b"]],
 "key2":[["e","f"]]
}

TypeError Traceback (most recent call last) Cell In[6], line 3 1 import numpy as np 2 import pandas as pd ----> 3 import tensorflow as tf 4 import matplotlib.pyplot as plt 5 from sklearn.preprocessing import StandardScaler File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\__init__.py:37 34 import sys as _sys 35 import typing as _typing ---> 37 from tensorflow.python.tools import module_util as _module_util 38 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader 40 # Make sure code inside the TensorFlow codebase can use tf2.enabled() at import. File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\__init__.py:42 37 from tensorflow.python.eager import context 39 # pylint: enable=wildcard-import 40 41 # Bring in subpackages. ---> 42 from tensorflow.python import data 43 from tensorflow.python import distribute 44 # from tensorflow.python import keras File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\data\__init__.py:21 15 """`tf.data.Dataset` API for input pipelines. 16 17 See [Importing Data](https://tensorflow.org/guide/data) for an overview. 18 """ 20 # pylint: disable=unused-import ---> 21 from tensorflow.python.data import experimental 22 from tensorflow.python.data.ops.dataset_ops import AUTOTUNE 23 from tensorflow.python.data.ops.dataset_ops import Dataset File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\data\experimental\__init__.py:96 15 """Experimental API for building input pipelines. 16 17 This module contains experimental `Dataset` sources and transformations that can (...) 92 @@UNKNOWN_CARDINALITY 93 """ 95 # pylint: disable=unused-import ---> 96 from tensorflow.python.data.experimental import service 97 from tensorflow.python.data.experimental.ops.batching import dense_to_ragged_batch 98 from tensorflow.python.data.experimental.ops.batching import dense_to_sparse_batch File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py:419 1 # Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); (...) 13 # limitations under the License. 14 # ============================================================================== 15 """API for using the tf.data service. 16 17 This module contains: (...) 416 job of ParameterServerStrategy). 417 """ --> 419 from tensorflow.python.data.experimental.ops.data_service_ops import distribute 420 from tensorflow.python.data.experimental.ops.data_service_ops import from_dataset_id 421 from tensorflow.python.data.experimental.ops.data_service_ops import register_dataset File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py:24 22 from tensorflow.python import tf2 23 from tensorflow.python.compat import compat ---> 24 from tensorflow.python.data.experimental.ops import compression_ops 25 from tensorflow.python.data.experimental.service import _pywrap_server_lib 26 from tensorflow.python.data.experimental.service import _pywrap_utils File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py:16 1 # Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); (...) 13 # limitations under the License. 14 # ============================================================================== 15 """Ops for compressing and uncompressing dataset elements.""" ---> 16 from tensorflow.python.data.util import structure 17 from tensorflow.python.ops import gen_experimental_dataset_ops as ged_ops 20 def compress(element): File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\data\util\structure.py:23 20 import six 21 import wrapt ---> 23 from tensorflow.python.data.util import nest 24 from tensorflow.python.framework import composite_tensor 25 from tensorflow.python.framework import ops File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\data\util\nest.py:36 16 """## Functions for working with arbitrarily nested sequences of elements. 17 18 NOTE(mrry): This fork of the `tensorflow.python.util.nest` module (...) 31 arrays. 32 """ 34 import six as _six ---> 36 from tensorflow.python.framework import sparse_tensor as _sparse_tensor 37 from tensorflow.python.util import _pywrap_utils 38 from tensorflow.python.util import nest File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\framework\sparse_tensor.py:24 22 from tensorflow.python import tf2 23 from tensorflow.python.framework import composite_tensor ---> 24 from tensorflow.python.framework import constant_op 25 from tensorflow.python.framework import dtypes 26 from tensorflow.python.framework import ops File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\framework\constant_op.py:25 23 from tensorflow.core.framework import types_pb2 24 from tensorflow.python.eager import context ---> 25 from tensorflow.python.eager import execute 26 from tensorflow.python.framework import dtypes 27 from tensorflow.python.framework import op_callbacks File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\eager\execute.py:23 21 from tensorflow.python import pywrap_tfe 22 from tensorflow.python.eager import core ---> 23 from tensorflow.python.framework import dtypes 24 from tensorflow.python.framework import ops 25 from tensorflow.python.framework import tensor_shape File ~\anaconda3\envs\py39\lib\site-packages\tensorflow\python\framework\dtypes.py:34 31 from tensorflow.python.types import trace 32 from tensorflow.core.function import trace_type ---> 34 _np_bfloat16 = _pywrap_bfloat16.TF_bfloat16_type() 37 class DTypeMeta(type(_dtypes.DType), abc.ABCMeta): 38 pass TypeError: Unable to convert function return value to a Python type! The signature was () -> handle
10-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会发paper的学渣

您的鼓励和将是我前进的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值