import tensorflow as tf
import tensorflow. keras
from tensorflow. keras. datasets import cifar10
import numpy as np
from tensorflow. keras. utils import to_categorical
np. random. seed ( 10 )
( x_img_train, y_label_train) , \
( x_img_test, y_label_test) = cifar10. load_data ( )
x_img_train_normalize= x_img_train. astype ( 'float32' ) / 255.0
x_img_test_normalize= x_img_test. astype ( 'float32' ) / 255.0
y_label_train_onehot = tf. keras. utils. to_categorical ( y_label_train)
y_label_test_onehot = tf. keras. utils. to_categorical ( y_label_test)
这是建立一个2次卷积运算的神经网络
from tensorflow. keras. models import Sequential
from tensorflow. keras. layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D, Activation, ZeroPadding2D
model = Sequential ( )
model. add ( Conv2D ( filters= 32 ,
kernel_size= ( 3 , 3 ) ,
padding= 'same' ,
input_shape= ( 32 , 32 , 3 ) ,
activation= 'relu' ) )
model. add ( Dropout ( rate= 0.25 ) )
model. add ( MaxPooling2D ( pool_size= ( 2 , 2 ) ) )
model. add ( Conv2D ( filters= 64 ,
kernel_size= ( 3 , 3 ) ,
padding= 'same' ,
activation= 'relu' ) )
model. add ( Dropout ( 0.25 ) )
model. add ( MaxPooling2D ( pool_size= ( 2 , 2 ) ) )
model. add ( Flatten ( ) )
model. add ( Dropout ( rate= 0.25 ) )
model. add ( Dense ( 1024 , activation= 'relu'