import tensorflow as tf
print('Tensorflow Version:{}'.format(tf.__version__))print(tf.config.list_physical_devices())
1.MNIST的数据集下载与预处理
import tensorflow as tf
from keras.datasets import mnist
from keras.utils import to_categorical
(train_x,train_y),(test_x,test_y)= mnist.load_data()
X_train,X_test = tf.cast(train_x/255.0,tf.float32),tf.cast(test_x/255.0,tf.float32)# 归一化
y_train,y_test = to_categorical(train_y),to_categorical(test_y)# onehotprint(X_train[:5])print(y_train[:5])
2.搭建MLP模型
from keras import Sequential
from keras.layers import Flatten,Dense
from keras import Input
model = Sequential()
model.add(Input(shape=(28,28)))
model.add(Flatten())
model.add(Dense(units=256,kernel_initializer='normal',activation='relu'))
model.add(Dense(units=10,kernel_initializer='normal',activation='softmax'))
model.summary()