from tensorflow.python.keras.datasets import cifar100
from tensorflow.python.keras import layers,losses,optimizers
from tensorflow.python.keras.models import Sequential
import tensorflow as tf
class CNNMnist(object):
model=Sequential([
layers.Conv2D(32,kernel_size=5,strides=1,padding='same',data_format='channels_last',activation=tf.nn.relu),
layers.MaxPool2D(pool_size=2,strides=2,padding='same'),
layers.Conv2D(64,kernel_size=5,strides=1,padding='same',data_format='channels_last',activation=tf.nn.relu),
layers.MaxPool2D(pool_size=2,strides=2,padding='same'),
layers.Flatten(),
layers.Dense(1024,activation=tf.nn.relu),
layers.Dense(100,activation=tf.nn.softmax)
])
def __init__(self):
# 读取数据集
(self.train,self.train_label),(self.test,self.test_label) = cifar100.load_data()
# 对数据集进行归一化处理
self.train = self.train.reshape(-1,32,32,3) / 255.0
self.test = self.test.reshape(-1,32,32,3) / 255.0
def compile(self):
def compile(self):
CNNMnist.model.compile(optimizer=optimizers.adam_v2.Adam(),
loss=losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
return None
def fit(self):
CNNMnist.model.fit(self.train,self.train_label,epochs=1,batch_size=32)
return None
if __name__ == '__main__':
cnn = CNNMnist()
cnn.compile()
cnn.fit()
AttributeError: 'CNNMnist' object has no attribute 'compile'