用Keras和Python开发你的第一个神经网络

Keras功能强大且易于使用，常被用来训练深度学习模型在这篇文章中，我将向你展示如何使用Keras和Python一步一步地创建你的第一个神经网络模型，我们开始吧。

1、加载数据

2、定义模型

3、编译模型

4、训练模型

5、评估模型

6、做出预测

1

from keras.models import Sequential

from keras.layers import Dense

import numpy

# fix random seed for reproducibility

#split into input (X) and output (Y) variables

X = dataset[:,0:8]

Y = dataset[:,8]

2

#create model

model=Sequential()

activation='relu'))

3

#Compile model

model.compile(

loss='binary_crossentropy',

metrics=['accuracy'])

4

# Fit the model

model.fit(

X,

Y,

epochs=150,

batch_size=10)

5

# evaluate the model

scores=model.evaluate(X,Y)

print("\n%s:%.2f%%"%(model.metrics_names[1],scores[1]*100))

# Create your first MLP in Keras

from keras.models import Sequential

from keras.layers import Dense

import numpy

# fix random seed for reproducibility

numpy.random.seed(7)

# 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,epochs=150,batch_size=10)

# evaluate the model

scores=model.evaluate(X,Y)

print("\n%s:%.2f%%"% (model.metrics_names[1],scores[1]*100))

...

Epoch 145/150

768/768 [==============================] - 0s - loss: 0.5105 - acc: 0.7396

Epoch 146/150

768/768 [==============================] - 0s - loss: 0.4900 - acc: 0.7591

Epoch 147/150

768/768 [==============================] - 0s - loss: 0.4939 - acc: 0.7565

Epoch 148/150

768/768 [==============================] - 0s - loss: 0.4766 - acc: 0.7773

Epoch 149/150

768/768 [==============================] - 0s - loss: 0.4883 - acc: 0.7591

Epoch 150/150

768/768 [==============================] - 0s - loss: 0.4827 - acc: 0.7656

6

# 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,epochs=150,batch_size=10,verbose=2)

# calculate predictions

predictions=model.predict(X)

# round predictions

rounded=[round(x[0]) for x in predictions]

print(rounded)

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180803G08L4V00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2018-06-01

2018-05-23

2022-01-25

2022-01-25

2022-01-25

2022-01-25