使用jupyter notebook对数据集CIFAR10进行分类,把数据分割为训练集和测试集,比例为2:8。搭建全连接网络和卷积神经网络CNN,分别得到两种网络的预测结果的混淆矩阵,及灵敏性(Sensitivity),特异性(Specificity),对两个网络进行对比。
时间: 2024-09-08 17:04:32 浏览: 96
在使用Jupyter Notebook对CIFAR-10数据集进行分类时,首先需要导入必要的库,如NumPy、Matplotlib和TensorFlow或Keras。然后可以加载CIFAR-10数据集,该数据集通常会自带测试集和训练集的划分,但这里我们需要按照2:8的比例重新划分。
以下是使用Python代码在Jupyter Notebook中进行操作的概述步骤:
1. 导入库和加载数据集:
```python
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import cifar10
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten
# 加载CIFAR-10数据集
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# 按照2:8的比例划分训练集和测试集
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.8, random_state=42)
```
2. 构建全连接网络(DNN)和卷积神经网络(CNN)模型:
- 全连接网络模型示例:
```python
def build_dnn_model(input_shape):
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
return model
dnn_model = build_dnn_model(x_train.shape[1:])
```
- 卷积神经网络模型示例:
```python
def build_cnn_model(input_shape):
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=input_shape))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
return model
cnn_model = build_cnn_model(x_train.shape[1:])
```
3. 编译和训练模型:
```python
# 编译模型,指定优化器、损失函数和评价指标
dnn_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
cnn_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# 训练模型
dnn_model.fit(x_train, y_train, batch_size=64, epochs=10, validation_data=(x_val, y_val))
cnn_model.fit(x_train, y_train, batch_size=64, epochs=10, validation_data=(x_val, y_val))
```
4. 评估模型并生成混淆矩阵以及灵敏性、特异性:
```python
# 对测试集进行预测
dnn_predictions = dnn_model.predict(x_test)
cnn_predictions = cnn_model.predict(x_test)
# 将预测结果转换为类别标签
dnn_pred_labels = np.argmax(dnn_predictions, axis=1)
cnn_pred_labels = np.argmax(cnn_predictions, axis=1)
y_test_labels = np.argmax(y_test, axis=1)
# 生成混淆矩阵
dnn_cm = confusion_matrix(y_test_labels, dnn_pred_labels)
cnn_cm = confusion_matrix(y_test_labels, cnn_pred_labels)
# 计算灵敏性(Sensitivity)和特异性(Specificity)
dnn_report = classification_report(y_test_labels, dnn_pred_labels, output_dict=True)
cnn_report = classification_report(y_test_labels, cnn_pred_labels, output_dict=True)
# 输出评价指标
dnn_sensitivity = dnn_report['macro avg']['recall']
dnn_specificity = 1 - np.mean([1 - dnn_report[str(i)]['precision'] for i in range(10)])
cnn_sensitivity = cnn_report['macro avg']['recall']
cnn_specificity = 1 - np.mean([1 - cnn_report[str(i)]['precision'] for i in range(10)])
```
5. 对比两个网络的性能:
通过比较两个模型的准确率、混淆矩阵、灵敏性和特异性,可以对全连接网络和卷积神经网络进行对比分析。
阅读全文