美文网首页
Keras学习笔记

Keras学习笔记

作者: DannyCP | 来源:发表于2019-09-29 23:40 被阅读0次

    Keras学习笔记

    [Toc]

    Python For Data ScienceCheat SheetKeras:
    https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Keras_Cheat_Sheet_Python.pdf

    1. 版本问题

    1.1. 特性改动

    1. <font color = 'red'>from __future__ import print_function</font>

    在开头加上这句代码之后,即使在python2.X,使用print就得像python3.X那样加括号使用,python2.X中print不需要括号,而在python3.X中则需要。其他例子:
    <font color = 'red'>from __future__ import division</font>,
    <font color = 'red'>from __future__ import absolute_import</font>,
    <font color = 'red'>from __future__ import with_statement</font>。等等

    2. 模块介绍

    2.1. Sequential

    The Sequential model is a linear stack of layers

    生成序列模型(Sequential Model)的两种方法

    from keras.models import Sequential
    from keras.layers import Dense, Activation
    
    方法一(passing a list of layer instances):
    model = Sequential([
        Dense(32, input_shape=(784,)),
        Activation('relu'),
        Dense(10),
        Activation('softmax'),
    ])
    
    方法二(via the .add()):
    model = Sequential()
    model.add(Dense(32, input_dim=784))
    model.add(Activation('relu'))
    

    2.2. MaxPooling2D

    1. 导入方法:from keras.layers import MaxPooling2D

    2. MaxPooling2D

       keras.layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)
      

    优点

    • 保证特征的位置与旋转不变性。因为不论这个强特征在哪个位置出现,都会不考虑其出现位置而能把它提出来。

    • 减少模型参数数量,有利于减少模型过拟合问题。因为经过Pooling操作后,往往把2D或者1D的数组转换为单一数值。

    缺点

    • 特征的位置信息在这一步骤完全丢失。

    • 同一特征的强度信息丢失了。

    1. 改进的Pooling机制有: K-max Pooling,Chunk-Max Pooling。

    2.3. Conv2D

    1. 导入方法:from keras.layers import Conv2D

    2. Conv2D

       keras.layers.Conv2D(filters, kernel_size, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)
      

    Caution

    When using this layer as the first layer in a model, provide the keyword argument input_shape (tuple of integers, does not include the batch axis), e.g. input_shape=(128, 128, 3) for 128x128 RGB pictures in data_format="channels_last"

    Input shape

    4D tensor with shape: (batch, channels, rows, cols) if data_format is "channels_first" or 4D tensor with shape: (batch, rows, cols, channels) if data_format is "channels_last"

    2.4. ImageDataGenerator

    1. 导入方法:from keras.preprocessing.image import ImageDataGenerator

    2. ImageDataGenerator Class

       keras.preprocessing.image.ImageDataGenerator(featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, zca_epsilon=1e-06, rotation_range=0, width_shift_range=0.0, height_shift_range=0.0, brightness_range=None, shear_range=0.0, zoom_range=0.0, channel_shift_range=0.0, fill_mode='nearest', cval=0.0, horizontal_flip=False, vertical_flip=False, rescale=None, preprocessing_function=None, data_format=None, validation_split=0.0, dtype=None)
      

      参数:

      • featurewise_center: Boolean. 对输入的图片每个通道减去每个通道对应均值。
      • samplewise_center: Boolan. 每张图片减去样本均值, 使得每个样本均值为0。
      • eaturewise_std_normalization(): Boolean()
      • samplewise_std_normalization(): Boolean()
      • zca_epsilon(): Default 12-6
      • zca_whitening: Boolean. 去除样本之间的相关性
      • rotation_range(): 旋转范围
      • width_shift_range(): 水平平移范围
      • height_shift_range(): 垂直平移范围
      • shear_range(): float, 透视变换的范围
      • zoom_range(): 缩放范围
      • fill_mode: 填充模式, constant, nearest, reflect
      • cval: fill_mode == 'constant'的时候填充值
      • horizontal_flip(): 水平反转
      • vertical_flip(): 垂直翻转
      • preprocessing_function(): user提供的处理函数
      • data_format(): channels_first或者channels_last
      • validation_split(): 多少数据用于验证集

    3. Datasets

    3.1. CIFAR10

    1. CIFAR10 small image classification

    Dataset of 50,000 32x32 color training images, labeled over 10 categories, and 10,000 test images.

    1. 用法
    from keras.datasets import cifar10
    
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    
    1. cifar10
    • 定义了数据加载方法:load_data()
    • 使用get_file()方法调用urlretrieve()方法下载cifar10数据集

    3.2. CIFAR100

    1. CIFAR100 small image classification

    Dataset of 50,000 32x32 color training images, labeled over 100 categories, and 10,000 test images.

    1. 用法
    from keras.datasets import cifar100
    
    (x_train, y_train), (x_test, y_test) = cifar100.load_data(label_mode='fine')
    

    3.3. IMDB

    1. IMDB Movie reviews sentiment classification

    Dataset of 25,000 movies reviews from IMDB, labeled by sentiment (positive/negative). Reviews have been preprocessed, and each review is encoded as a sequence of word indexes (integers). For convenience, words are indexed by overall frequency in the dataset, so that for instance the integer "3" encodes the 3rd most frequent word in the data. This allows for quick filtering operations such as: "only consider the top 10,000 most common words, but eliminate the top 20 most common words".

    As a convention, "0" does not stand for a specific word, but instead is used to encode any unknown word.

    1. 用法
    from keras.datasets import imdb
    
    (x_train, y_train), (x_test, y_test) = imdb.load_data(path="imdb.npz",
                                                          num_words=None,
                                                          skip_top=0,
                                                          maxlen=None,
                                                          seed=113,
                                                          start_char=1,
                                                          oov_char=2,
                                                          index_from=3)
    
    1. imdb

    3.4. MNIST

    1. MNIST database of handwritten digits

    Dataset of 60,000 28x28 grayscale images of the 10 digits, along with a test set of 10,000 images.

    1. 用法
    from keras.datasets import mnist
    
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    

    相关文章

      网友评论

          本文标题:Keras学习笔记

          本文链接:https://www.haomeiwen.com/subject/mzaqpctx.html