TypeError: Expected tensorflow.python.framework.tensor_spec.TensorSpec, found numpy.ndarray
TypeError: Expected tensorflow.python.framework.tensor_spec.TensorSpec, found numpy.ndarray
当我想从 TFF 0.12.0 迁移到 TFF 0.18.0 时出现以下错误,
知道我有一个图像数据集,这是我的 sample_batch
images, labels = next(img_gen.flow_from_directory(path0,target_size=(224, 224), batch_size=2))
sample_batch = (images,labels)
...
def model_fn():
keras_model = create_keras_model()
return tff.learning.from_keras_model(
keras_model,
input_spec=sample_batch,
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
那么我该如何修改我的 sample_batch 以使其与这个版本相符呢?请帮忙 !!谢谢
版本 0.13.0
the sample_batch
parameter was deprecated. The input_spec
parameter must be a tff.Type
or tf.TensorSpec
as per the documentation。
从 numpy.ndarray
构建 tf.TensorSpec
的结构:
def tensor_spec_from_ndarray(a):
return tf.TensorSpec(dtype=tf.dtypes.as_dtype(a.dtype),
shape=a.shape)
sample_batch = (images,labels) # assumes images and labels are np.ndarray
input_spec = tf.nest.map_structure(
tensor_spec_from_ndarray, sample_batch)
当我想从 TFF 0.12.0 迁移到 TFF 0.18.0 时出现以下错误, 知道我有一个图像数据集,这是我的 sample_batch
images, labels = next(img_gen.flow_from_directory(path0,target_size=(224, 224), batch_size=2))
sample_batch = (images,labels)
...
def model_fn():
keras_model = create_keras_model()
return tff.learning.from_keras_model(
keras_model,
input_spec=sample_batch,
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
那么我该如何修改我的 sample_batch 以使其与这个版本相符呢?请帮忙 !!谢谢
版本 0.13.0
the sample_batch
parameter was deprecated. The input_spec
parameter must be a tff.Type
or tf.TensorSpec
as per the documentation。
从 numpy.ndarray
构建 tf.TensorSpec
的结构:
def tensor_spec_from_ndarray(a):
return tf.TensorSpec(dtype=tf.dtypes.as_dtype(a.dtype),
shape=a.shape)
sample_batch = (images,labels) # assumes images and labels are np.ndarray
input_spec = tf.nest.map_structure(
tensor_spec_from_ndarray, sample_batch)