【问题标题】:TypeError: object of type 'Tensor' has no len()TypeError:“张量”类型的对象没有 len()
【发布时间】:2021-10-24 14:17:29
【问题描述】:

您好,我正在尝试获取文件的长度。我正在使用 tf 版本 1.14.0 和 keras 2.2.4。但是下面的代码是用 2.6 版本编写的。出现错误:

TypeError: object of type 'Tensor' has no len()

谁能帮忙修改tf==1.14.0的代码?

我的数据集是大约 3000 个文件的音频文件。

**filenames = tf.io.gfile.glob(str(data_dir) + '/*/*')
filenames = tf.random.shuffle(filenames)
num_samples = len(filenames)
print('Number of total examples:', num_samples)
print('Number of examples per label:',
      len(tf.io.gfile.listdir(str(data_dir/commands[0]))))
print('Example file tensor:', filenames[0])**

【问题讨论】:

  • 您在代码中使用了两次len()。你能说出错误来自哪里吗?如果能提供完整的错误回溯就更好了。
  • 嗨,感谢您的回复,我正在尝试从此链接运行代码tensorflow.org/tutorials/audio/simple_audio tensorflow version-1.14.0 和 keras 2.2.4
  • 当我打印样本数时,它应该返回“3000”,但现在出现错误,因为我将 tf 和 keras 版本分别更改为 1.14.0 和 2.2.4。
  • @shivashankari 代替 len(filenames) 使用 filenames.shape[0]tf.shape(filenames)[0] 以及第二个镜头。

标签: python tensorflow model speech-recognition attributeerror


【解决方案1】:

len() 可能不支持 Tensorflow 版本 1.14.0 的张量。请改用.shape

num_samples = filenames.shape[0].

【讨论】:

  • 在同一个程序中出现错误“AttributeError: 'Tensor' object has no attribute 'to_sparse'”当我运行下面的代码时。**AUTOTUNE = tf.data.experimental.AUTOTUNE files_ds = tf .data.Dataset.from_tensor_slices(train_files)waveform_ds = files_ds.map(get_waveform_and_label, num_parallel_calls=AUTOTUNE)**
  • @shivashankari 在尝试在另一个 Tensorflow 版本中运行适用于一个 Tensorflow 版本的代码时,您会遇到多个错误,因为每个版本都会进行大量重大更改,有时会删除或更新整个模块和功能。我建议您在设计用于避免进一步错误的 Tensorflow 版本中运行代码。
猜你喜欢
  • 1970-01-01
  • 2015-08-21
  • 2019-05-23
  • 2013-04-18
  • 2015-01-21
  • 2021-12-19
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多