【python keras实战】快速开始:训练一个简单的Keras 模型

Keras 模型在输入数据和标签的 Numpy 矩阵上进行训练。为了训练一个模型,你通常会使用 fit 函数。

对于具有2个类的单输入模型(二进制分类)


from keras.models import Sequential
from keras.layers import Dense, Activation


model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# 生成虚拟数据
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))

# 训练模型,以 32 个样本为一个 batch 进行迭代
model.fit(data, labels, epochs=10, batch_size=32)

测试例子1运行结果

>>> model.fit(data, labels, epochs=10, batch_size=32)
Epoch 1/10
1000/1000 [==============================] - 0s - loss: 0.6670 - acc: 0.5900     
Epoch 2/10
1000/1000 [==============================] - 0s - loss: 0.6616 - acc: 0.6040     
Epoch 3/10
1000/1000 [==============================] - 0s - loss: 0.6579 - acc: 0.6100     
Epoch 4/10
1000/1000 [==============================] - 0s - loss: 0.6580 - acc: 0.6090     
Epoch 5/10
1000/1000 [==============================] - 0s - loss: 0.6483 - acc: 0.6250     
Epoch 6/10
1000/1000 [==============================] - 0s - loss: 0.6448 - acc: 0.6370     
Epoch 7/10
1000/1000 [==============================] - 0s - loss: 0.6454 - acc: 0.6340     
Epoch 8/10
1000/1000 [==============================] - 0s - loss: 0.6403 - acc: 0.6210     
Epoch 9/10
1000/1000 [==============================] - 0s - loss: 0.6387 - acc: 0.6330     
Epoch 10/10
1000/1000 [==============================] - 0s - loss: 0.6353 - acc: 0.6340     
<keras.callbacks.History object at 0x62051d0>
>>> 

对于具有10个类的单输入模型(多分类分类)

import keras
from keras.models import Sequential
from keras.layers import Dense, Activation

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# 生成虚拟数据
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(10, size=(1000, 1))

# 将标签转换为分类的 one-hot 编码
one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)

# 训练模型,以 32 个样本为一个 batch 进行迭代
model.fit(data, one_hot_labels, epochs=10, batch_size=32)

测试例子2运行结果:

>>> model.fit(data, one_hot_labels, epochs=10, batch_size=32)
Epoch 1/10
1000/1000 [==============================] - 0s - loss: 2.3585 - acc: 0.1150     
Epoch 2/10
1000/1000 [==============================] - 0s - loss: 2.2992 - acc: 0.1320     
Epoch 3/10
1000/1000 [==============================] - 0s - loss: 2.2846 - acc: 0.1370     
Epoch 4/10
1000/1000 [==============================] - 0s - loss: 2.2771 - acc: 0.1510     
Epoch 5/10
1000/1000 [==============================] - 0s - loss: 2.2700 - acc: 0.1630     
Epoch 6/10
1000/1000 [==============================] - 0s - loss: 2.2601 - acc: 0.1590     
Epoch 7/10
1000/1000 [==============================] - 0s - loss: 2.2479 - acc: 0.1920     
Epoch 8/10
1000/1000 [==============================] - 0s - loss: 2.2415 - acc: 0.1800     
Epoch 9/10
1000/1000 [==============================] - 0s - loss: 2.2335 - acc: 0.1920     
Epoch 10/10
1000/1000 [==============================] - 0s - loss: 2.2229 - acc: 0.2200     
<keras.callbacks.History object at 0x674a610>
>>> 
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页