DL框架之Keras:深度学习框架Keras框架的简介、安装(Python库)、相关概念、Keras模型使用、使用方法之详细攻略(二)

简介: DL框架之Keras:深度学习框架Keras框架的简介、安装(Python库)、相关概念、Keras模型使用、使用方法之详细攻略

Keras的安装


pip install Keras

python -m pip install keras


image.png


哈哈,大功告成!继续学习去啦!

pip install --upgrade Kera


image.png


190827更新到2.2.5


image.png


190827再次还原到2.2.4

image.png



相关文章

Py之keras-resnet:keras-resnet的简介、安装、使用方法之详细攻略



Keras的使用方法


0、三种API方式:The Sequential Model (序列模型)、The functional API (函数式API)、Model subclassing(模型子类化)


from keras.models import Model

from keras.callbacks import ModelCheckpoint

from keras.layers import Conv2D, MaxPool2D, Flatten, Dropout, Dense, Input

from keras.optimizers import Adam

from keras.backend.tensorflow_backend import set_session

from keras.utils.vis_utils import model_to_dot

import tensorflow as tf

from keras.backend.tensorflow_backend import set_session

np.random.seed(5)

config = tf.ConfigProto()

config.gpu_options.allow_growth=True

set_session(tf.Session(config=config))


1、The Sequential Model 序列模型


-非常简单

-仅适用于单输入,单输出,顺序的层堆叠

-适用于70%以上的用例


Sequential 序列模型如所示

可以简单地使用.add() 来堆叠模型

在完成了模型的构建后, 可以使用.compile() 来配置学习过程:

如果需要,还可以进一步地配置优化器:

批量地在训练数据上进行迭代: # x_train 和y_train 是Numpy 数组--就像在Scikit-Learn API 中一样


或者,可以手动地将批次的数据提供给模型:


一行代码就能评估模型性能:

对新的数据生成预测

1、快速开始序贯(Sequential)模型  

序贯模型是多个网络层的线性堆叠,也就是“一条路走到黑”。  

(1)、可以通过向Sequential模型传递一个layer的list来构造该模型:  

from keras.models import Sequential

from keras.layers import Dense, Activation  

model = Sequential([ Dense(32, units=784), Activation('relu'), Dense(10), Activation('softmax'), ])

(2)、也可以通过.add()方法一个个的将layer加入模型中:  

model = Sequential() model.add(Dense(32, input_shape=(784,)))

model.add(Activation('relu'))


#引入Sequential,Dense,Activation

from keras.models import Sequential

from keras.layers import Dense, Activation

#向layer添加list方式

model = Sequential([Dense(32, input_dim=784),Activation('relu'),Dense(10),Activation('softmax'),])

#通过.add()方式

model = Sequential()

model.add(Dense(32, input_dim=784))

model.add(Activation('relu'))


2、The functional API 函数式API


-象玩乐高积木

-多输入,多输出,任意静态图拓扑

-适用于95%的用例


Keras 函数式API 是定义复杂模型(如多输出模型、有向无环图,或具有共享层的模型)的方法。


例一:全连接网络


3、Model subclassing 模型子类化


-最大的灵活性

-更大的可能错误面


(1)、通过对tf.keras.Model 进行子类化并定义你自己的前向传播来构建完全可自定义的模型。在__init__ 方法中创建层并将它们设置为类实例的属性。在call 方法中定义前向传播。

(2)、在启用Eager Execution 时,模型子类化特别有用,因为可以命令式地编写前向传播。

(3)、以下示例展示了使用自定义前向传播进行子类化的tf.keras.Model


class MyModel(tf.keras.Model):

   def __init__(self, num_classes=10):

       super(MyModel, self).__init__(name='my_model')

       self.num_classes = num_classes  

       # Define your layers here.

       self.dense_1 = layers.Dense(32, activation='relu')

       self.dense_2 = layers.Dense(num_classes, activation='sigmoid')

     

   def call(self, inputs):

       # Define your forward pass here,

       # using layers you previously defined (in `__init__`).

       x = self.dense_1(inputs)

       return self.dense_2(x)

   def compute_output_shape(self, input_shape):

       # You need to override this function if you want to use the subclassed model

       # as part of a functional-style model.# Otherwise, this method is optional.

       shape = tf.TensorShape(input_shape).as_list()

       shape[-1] = self.num_classes

       return tf.TensorShape(shape)

实例化新模型类

model = MyModel(num_classes=10) # The compile step specifies the training configuration.

model.compile(optimizer=tf.train.RMSPropOptimizer(0.001),

             loss='categorical_crossentropy',

             metrics=['accuracy'])

# Trains for 5 epochs.

model.fit(data, labels, batch_size=32, epochs=5)



相关文章
|
1月前
|
机器学习/深度学习 数据可视化 TensorFlow
使用Python实现深度学习模型的分布式训练
使用Python实现深度学习模型的分布式训练
175 73
|
1月前
|
机器学习/深度学习 算法 安全
从方向导数到梯度:深度学习中的关键数学概念详解
方向导数衡量函数在特定方向上的变化率,其值可通过梯度与方向向量的点积或构造辅助函数求得。梯度则是由偏导数组成的向量,指向函数值增长最快的方向,其模长等于最速上升方向上的方向导数。这两者的关系在多维函数分析中至关重要,广泛应用于优化算法等领域。
103 36
从方向导数到梯度:深度学习中的关键数学概念详解
|
1月前
|
机器学习/深度学习 数据采集 数据挖掘
使用Python实现智能食品消费市场分析的深度学习模型
使用Python实现智能食品消费市场分析的深度学习模型
127 36
|
1月前
|
机器学习/深度学习 数据采集 供应链
使用Python实现智能食品消费需求分析的深度学习模型
使用Python实现智能食品消费需求分析的深度学习模型
84 21
|
1月前
|
机器学习/深度学习 数据采集 搜索推荐
使用Python实现智能食品消费偏好预测的深度学习模型
使用Python实现智能食品消费偏好预测的深度学习模型
84 23
|
1月前
|
机器学习/深度学习 数据采集 数据挖掘
使用Python实现智能食品消费习惯预测的深度学习模型
使用Python实现智能食品消费习惯预测的深度学习模型
119 19
|
1月前
|
机器学习/深度学习 数据采集 数据挖掘
使用Python实现智能食品消费趋势分析的深度学习模型
使用Python实现智能食品消费趋势分析的深度学习模型
126 18
|
1月前
|
机器学习/深度学习 数据采集 供应链
使用Python实现智能食品消费需求预测的深度学习模型
使用Python实现智能食品消费需求预测的深度学习模型
74 10
|
1月前
|
机器学习/深度学习 数据采集 搜索推荐
使用Python实现深度学习模型:智能食品消费行为预测
使用Python实现深度学习模型:智能食品消费行为预测
82 8
|
1月前
|
机器学习/深度学习 数据采集 数据挖掘
使用Python实现智能食品消费模式预测的深度学习模型
使用Python实现智能食品消费模式预测的深度学习模型
60 2