Tensorflow#fashion数据集class类实现#Fashion数据集下载失败

 Tensorflow学习,Fashion数据集,包含7万张图片用于训练和测试,其中6万张用于训练,1万张用于测试。

##注意:

代码在运行时加载数据集可能会从外网下载,可能会导致下载失败中断运行。

给出两个方法:

1、直接从官网下载,附上教程链接:https://www.tensorflow.org/tutorials/keras/classification

2、条件允许的可以直接开启科学上网,就可以下载成功啦

最后附上完整代码:

import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras import Model

fashion = tf.keras.datasets.fashion_mnist
(x_train, y_train), (x_test, y_test) = fashion.load_data()

class FashionModel(Model):
    def __init__(self):
        super(FashionModel, self).__init__()
        self.flatten = Flatten()
        self.d1 = Dense(128, activation='relu')
        self.d2 = Dense(10, activation='softmax')

    def call(self, x, training=None, mask=None):
        x = self.flatten(x)
        x = self.d1(x)
        y = self.d2(x)
        return y

model = FashionModel()

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy']
              )

model.fit(x_train, y_train, batch_size=32, epochs=20, validation_data=(x_test, y_test), validation_freq=1)

model.summary()

运行成功:

Epoch 18/20
1875/1875 [==============================] - 6s 3ms/step - loss: 0.4907 - sparse_categorical_accuracy: 0.8283 - val_loss: 0.5619 - val_sparse_categorical_accuracy: 0.8230
Epoch 19/20
1875/1875 [==============================] - 6s 3ms/step - loss: 0.4904 - sparse_categorical_accuracy: 0.8289 - val_loss: 0.5419 - val_sparse_categorical_accuracy: 0.8139
Epoch 20/20
1875/1875 [==============================] - 6s 3ms/step - loss: 0.4721 - sparse_categorical_accuracy: 0.8324 - val_loss: 0.6536 - val_sparse_categorical_accuracy: 0.7796
Model: "fashion_model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten (Flatten)            multiple                  0         
_________________________________________________________________
dense (Dense)                multiple                  100480    
_________________________________________________________________
dense_1 (Dense)              multiple                  1290      
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________

进程已结束,退出代码0

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值