【问题标题】:TypeError: Expected tensorflow.python.framework.tensor_spec.TensorSpec, found numpy.ndarrayTypeError: Expected tensorflow.python.framework.tensor_spec.TensorSpec, found numpy.ndarray
【发布时间】:2021-02-15 10:12:18
【问题描述】:

当我想从 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 以使其与此版本正确?请帮忙 !!谢谢

【问题讨论】:

    标签: tensorflow-federated


    【解决方案1】:

    在版本 0.13.0 中,sample_batch 参数已被弃用。根据the documentationinput_spec 参数必须是 tff.Typetf.TensorSpec

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-10
      • 2019-07-18
      • 2021-01-22
      • 2021-02-07
      • 2018-12-25
      • 1970-01-01
      • 2022-12-28
      • 1970-01-01
      相关资源
      最近更新 更多