当我想从 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
sample_batch
参数已弃用。 input_spec
参数必须是 tff.Type
或 tf.TensorSpec
根据 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)
关于tensorflow-federated - TypeError : Expected tensorflow. python.framework.tensor_spec.TensorSpec,发现numpy.ndarray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66206118/