import tensorflow as tffrom tensorflow.keras import layers# 定义模型model = tf.keras.Sequential()model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Conv2D(64, (3, 3), activation='relu'))model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Conv2D(64, (3, 3), activation='relu'))model.add(layers.Flatten())model.add(layers.Dense(64, activation='relu'))model.add(layers.Dense(10))# 编译模型model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])# 训练模型model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))
时间: 2023-05-19 19:01:43 浏览: 206
Python示例使用Keras构建和训练一个基本的CNN模型来进行图像分类任务.pdf
这是Python中导入TensorFlow和Keras的代码。其中,import tensorflow as tf表示导入TensorFlow库,from tensorflow.keras import layers表示从TensorFlow的Keras模块中导入layers层。
阅读全文