【发布时间】: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