深度学习之路=====2=====>>AlexNet(tensorflow2)

模型结构

在这里插入图片描述

在这里插入图片描述

代码

import tensorflow as tf
import os
import datetime
from tensorflow.keras.layers import Conv2D,BatchNormalization,Activation,MaxPool2D,Dropout,Flatten,Dense
from tensorflow.keras import Model

cifar=tf.keras.datasets.cifar10
(x_train,y_train),(x_test,y_test)=cifar.load_data()
x_train,x_test=x_train/255.0,x_test/255.0
from tensorflow.keras import layers
%load_ext tensorboard
class AlexNet(Model):
    def __init__(self):
        super(AlexNet,self).__init__()
        #CONV1
        self.input_layer=layers.Conv2D(filters=96,kernel_size=(11,11),strides=1,padding="same",activation='relu')
        self.middle_layers=[
        layers.BatchNormalization(),
        layers.MaxPool2D(pool_size=(3,3),strides=2),
        #CONV2
        layers.Conv2D(filters=256,kernel_size=(5,5),strides=1,padding="same",activation='relu'),
        layers.BatchNormalization(),
        layers.MaxPool2D(pool_size=(3,3),strides=2),
        #CONV3
        layers.Conv2D(filters=384,kernel_size=(3,3),strides=1,padding="same",activation='relu'),
        #CONV4
        layers.Conv2D(filters=384,kernel_size=(3,3),strides=1,padding="same",activation='relu'),
        #CONV5
        layers.Conv2D(filters=256,kernel_size=(3,3),strides=1,padding="same",activation='relu'),
        layers.MaxPool2D(pool_size=(3,3),strides=2),
        slayers.Flatten(),
        layers.Dense(2048,activation='relu'),
        layers.Dropout(0.5),
        layers.Dense(2048,activation='relu'),
        layers.Dropout(0.5)   ]
        
        self.output_layer=layers.Dense(10,activation='softmax')
    def call(self,x):
        x=self.input_layer(x)
        for layer in self.middle_layers:
            x=layer(x)
        y=self.output_layer(x)
        return y
model=AlexNet()
model.compile(optimizer="adam",loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),metrics=['sparse_categorical_accuracy'])
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
history=model.fit(x_train,y_train,batch_size=32,epochs=5,validation_data=(x_test,y_test),validation_freq=1,callbacks=[tensorboard_callback])



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值