教程概述：这里不需要编写太多的代码,不过我们将一步步慢慢地告诉你怎么以后怎么创建自己的模型。教程将会涵盖以下步骤:

• 加载数据
• 定义模型
• 编译模型
• 训练模型
• 评估模型
• 结合所有步骤在一起

• 有 python 2 或 3 的环境和编程基础
• 安装并配置好 Scipy 库（包括 Numpy ）
• 你安装好 Keras 并且有一个后端（Theano or TensorFlow）

1. 加载数据

```from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
```

```# load pima indians dataset
# split into input (X) and output (Y) variables
X = dataset[:, 0:8]
Y = dataset[:, 8]
```

2. 定义模型

Keras 中的模型被定义为一系列的层。

```# create model
model = Sequential()
```

3. 编译模型

```# Compile model
```

4. 训练模型

```# Fit the model
model.fit(X, Y, nb_epoch=150, batch_size=10)
```

5. 评估模型

```# evaluate the model
scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
```

6. 将这些放在一起

...Epoch 143/150768/768 [==============================] - 0s - loss: 0.4614 - acc: 0.7878Epoch 144/150768/768 [==============================] - 0s - loss: 0.4508 - acc: 0.7969Epoch 145/150768/768 [==============================] - 0s - loss: 0.4580 - acc: 0.7747Epoch 146/150768/768 [==============================] - 0s - loss: 0.4627 - acc: 0.7812Epoch 147/150768/768 [==============================] - 0s - loss: 0.4531 - acc: 0.7943Epoch 148/150768/768 [==============================] - 0s - loss: 0.4656 - acc: 0.7734Epoch 149/150768/768 [==============================] - 0s - loss: 0.4566 - acc: 0.7839Epoch 150/150768/768 [==============================] - 0s - loss: 0.4593 - acc: 0.7839768/768 [==============================] - 0sacc: 79.56%

福利: 做出预测

```# Create first network with Keras
from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
# Compile model
# Fit the model
model.fit(X, Y, nb_epoch=150, batch_size=10, verbose=2)
# calculate predictions
predictions = model.predict(X)
# round predictions
rounded = [round(x) for x in predictions]
print(rounded)
```

• 加载数据
• 定义模型
• 编译模型
• 训练模型
• 评估模型

0 条评论

862

4529

3097

5628

4687

3779

811

1452

3576

3428