CNN卷积神经网络天气识别代码
时间: 2023-08-10 15:08:27 浏览: 103
深度学习实战6-卷积神经网络(Pytorch)+聚类分析实现空气质量与天气预测,数据集+代码,可直接运行
5星 · 资源好评率100%
以下是一个简单的CNN卷积神经网络天气识别代码的例子:
```
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.utils import to_categorical
# 加载天气数据集
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# 对数据进行预处理
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# 创建卷积神经网络模型
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))
# 编译模型
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
# 训练模型
model.fit(train_images, train_labels, epochs=5, batch_size=64)
# 评估模型
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)
```
这个例子中,我们使用了TensorFlow的Keras API来创建CNN卷积神经网络模型,用于识别MNIST手写数字数据集。如果要使用此代码来识别天气,需要替换MNIST数据集为相应的天气数据集,并对代码进行适当的修改。
阅读全文