如何转换Tensorflow数据集[EMNIST/balanced]的数据类型(从uint8到float32)
我使用的是Tensorflow数据集“emnist/balanced”。默认情况下,“要素”值的数据类型为uint8。然而,Tensorflow模型只接受浮点值 如何将要素和标签数据类型转换为float32。 代码如下:如何转换Tensorflow数据集[EMNIST/balanced]的数据类型(从uint8到float32),tensorflow,Tensorflow,我使用的是Tensorflow数据集“emnist/balanced”。默认情况下,“要素”值的数据类型为uint8。然而,Tensorflow模型只接受浮点值 如何将要素和标签数据类型转换为float32。 代码如下: #########################################################3 import tensorflow as tf import tensorflow_datasets as tfds datasets, info = tfd
#########################################################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:传递给参数“features”的值的数据类型uint8不在允许值列表中:float16、bfloat16、float32、float64请参考工作代码为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您是否尝试过
emnist\u train=tf.cast(emnist\u train,tf.float32)
是的,我尝试过emnist\u train=tf.cast(emnist\u train,tf.float32)
并得到以下错误ValueError:尝试将值()转换为不支持的类型()一个张量。
事实上,现在我想到了uint8,它看起来应该可以作为某种神经网络的灰度输入图像(这大概就是你的模型);无论如何,是的,TFD给出的是“适配器”而不是张量,这很烦人。我以前没有使用过tfds,所以我退出。有一个.map函数用于元素映射。答案不合适,因为它是关于MNIST和Keras数据集的。然而,提出的问题是关于EMNIST和Tensorflow数据集(tfds)。我使用Kaggle EMNIST数据集作为CSV文件,获得了0.91的训练精度和0.89的测试精度。我想把精确度提高到95+。如果有人能分享一个深度学习模型,该模型的准确率可以达到95%以上。