首页 > 其他 > 详细

TensorFlow-keras 100分类

时间:2020-02-03 00:02:17      阅读:146      评论:0      收藏:0      [点我收藏+]
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),
        #池化层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()

  

TensorFlow-keras 100分类

原文:https://www.cnblogs.com/LiuXinyu12378/p/12254284.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!