tf2_classfiction_deeplearning

import tensorflow as tf
from tensorflow import keras
import matplotlib as mpl
from matplotlib import pyplot as plt
%matplotlib inline
import sklearn
import numpy as np
import pandas as pd
import os
import sys
#Get data:
fashion_mnist = keras.datasets.fashion_mnist                             #download the mnist data
(x_train_all,y_train_all),(x_test,y_test) = fashion_mnist.load_data()    #split data to train and test
x_valid ,x_train = x_train_all[:5000],x_train_all[5000:]                 #splot train data to train and valid
y_valid ,y_train = y_train_all[:5000],y_train_all[5000:]                 #splot train data to train and valid

print(x_valid.shape,y_valid.shape)
print(x_train.shape,y_train.shape)
print(x_test.shape,y_test.shape)
(5000, 28, 28) (5000,)
(55000, 28, 28) (55000,)
(10000, 28, 28) (10000,)
#Normalization或StandarScaler(归一化)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()

#归一化训练集,归一化输入的是float类型的二维数据,所以先转化为二维数据,得到结果之后在把shape换回来
x_train_scaled = scaler.fit_transform(         
    x_train.astype(np.float32).reshape(-1,1)
).reshape(-1,28,28)
#归一化验证集
x_valid_scaled = scaler.transform(
    x_valid.astype(np.float32).reshape(-1,1)
).reshape(-1,28,28)
#归一化测试集
x_test_scaled = scaler.transform(
    x_test.astype(np.float32).reshape(-1,1)
).reshape(-1,28,28)

#built model
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28])) 

for i in range(20):
    model.add(keras.layers.Dense(100,activation="relu"))
    
model.add(keras.layers.Dense(10,activation="softmax"))

model.compile(
    loss = "sparse_categorical_crossentropy",
    optimizer = "sgd",
    metrics = ["accuracy"]
)
#train
history = model.fit(x_train_scaled,y_train,epochs=10,validation_data=[x_valid_scaled,y_valid])
Train on 55000 samples, validate on 5000 samples
Epoch 1/10
55000/55000 [==============================] - 8s 141us/sample - loss: 2.3018 - accuracy: 0.1258 - val_loss: 2.3007 - val_accuracy: 0.1156
Epoch 2/10
55000/55000 [==============================] - 7s 123us/sample - loss: 2.2988 - accuracy: 0.1675 - val_loss: 2.2959 - val_accuracy: 0.1782
Epoch 3/10
55000/55000 [==============================] - 7s 123us/sample - loss: 2.2905 - accuracy: 0.1876 - val_loss: 2.2819 - val_accuracy: 0.1912
Epoch 4/10
55000/55000 [==============================] - 7s 124us/sample - loss: 2.2654 - accuracy: 0.2073 - val_loss: 2.2420 - val_accuracy: 0.2012
Epoch 5/10
55000/55000 [==============================] - 7s 123us/sample - loss: 2.1991 - accuracy: 0.2014 - val_loss: 2.1307 - val_accuracy: 0.1992
Epoch 6/10
55000/55000 [==============================] - 7s 126us/sample - loss: 1.9706 - accuracy: 0.2207 - val_loss: 1.7602 - val_accuracy: 0.2696
Epoch 7/10
55000/55000 [==============================] - 7s 126us/sample - loss: 1.4553 - accuracy: 0.3804 - val_loss: 1.1294 - val_accuracy: 0.4988
Epoch 8/10
55000/55000 [==============================] - 7s 126us/sample - loss: 1.1047 - accuracy: 0.5268 - val_loss: 0.9672 - val_accuracy: 0.5908
Epoch 9/10
55000/55000 [==============================] - 7s 125us/sample - loss: 0.9735 - accuracy: 0.5869 - val_loss: 0.8697 - val_accuracy: 0.6432
Epoch 10/10
55000/55000 [==============================] - 7s 126us/sample - loss: 0.8924 - accuracy: 0.6375 - val_loss: 0.8151 - val_accuracy: 0.6840
# Show learn result
def plot_learning_curves(history):
    pd.DataFrame(history.history).plot(figsize = (8,5))
    plt.grid(True)
    plt.gca().set_ylim(0,3)
    plt.show()
plot_learning_curves(history)

# 1.参数众多,训练不重复
# 2.梯度消失(深度学习中多次参数相乘导致一开始梯度(导数)很小) -> 链式负责 -> 复合函数求导

在这里插入图片描述


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值