【问题标题】:Error writing TFRecords, Networks reads double the values (Input to reshape is a tensor with n*2 values, but the requested shape has n values)写入 TFRecords 时出错,Networks 读取双倍值(reshape 的输入是具有 n*2 值的张量,但请求的形状具有 n 个值)
【发布时间】:2022-06-16 23:59:19
【问题描述】:

我写这个问题是为了提醒自己,因为我已经知道我会再次重现这个错误,我不想再花半个小时来修复它。

我目前正在做一个机器学习项目,在执行网络的过程中遇到了一个错误: 当我像这样编写 Tfrecords 后执行神经网络时

def write_to_tfrec_spatial(training_directories, path, filename):
  record_file = filename
  n_samples = len(training_directories)
  print()
  print(n_samples)
  with tf.io.TFRecordWriter(record_file) as writer:

    print("writing", end=": ")
    for i in range(n_samples):
      if(i % 50) == 0:
        print()
      print(i, end=",")

      dir = path + training_directories[i]

      loaded = np.load(dir)
      ground = loaded["rad"]

      if normalization:
        ground = ground / max_norm_value
        print(np.amax(ground), end=",")

      padded_ground = np.pad(ground, [(3, 2), (0, 0)], mode='constant')
      inputs = data_augmentation(padded_ground)

      for input in inputs:
        tf_example = image_example_spatial(input=input, ground=padded_ground)
        writer.write(tf_example.SerializeToString())
  return record_file

然后我像这样执行网络:

model.fit(training_dataset, steps_per_epoch=steps, epochs=60, validation_data=validation_dataset, callbacks=my_callbacks)

但我收到以下错误:

2 root error(s) found.
  (0) INVALID_ARGUMENT:  Input to reshape is a tensor with 376832 values, but the requested shape has 188416
     [[{{node Reshape}}]]
     [[IteratorGetNext]]
     [[IteratorGetNext/_428]]
  (1) INVALID_ARGUMENT:  Input to reshape is a tensor with 376832 values, but the requested shape has 188416
     [[{{node Reshape}}]]
     [[IteratorGetNext]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_165085]

我不明白为什么我得到的值恰好是两倍,我检查了多次形状并且它们总是正确的,但是 TFRecord 总是返回错误数量的值

【问题讨论】:

    标签: python tensorflow keras deep-learning neural-network


    【解决方案1】:

    这里的错误是通过操作图像,我间接将 numpy 数组的 dtype 从 np.float32 更改为 np.float64

    通过这样做,我正在编写 tf.float64 张量,然后用这个来读取它们:

    input_raw = tf.io.decode_raw(new_record['input'], out_type=tf.float32, little_endian=True, fixed_length=None, name=None)
    

    并尝试将它们解码为tf.float32

    为了解决这个问题,我不得不在这样的操作之后再次将 numpy 数组转换为 np.float32

    rad = np.float32(rad)
    

    【讨论】:

      猜你喜欢
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      相关资源
      最近更新 更多