1、人工智能实践:Tensorflow笔记曹健北京大学软件与微电子学院神经网络搭建八股iris代码复现MNIST数据集训练MNIST数据集Fashion数据集本讲目标:使用八股搭建神经网络2用Tensorflow API:tf.keras搭建网络八股importtrain, testmodel = tf.keras.models.Spilemodel.fitmodel.summary3六步法model = tf.keras.models.Sequential ( 网络结构 )#描述各层网络网络结构举例:拉直层: tf.keras.layers.Flatten( )全连接层: tf.keras.la
2、yers.Dense(神经元个数, activation= 激活函数“ ,kernel_regularizer=哪种正则化)activation(字符串给出)可选: relu、 softmax、 sigmoid 、 tanhkernel_regularizer可选: tf.keras.regularizers.l1()、tf.keras.regularizers.l2()卷积层: tf.keras.layers.Conv2D(filters = 卷积核个数, kernel_size = 卷积核尺寸,strides = 卷积步长, padding = valid or same)LSTM层: t
3、f.keras.layers.LSTM()pile(optimizer = 优化器,loss = 损失函数metrics = “准确率” )Optimizer可选:sgd or tf.keras.optimizers.SGD (lr=学习率,momentum=动量参数)adagrad or tf.keras.optimizers.Adagrad (lr=学习率)adadelta or tf.keras.optimizers.Adadelta (lr=学习率)adam or tf.keras.optimizers.Adam (lr=学习率, beta_1=0.9, beta_2=0.999)lo
4、ss可选:mse or tf.keras.losses.MeanSquaredError()sparse_categorical_crossentropy or tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)Metrics可选:accuracy :y_和y都是数值,如y_=1y=1categorical_accuracy :y_和y都是独热码(概率分布),如y_=0,1,0 y=0.256,0.695,0.048sparse_categorical_accuracy :y_是数值,y是独热码(概率分布),如y_=
5、1 y=0.256,0.695,0.048model.fit (训练集的输入特征, 训练集的标签,batch_size= ,epochs= ,validation_data=(测试集的输入特征,测试集的标签),validation_split=从训练集划分多少比例给测试集,validation_freq = 多少次epoch测试一次)model.summary()models.Spilemodel.fitmodel.summary8import源码:p8_iris_sequential.pytrain test9用Tensorflow API:tf.keras搭建网络八股importtrain
6、, testclass MyModel(Model) model=MyMpilemodel.fitmodel.summary六步法class MyModel(Model):def _init_(self):super(MyModel, self)._init_()定义网络结构块def call(self, x):调用网络结构块,实现前向传播return ymodel = MyModel()_init_( )call( )定义所需网络结构块写出前向传播class MyModel(Model)model = MyModeliris_sequential.py11importtrain testcl
7、ass MyMpilemodel.fitmodel.summary源码:p11_iris_class.py00048 238 252 252 253 1861200000 MNIST数据集:提供 6万张 28*28 像素点的09手写数字图片和标签,用于训练。提供 1万张 28*28 像素点的09手写数字图片和标签,用于测试。 导入MNIST数据集:mnist = tf.keras.datasets.mnist(x_train, y_train) , (x_test, y_test) = mnist.load_data() 作为输入特征,输入神经网络时,将数据拉伸为一维数组:tf.keras.l
8、ayers.Flatten( )plt.imshow(x_train0, cmap=gray)#绘制灰度图plt.show()print(x_train0:n , x_train0)x_train0:print(y_train0:, y_train0)y_train0:5print(x_test.shape:, x_test.shapex_test.shape: (10000, 28, 28)源码:p13_mnist_datasets.pymnist_sequential.py源码:p14_mnist_sequential.pyimporttrain testmodels.Spilemodel
9、.fitmodel.summary14mnist_class.py源码:p15_mnist_class.pyimporttrain testclass MyMpilemodel.fit15model.summary FASHION数据集:提供 6万张 28*28 像素点的衣裤等图片和标签,用于训练。提供 1万张 28*28 像素点的衣裤等图片和标签,用于测试。导入FASHION数据集:fashion = tf.keras.datasets.fashion_mnist(x_train, y_train),(x_test, y_test) = fashion.load_data()参考源码: p16_fashion_sequential.pyp16_fashion_class.py