【发布时间】:2020-03-15 13:03:34
【问题描述】:
我正在使用 Tensorflow 数据集“emnist/balanced”。 features 值的数据类型默认为 uint8。但是,Tensorflow 模型只接受浮点值。
如何将特征和标签数据类型转换为 float32。
代码在这里:
#########################################################3
import tensorflow as tf
import tensorflow_datasets as tfds
datasets, info = tfds.load(name="emnist/balanced", with_info=True, as_supervised=True)
emnist_train, emnist_test = datasets['train'], datasets['test']
.
.
.
.
.
.
history = model.fit(emnist_train, epochs = 10)
#validation
test_loss, test_acc = model.evaluate(emnist_test, verbose=2)
print(test_acc)
Error --
2
3
----> 4 history = model.fit(emnist_train, epochs = 10)
5
6 #validation
TypeError: Value passed to parameter 'features' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64
TypeError:传递给参数“功能”的值的 DataType uint8 不在允许值列表中:float16、bfloat16、float32、float64
【问题讨论】:
-
你试过
emnist_train = tf.cast(emnist_train, tf.float32) -
是的,我试过
emnist_train = tf.cast(emnist_train, tf.float32)并得到以下错误ValueError: Attempt to convert a value (<DatasetV1Adapter shapes: ((28, 28, 1), ()), types: (tf.uint8, tf.int64)>) with an unsupported type (<class 'tensorflow.python.data.ops.dataset_ops.DatasetV1Adapter'>) to a Tensor. -
实际上现在我认为 uint8 似乎应该可以作为灰度输入图像进入某种神经网络(这可能是您的模型);无论如何,是的,tfds 提供的是“适配器”而不是张量,这很烦人。我以前没有使用过 tfds,所以我会退出。
-
有一个.map函数可以申请元素映射。
标签: tensorflow