鸢尾花分类
import tensorflow as tf
from sklearn import datasets
import numpy as np
#数据获取
x_train = datasets.load_iris().data
y_train = datasets.load_iris().target
np.random.seed(1024)
np.random.shuffle(x_train)
np.random.seed(1024)
np.random.shuffle(y_train)
np.random.seed(1024)
#网络搭建
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(3,activation='softmax',kernel_regularizer=tf.keras.regularizers.l2())
])
#训练参数设置
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy']
)
#训练
model.fit(x_train,y_train,batch_size=32,epochs=500,validation_split=0.2,validation_freq=20)
#网络结构和参数显示
model.summary()
运行结果
curacy: 0.8667 - val_loss: 0.2987 - val_sparse_categorical_accuracy: 1.0000
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) multiple 15
=================================================================
Total params: 15
Trainable params: 15
Non-trainable params: 0
_________________________________________________________________
手写数字识别
import tensorflow as tf
#获取数据集
mnist = tf.keras.datasets.mnist
(x_train ,y_train),(x_test,y_test) = mnist.load_data()
x_train = tf.cast(x_train,dtype=tf.float32)
y_train = tf.cast(y_train,dtype=tf.float32)
x_test = tf.cast(x_test,dtype=tf.float32)
y_test = tf.cast(y_test,dtype=tf.float32)
#网络结构搭建
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation='relu',kernel_regularizer=tf.keras.regularizers.l2()),
tf.keras.layers.Dense(10,activation='softmax',kernel_regularizer=tf.keras.regularizers.l2())
])
#训练参数设置
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy']
)
#设置数据集和训练集进行训练
model.fit(x_train,y_train,batch_size=32,epochs=50,validation_data=(x_test,y_test),validation_freq=1)
#打印网络结构和参数信息
model.summary()
运行结果
tegorical_accuracy: 0.9393
Model: "sequential"
_________________________________________________________________
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
________________________________________________________________
FASHION服装分类
import tensorflow as tf
#导入数据集
fashion = tf.keras.datasets.fashion_mnist
(x_train,y_train),(x_test,y_test) = fashion.load_data()
x_train = tf.cast(x_train,dtype=tf.float32)
y_train = tf.cast(y_train,dtype=tf.float32)
x_test = tf.cast(x_test,dtype=tf.float32)
y_test = tf.cast(y_test,dtype=tf.float32)
#网络结构搭建
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(300,activation='relu',kernel_regularizer=tf.keras.regularizers.l2()),
tf.keras.layers.Dense(100,activation='relu',kernel_regularizer=tf.keras.regularizers.l2()),
tf.keras.layers.Dense(20,activation='relu',kernel_regularizer=tf.keras.regularizers.l2()),
tf.keras.layers.Dense(10,activation='softmax',kernel_regularizer=tf.keras.regularizers.l2())
])
#训练参数设置
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy']
)
#设置数据集和训练集进行训练
model.fit(x_train,y_train,batch_size=32,epochs=50,validation_data=(x_test,y_test),validation_freq=1)
#打印网络结构和参数信息
model.summary()
运行结果
categorical_accuracy: 0.8264
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) multiple 0
_________________________________________________________________
dense (Dense) multiple 235500
_________________________________________________________________
dense_1 (Dense) multiple 30100
_________________________________________________________________
dense_2 (Dense) multiple 2020
_________________________________________________________________
dense_3 (Dense) multiple 210
=================================================================
Total params: 267,830
Trainable params: 267,830
Non-trainable params: 0
_________________________________________________________________
精确率感人,提升层数带来的精度提升有限,只用全连接不太行