环境配置我就不再这赘述了
1利用kares搭建分类模型
import tensorflow as tf
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
from tensorlow import keras
print(tf.__version__)
print(sys.version_info)
for module in mpl,np,pd,sklearn,tf,keras:
print(module.__name__, module.__version__)
%%加载训练集和测试集
fashion_mnist = keras.datasets.fashion_mnist
(x_train_all,y_train_all),(x_test,y_test) = fashion_mnist.load_data()
x_vaild,x_train = x_train_all[:5000],x_train_all[5000:]
y_vaild,y_train = y_train_all[:5000],y_train_all[5000:]
print(x_vaild.shape,y_vaild.shape)
print(x_train.shape,y_train.shape)
print(x_test.shape,y_test.shape)
def show_single_iamge(image_arr):
plt.imshow(img_arr,cmap ="binary")
plt.show()
show_single_image(x_train[0])
model = keras.model.Sequential()
modle.add(keras.layers.Flatten(input_shape[28,28]))
model.add(keras.layers.dense(300,activation="relu"))
model.add(keras.layers.dense(100,activation="relu"))
model.add(keras.layers.dense(10,activetion="softmax"))
model.compile(loss ="sparse_categorical_crossentropy",optimizer ="sgd",metrics = ["accuracy"])
%查看层
model.layers
model.summary
1.1激活函数:
relu:
y=max(1,x)
softmax:
将向量变成概率分布。
x=[x1,x2,x3]
y=[e^x1/sum,e^x2/sum,[e^x3/sum] ; (sum = e^x1+e^x2+e^x3)
1.2全连接层参数计算
假设输入为x:[28,28]=784
x*w+b=>w.shape = [784,300], b.shape = [300]
总的参数数量 = w.shape + b.shape