Keras中建立神经网络测试XOR问题

#机器学习的课程中,XOR问题是必讲的一个例题,它能很好地帮助理解神经网络的工作过程。可是它只有4个样本,每个样本二维输入一维输出,太简单。实现来讲,体现不出
#神经网络的强大来。

# -*- coding: utf-8 -*-
"""
Created on Fri May 20 10:08:00 2016

@author: Michelle
"""

from keras.models import Sequential    
from keras.layers.core import Dense, Activation  
from keras.optimizers import SGD
import numpy as np 

#input data
x_train =  np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y_label = np.array([0,1,1,0])

#create models, with 1hidden layers    
model = Sequential()    
model.add(Dense(32, init='uniform', input_dim=2))    
model.add(Activation('relu')) 

model.add(Dense(1))    
model.add(Activation('sigmoid'))
#training
sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=["accuracy"])
   
hist = model.fit(x_train, y_label, batch_size=1, nb_epoch=100, shuffle=True, verbose=0,validation_split=0.0)
print(hist.history)
#evaluating model performance
loss_metrics = model.evaluate(x_train, y_label, batch_size=1)

你可能感兴趣的:(机器学习)