zoukankan      html  css  js  c++  java
  • TensorFlow keras卷积神经网络 添加L2正则化

    model = keras.models.Sequential([
            #卷积层1
            keras.layers.Conv2D(32,kernel_size=5,strides=1,padding="same",data_format="channels_last",activation=tf.nn.relu,kernel_regularizer=keras.regularizers.l2(0.01)),
            #池化层1
            keras.layers.MaxPool2D(pool_size=2,strides=2,padding="same"),
            #卷积层2
            keras.layers.Conv2D(64,kernel_size=5,strides=1,padding="same",data_format="channels_last",activation=tf.nn.relu),
            #池化层2
            keras.layers.MaxPool2D(pool_size=2,strides=2,padding="same"),
            #数据整理
            keras.layers.Flatten(),
            #1024个,全连接层
            keras.layers.Dense(1024,activation=tf.nn.relu),
            #100个,全连接层
            keras.layers.Dense(100,activation=tf.nn.softmax)
        ])
    

      

    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    
    from tensorflow.python.keras.datasets import cifar100
    from tensorflow.python import keras
    import tensorflow as tf
    
    
    class CNNMnist(object):
    
        model = keras.models.Sequential([
            #卷积层1
            keras.layers.Conv2D(32,kernel_size=5,strides=1,padding="same",data_format="channels_last",activation=tf.nn.relu,kernel_regularizer=keras.regularizers.l2(0.01)),
            #池化层1
            keras.layers.MaxPool2D(pool_size=2,strides=2,padding="same"),
            #卷积层2
            keras.layers.Conv2D(64,kernel_size=5,strides=1,padding="same",data_format="channels_last",activation=tf.nn.relu),
            #池化层2
            keras.layers.MaxPool2D(pool_size=2,strides=2,padding="same"),
            #数据整理
            keras.layers.Flatten(),
            #1024个,全连接层
            keras.layers.Dense(1024,activation=tf.nn.relu),
            #100个,全连接层
            keras.layers.Dense(100,activation=tf.nn.softmax)
        ])
    
        def __init__(self):
            (self.x_train,self.y_train),(self.x_test,self.y_test) = cifar100.load_data()
    
            self.x_train = self.x_train/255.0
            self.x_test = self.x_test/255.0
    
    
        def compile(self):
            CNNMnist.model.compile(optimizer=keras.optimizers.Adam(),loss=keras.losses.sparse_categorical_crossentropy,metrics=["accuracy"])
    
        def fit(self):
            CNNMnist.model.fit(self.x_train,self.y_train,epochs=1,batch_size=32)
    
        def evaluate(self):
            test_loss,test_acc = CNNMnist.model.evaluate(self.x_test,self.y_test)
            print(test_loss,test_acc)
    
    if __name__ == '__main__':
        cnn = CNNMnist()
        print(CNNMnist.model.summary())
        cnn.compile()
        cnn.fit()
    

      

    多思考也是一种努力,做出正确的分析和选择,因为我们的时间和精力都有限,所以把时间花在更有价值的地方。
  • 相关阅读:
    手机号码 正则表达式
    邮政编码的正则表达式
    对象为null,调用非静态方法产生空指针异常
    文件找不到异常(FileNotFoundException)
    数组下标越界异常解决方法
    空指针异常的解决方法
    需求:打印九九乘法表
    创建简单线程
    ·博客作业06--图
    博客作业05--查找
  • 原文地址:https://www.cnblogs.com/LiuXinyu12378/p/12256785.html
Copyright © 2011-2022 走看看