AlexNet网络模型代码
时间: 2023-07-05 18:17:53 浏览: 86
以下是AlexNet网络模型的代码:
```python
import tensorflow as tf
def alexnet(input_shape, num_classes):
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(filters=96, kernel_size=(11,11), strides=(4,4), activation='relu', input_shape=input_shape),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(3,3), strides=(2,2)),
tf.keras.layers.Conv2D(filters=256, kernel_size=(5,5), strides=(1,1), activation='relu', padding="same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(3,3), strides=(2,2)),
tf.keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(3,3), strides=(2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(units=4096, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(units=4096, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(units=num_classes, activation='softmax')
])
return model
```
该模型包括13个层,其中包括5个卷积层、3个池化层和5个全连接层。它的输入是一个形状为(224,224,3)的图像,输出是一个长度为num_classes的向量,表示每个类别的概率。该模型使用了ReLU激活函数和批归一化技术,以及Dropout层来防止过拟合。
阅读全文