Keras在希腊语中是尖角(horn)的意思,大概作者希望这个框架成为深度学习利器。
- Keras是神经网络的高级API。运行在诸如tensorflow,theano之类的框架之上,简而言之就是封装了一些常用的操作,让调用更加方便,少写代码(重点)。
- keras支持CPU,GPU运算。以tonsorflow作为后端时,自动支持GPU,不需要额外的代码。也支持多卡并行训练,包括模型并行和数据并行两种,需要一点额外的代码实现,总的来说也很简单。
- keras作为高层的API,也被引入了tensorflow的官方库,特别是tf2.0中,成为一个主要的包。根据另一篇博客谈到,tf.keras几乎兼容keras,但是tf.keras还有一些tf自己的特色函数,用一句话概括就是和keras用法一样但是更niubi一些。(未经验证)
- 最简单的使用方法包含几个步骤:模型定义,compile 指定损失函数和训练器等,fit 训练模型,eval评估模型,predict输出。 最简单的模型定义方式是使用顺序(Sequential )模型。如下所示
# 来自官方教程 网址:https://keras.io/
from keras.models import Sequential
from keras.layers import Dense
# 创建序列式模型,通过add方法添加网络层
model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))
# complie指定损失函数,监控的性能标准,优化方式
model.compile(loss='categorical_crossentropy', optimizer='sgd',
metrics=['accuracy'])
# x_train and y_train are Numpy arrays --just like in the Scikit-Learn API.
model.fit(x_train, y_train, epochs=5, batch_size=32) #模型训练
#评估模型
loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)
# 使用模型做预测
classes = model.predict(x_test, batch_size=128)