首页 > 其他分享 >LeNet5实现CIFAR-10图片分类

LeNet5实现CIFAR-10图片分类

时间:2022-11-07 22:15:03浏览次数:52  
标签:layers 10 plt LeNet5 keras activation CIFAR tf history

import tensorflow as tf
from tensorflow.keras import datasets ,layers ,models
import matplotlib.pyplot as plt
from keras import regularizers
# load and normalize the data
(x_train, y_train), (x_test, y_test) = datasets.cifar10.load_data()
num_classes = 10
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255
# LeNet5
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(filters=6, kernel_size=(5, 5), padding='valid', activation=tf.nn.relu,
                           input_shape=(32, 32, 3)),
    tf.keras.layers.AveragePooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
    tf.keras.layers.Conv2D(filters=16, kernel_size=(5, 5), padding='valid', activation=tf.nn.relu,
                           input_shape=(32, 32, 3)),
    tf.keras.layers.AveragePooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(units=16, kernel_regularizer=regularizers.l2(0.001),activation=tf.nn.relu,input_shape=(10000,)),
    tf.keras.layers.Dense(units=16, activation=tf.nn.relu,kernel_regularizer=regularizers.l2(0.001),),
    tf.keras.layers.Dense(units=10, activation=tf.nn.sigmoid),
#     tf.keras.layers.Dense(16, kernel_regularizer=regularizers.l2(0.001),
# activation=tf.nn.relu, input_shape=(10000,)),
#     tf.keras.layers.Dense(16, kernel_regularizer=regularizers.l2(0.001),
# activation=tf.nn.relu),
#     tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
])
model.summary()

# train the model using ADAM
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
# fit
history=model.fit(x_train, y_train, batch_size=64, epochs=5, validation_split=0.2)

# 训练结果可视化
loss = history.history["loss"]
val_loss = history.history["val_loss"]
acc = history.history["sparse_categorical_accuracy"]
val_acc = history.history["val_sparse_categorical_accuracy"]
plt.subplot(1,2,1)
plt.plot(loss,label = "Training Loss")
plt.plot(val_loss,label = "Validation Loss")
plt.title("Trainning and Validation Loss")
plt.legend()
plt.subplot(1,2,2)
plt.plot(acc,label = "Training Acc")
plt.plot(val_acc,label = "Validation Acc")
plt.title("Training and Validation Acc")
plt.legend()
# evaluate
model.evaluate(x_test, y_test,verbose=2)

 

标签:layers,10,plt,LeNet5,keras,activation,CIFAR,tf,history
From: https://www.cnblogs.com/ljq20204136/p/16867646.html

相关文章

  • POJ-1018
    POJ-1018(现在好像过不了)题意目前有一个公司需要购进宽带设备,每种设备有多款机器供选择,每种设备都需购进一台,现给出每台设备的带宽p与价格q,要求选择设备的最小带宽\(min(......
  • P3205 [HNOI2010]合唱队
    P3205[HNOI2010]合唱队题目大意:一个排队方式,共\(n\)个人($n\leq1000$),如果当前人的身高大于前一个,那么将这个人排在前一个人右边,如果当前人身高小于前一个人,那么......
  • 三\丰\云\评\测+100分
    使用+三+丰+云+几天了,效果杠杠的,有几大好处:①服务器访问速度确实不错②可以一直免费使用,比较适合平时开发测试使用③运行稳定,性价比是同类型行服务器中是很不错......
  • ASEMI肖特基二极管MBR10200AC体积,MBR10200AC大小
    编辑-ZASEMI肖特基二极管MBR10200AC参数:型号:MBR10200AC最大重复峰值反向电压(VRRM):200V最大RMS电桥输入电压(VRMS):140V最大直流阻断电压(VDC):200V最大平均正向整流输出电流(IF):10A峰......
  • SB20100LFCT-ASEMI半塑封肖特基二极管SB20100LFCT
    编辑-ZSB20100LFCT在ITO-220AB封装里采用的2个芯片,其尺寸都是82MIL,是一款半塑封肖特基二极管。SB20100LFCT的浪涌电流Ifsm为180A,漏电流(Ir)为6uA,其工作时耐温度范围为-65~15......
  • vue组件间的通讯(10种方法)【重要】(待补充。。。)
    1.props/$emitprops主要用于父组件传递数据给子组件,父==>子。Vue自定义事件父组件可以在使用子组件的地方直接用v-on来监听子组件触发的事件。即父组件中使用v-on绑定自......
  • SB20100LFCT-ASEMI半塑封肖特基二极管SB20100LFCT
    编辑-ZSB20100LFCT在ITO-220AB封装里采用的2个芯片,其尺寸都是82MIL,是一款半塑封肖特基二极管。SB20100LFCT的浪涌电流Ifsm为180A,漏电流(Ir)为6uA,其工作时耐温度范围为-65~......
  • 展锐T618/T610安卓4G核心板/开发板/方案定制
    XY610(以UMS512为主芯片)是一款基于UNISOC平台、工业级高性能、可运行安卓操作系统的4G智能模块,支持TDD-LTE/LTE-FDD/WCDMA/TD-SCDMA/CDMA2000/GSM等多种制式;支持WiFi80......
  • 【2022-10-31】连岳摘抄
    23:59如人走路一般,走得一段,方认得一段;走到岐路处,有疑便问,问了又走,方渐能到得欲到之处。                         ......
  • CF1045G AI Robots (动态开点线段树 + 离散化)(关于其空间复杂度的分析)
    题目大意:火星上有\(N\)个机器人排成一行,第\(i\)个机器人的位置为\(x_i\),视野维\(r_i\),智商为\(q_i\)。我们认为第\(i\)个机器人可以看到的位置是\([x_i-r_i,......