import tensorflow as tf
from tensorflow.keras import datasets, layers, optimizers, Sequential
def preprocess(x, y):
x = tf.cast(x, dtype=tf.float32) / 255.
y = tf.cast(y, dtype=tf.int32)
return x, y
batch_size = 128 # 因为数据量较小,所以选用的batch也较小
(x, y), (x_test, y_test) = datasets.fashion_mnist.load_data()
# print(tf.unique(y))
db = tf.data.Dataset.from_tensor_slices((x, y))
db = db.map(preprocess).shuffle(10000).batch(batch_size)
db_test = tf.data.Dataset.from_tensor_slices((x_test, y_test))
db_test = db_test.map(preprocess).batch(batch_size)
''' 查看一下打包后的数据的情况 '''
db_iter = iter(db)
sample = next(db_iter)
print('sample:', sample[0].shape, sample[1].shape)
model = Sequential([
layers.Dense(256, activation=tf.nn.relu), # [b, 784] => [b, 256]
layers.Dense(128, activation=tf.nn.relu), # [b, 256] => [b, 128]
layers.Dense(64, activation=t
利用tensorflow深度学习框架做fashionmnist的神经网络分类
最新推荐文章于 2024-05-17 05:30:45 发布
本文探讨了使用TensorFlow框架构建神经网络对FashionMNIST数据集进行分类的问题。在定义损失函数时,作者遇到了使用交叉熵(crossentropy)时无法求梯度的困扰,但在将from_logits参数设置为False后问题得到解决,但原因未明。
摘要由CSDN通过智能技术生成