【问题标题】:How do convert data type of Tensorflow Dataset [EMNIST/balanced] (From uint8 to float32)如何转换 Tensorflow 数据集的数据类型 [EMNIST/balanced](从 uint8 到 float32)
【发布时间】: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


【解决方案1】:

请参考工作代码为 MNIST 数据集训练 ANN

try:
  # %tensorflow_version only exists in Colab.
  %tensorflow_version 2.x
except Exception:
  pass
from __future__ import absolute_import, division, print_function, unicode_literals
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print("T/F Version:",tf.__version__)
#### Import the Fashion MNIST dataset
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
##Scale these values to a range of 0 to 1 before feeding them to the neural network model
train_images = train_images / 255.0
test_images = test_images / 255.0
###Build the model
##the neural network requires configuring the layers of the model
##Set up the layers
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10)
])
###Compile the model
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
###Train the model
##Feed the model
model.fit(train_images, train_labels, epochs=10)
###Evaluate accuracy
##compare how the model performs on the test dataset
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print('\nTest accuracy:', test_acc)

输出:

T/F 版本:2.1.0

训练精度:91.06

测试精度:0.8871

【讨论】:

  • 答案不适合,因为它是关于 MNIST 和 Keras 数据集的。然而,提出的问题是关于 EMNIST 和 Tensorflow 数据集 (tfds)。我使用 Kaggle EMNIST 数据集作为 CSV 文件,得到了 0.91 的训练准确度和 0.89 的测试准确度。我想将准确率提高到 95+。如果有人可以分享一个可以达到 95% 以上准确率的深度学习模型。
猜你喜欢
  • 2020-04-25
  • 1970-01-01
  • 2021-01-22
  • 2020-09-02
  • 2019-12-28
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
  • 2023-03-06
相关资源
最近更新 更多