李宏毅机器学习笔记二

本文介绍了Keras的使用,强调了Adam优化器和mini-batch在训练深度神经网络中的优势,以及如何避免梯度消失问题。此外,讨论了深度学习模型的训练策略,包括ensemble集成学习和避免过拟合的方法。最后,探讨了深度学习背后的理论,通过类比解释了多层网络的重要性。
摘要由CSDN通过智能技术生成

8 Keras使用

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
adam高级优化方法不需要自己设置学习速率
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
如果有100个batch,1个epoch就更新参数100次

在这里插入图片描述
可以看出,batch_size 为1 和为10,参数更新速度是一样的(相同时间下,都用166s),这是由于vectorization带来的加速,并行运算。但随着batch_size 增大,速度会减慢。

mini-batch,参数更新有较大随机性,帮助跳出local minimum。如果不使用mini=batch,计算机可能会卡(内存不足),或则容易陷入local minimum。
在这里插入图片描述
在这里插入图片描述
上面的时间是下面的两倍,所以,当有GPU计算资源时,应该使用mini-batch,否则不会带来加速。
在这里插入图片描述

手写数字识别

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.datasets import mnist
from keras.utils import np_utils
from  keras.optimizers import SGD,Adam
import keras.callbacks
import time

a = time.time()
def load_data():
    (x_train,y_train),(x_test,y_test)=mnist.load_data()
    # print(x_train.shape,y_train.shape)  #(60000, 28, 28)   (60000,)
    # print(x_test.shape, y_test.shape)  # (10000, 28, 28) (10000,)

    number = 10000
    x_train = x_train[:number]
    y_train = y_train[:number]
    x_train = x_train.reshape(number,28*28)
    # print(y_train.shape) # (10000,)
    x_test = x_test.reshape(x_test.shape[0],28*28)
    x_train = x_train.astype(np.float32)

    x_test = x_test.astype(np.float32)

    y_train = np_utils.to_categorical(y_train,10)
    y_test = np_utils.to_categorical(y_test, 10)
    # print(y_train.shape)  # (10000, 10)
    x_train /= 255
    x_test /= 255

    # print(x_train.shape,y_train.shape,x_test.shape,y_test.shape) # (10000, 784) (10000, 10) (10000, 784) (10000, 10)
    return (x_train,y_train),(x_test,y_test)

(x_train,y_train),(x_test,y_test) = load_data()
model = Sequential()
# 全连接层
model.add(Dense(input_dim=28*28,units=500,activation='relu'))  # 'relu' 'sigmoid'

model.add(Dense(units=500,activation='relu'))

model.add(Dense(units=10,activation='softmax'))

# model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
# model.compile(loss='mse',optimizer='sgd',metrics=['accuracy'])
# model.compile(loss='categorical_crossentropy',optimizer=SGD(lr=0.01, momentum=0.9, nesterov=True),metrics=['accuracy'])
model.compile(loss='categorical_crossentropy',optimizer=Adam(),metrics=['accuracy'])
#
#可视化
log_filepath = 'E:\AAA\PYTHON\conda_test\keras_log'

tb_cb = keras.callbacks.TensorBoard(log_dir=log_filepath, write_images=1)
# 设置log的存储位置,将网络权值以图片格式保持在tensorboard中显示,设置每一个周期计算一次网络的权值,每层输出值的分布直方图

cbks = [tb_cb]

model.fit(x_train,y_t
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值