使用八股搭建神经网络
- 神经网络搭建八股
- iris代码复现
- MNIST数据集
- 训练MNIST数据集
- Fashion数据集
用Tensorflow API:tf.keras搭建网络八股六步法
import
train,test
model=tf.keras.models.Sequential
model.compile
model.fit
model.summary
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(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)
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=5, validation_split=0.2, validation_freq=20)
model.summary()
Epoch 1/5
WARNING:tensorflow:Layer dense_10 is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because it's dtype defaults to floatx.
If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.
To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.
4/4 [==============================] - 0s 3ms/step - loss: 2.3173 - sparse_categorical_accuracy: 0.3417
Epoch 2/5
4/4 [==============================] - 0s 3ms/step - loss: 1.0797 - sparse_categorical_accuracy: 0.6167
Epoch 3/5
4/4 [==============================] - 0s 3ms/step - loss: 0.8429 - sparse_categorical_accuracy: 0.6250
Epoch 4/5
4/4 [==============================] - 0s 4ms/step - loss: 0.7448 - sparse_categorical_accuracy: 0.6667
Epoch 5/5
4/4 [==============================] - 0s 3ms/step - loss: 0.9843 - sparse_categorical_accuracy: 0.6417
Model: "sequential_5"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_10 (Dense) multiple 15
=================================================================
Total params: 15
Trainable params: 15
Non-trainable params: 0
_________________________________________________________________
用类实现鸢尾花分类的代码
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras import Model
from sklearn import datasets
import numpy as np
x_train = datasets.load_iris().data
y_train = datasets.load_iris().target
np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)
class IrisModel(Model):
def __init__(self):
super(IrisModel, self).__init__()
self.d1 = Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
def call(self, x):
y = self.d1(x)
return y
model = IrisModel()
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=10, validation_split=0.2, validation_freq=20)
model.summary()
MNIST数据集
提供6万张2828像素点的0-9手写数字图片和标签,用于训练,提供1万张2828像素点的0-9手写数字图片和标签,用于测试
导入MNIST数据集
mnist=tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test)=mnist.load_data()
作为输入特征,输入神经网络时,将数据拉伸为一维数组tf.keras.layers.Flatten()
import tensorflow as tf
from matplotlib import pyplot as plt
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 可视化训练集输入特征的第一个元素
plt.imshow(x_train[0], cmap='gray') # 绘制灰度图
plt.show()
# 打印出训练集输入特征的第一个元素
print("x_train[0]:\n", x_train[0])
# 打印出训练集标签的第一个元素
print("y_train[0]:\n", y_train[0])
# 打印出整个训练集输入特征形状
print("x_train.shape:\n", x_train.shape)
# 打印出整个训练集标签的形状
print("y_train.shape:\n", y_train.shape)
# 打印出整个测试集输入特征的形状
print("x_test.shape:\n", x_test.shape)
# 打印出整个测试集标签的形状
print("y_test.shape:\n", y_test.shape)
x_train[0]:
[[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 3 18 18 18 126 136
175 26 166 255 247 127 0 0 0 0]
[ 0 0 0 0 0 0 0 0 30 36 94 154 170 253 253 253 253 253
225 172 253 242 195 64 0 0 0 0]
[ 0 0 0 0 0 0 0 49 238 253 253 253 253 253 253 253 253 251
93 82 82 56 39 0 0 0 0 0]
[ 0 0 0 0 0 0 0 18 219 253 253 253 253 253 198 182 247 241
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 80 156 107 253 253 205 11 0 43 154
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 14 1 154 253 90 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 139 253 190 2 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 11 190 253 70 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 35 241 225 160 108 1
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 81 240 253 253 119
25 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 186 253 253
150 27 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 93 252
253 187 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 249
253 249 64 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 130 183 253
253 207 2 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 39 148 229 253 253 253
250 182 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 24 114 221 253 253 253 253 201
78 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 23 66 213 253 253 253 253 198 81 2
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 18 171 219 253 253 253 253 195 80 9 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 55 172 226 253 253 253 253 244 133 11 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 136 253 253 253 212 135 132 16 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]]
y_train[0]:
5
x_train.shape:
(60000, 28, 28)
y_train.shape:
(60000,)
x_test.shape:
(10000, 28, 28)
y_test.shape:
(10000,)
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#对输入网络的输入特征进行归一化,使原本0到255之间的灰度值变为0到1之间的数值,把输入特征的数值变小更适合神经网络吸收
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
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=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()
Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2510 - sparse_categorical_accuracy: 0.9296 - val_loss: 0.1341 - val_sparse_categorical_accuracy: 0.9593
Epoch 2/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.1119 - sparse_categorical_accuracy: 0.9668 - val_loss: 0.1031 - val_sparse_categorical_accuracy: 0.9704
Epoch 3/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0763 - sparse_categorical_accuracy: 0.9775 - val_loss: 0.0847 - val_sparse_categorical_accuracy: 0.9747
Epoch 4/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0577 - sparse_categorical_accuracy: 0.9833 - val_loss: 0.0752 - val_sparse_categorical_accuracy: 0.9774
Epoch 5/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0438 - sparse_categorical_accuracy: 0.9869 - val_loss: 0.0733 - val_sparse_categorical_accuracy: 0.9775
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) multiple 0
_________________________________________________________________
dense_6 (Dense) multiple 100480
_________________________________________________________________
dense_7 (Dense) multiple 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
用类实现手写数字识别代码
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras import Model
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
class MnistModel(Model):
def __init__(self):
super(MnistModel, self).__init__()
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10, activation='softmax')
def call(self, x):
x = self.flatten(x)
x = self.d1(x)
y = self.d2(x)
return y
model = MnistModel()
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=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()
Epoch 1/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.2598 - sparse_categorical_accuracy: 0.9267 - val_loss: 0.1416 - val_sparse_categorical_accuracy: 0.9556
Epoch 2/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.1129 - sparse_categorical_accuracy: 0.9668 - val_loss: 0.1011 - val_sparse_categorical_accuracy: 0.9708
Epoch 3/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0757 - sparse_categorical_accuracy: 0.9775 - val_loss: 0.0869 - val_sparse_categorical_accuracy: 0.9729
Epoch 4/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.0575 - sparse_categorical_accuracy: 0.9825 - val_loss: 0.0736 - val_sparse_categorical_accuracy: 0.9766
Epoch 5/5
1875/1875 [==============================] - 5s 3ms/step - loss: 0.0439 - sparse_categorical_accuracy: 0.9865 - val_loss: 0.0740 - val_sparse_categorical_accuracy: 0.9764
Model: "mnist_model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_1 (Flatten) multiple 0
_________________________________________________________________
dense_8 (Dense) multiple 100480
_________________________________________________________________
dense_9 (Dense) multiple 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
FASHION数据集
提供6万张2828像素点的衣裤等图片和标签,用于训练,提供1万张2828像素点的衣裤等图片和标签,用于测试
导入FASHION数据集
fashion=tf.keras.datasets.fashion.fashion_mnist
(x_train,y_train),(x_test,y_test)=fashion.load_data()
import tensorflow as tf
fashion = tf.keras.datasets.fashion_mnist
(x_train, y_train),(x_test, y_test) = fashion.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
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=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()
Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2620 - sparse_categorical_accuracy: 0.9252 - val_loss: 0.1395 - val_sparse_categorical_accuracy: 0.9586
Epoch 2/5
1875/1875 [==============================] - 5s 3ms/step - loss: 0.1145 - sparse_categorical_accuracy: 0.9655 - val_loss: 0.1017 - val_sparse_categorical_accuracy: 0.9686
Epoch 3/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0789 - sparse_categorical_accuracy: 0.9756 - val_loss: 0.0912 - val_sparse_categorical_accuracy: 0.9722
Epoch 4/5
1875/1875 [==============================] - 5s 3ms/step - loss: 0.0578 - sparse_categorical_accuracy: 0.9822 - val_loss: 0.0758 - val_sparse_categorical_accuracy: 0.9773
Epoch 5/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.0449 - sparse_categorical_accuracy: 0.9855 - val_loss: 0.0731 - val_sparse_categorical_accuracy: 0.9792
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
_________________________________________________________________
用类实现衣服识别代码
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()
x_train, x_test = x_train / 255.0, x_test / 255.0
class MnistModel(Model):
def __init__(self):
super(MnistModel, self).__init__()
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10, activation='softmax')
def call(self, x):
x = self.flatten(x)
x = self.d1(x)
y = self.d2(x)
return y
model = MnistModel()
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=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()
Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2569 - sparse_categorical_accuracy: 0.9255 - val_loss: 0.1315 - val_sparse_categorical_accuracy: 0.9625
Epoch 2/5
1875/1875 [==============================] - 5s 3ms/step - loss: 0.1122 - sparse_categorical_accuracy: 0.9666 - val_loss: 0.0980 - val_sparse_categorical_accuracy: 0.9711
Epoch 3/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.0774 - sparse_categorical_accuracy: 0.9758 - val_loss: 0.0794 - val_sparse_categorical_accuracy: 0.9766
Epoch 4/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0583 - sparse_categorical_accuracy: 0.9822 - val_loss: 0.0785 - val_sparse_categorical_accuracy: 0.9756
Epoch 5/5
1875/1875 [==============================] - 5s 3ms/step - loss: 0.0448 - sparse_categorical_accuracy: 0.9860 - val_loss: 0.0772 - val_sparse_categorical_accuracy: 0.9771
Model: "mnist_model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_1 (Flatten) multiple 0
_________________________________________________________________
dense_2 (Dense) multiple 100480
_________________________________________________________________
dense_3 (Dense) multiple 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________