Keras

sudo pip install keras --安装

新建一个文件,里面存储的数据:第一列是属性,第二列是类别

11220044 0
11220044 0
11220044 0
11220033 1
11222244 0
11222244 0
11222444 1
11222044 0
11220044 0
11220044 0
11220044 0
11220044 0
11220044 0
11220055 1
11220066 1
11220077 1

X_test,y_test=np.loadtxt('/home/hadoop/py_test.txt',unpack='true')

这样python导入的是一个

先创建一个numpy.ndarray的数组

array([ 11220044.,  11220044.,  11220044.,  11220033.,  11222244.,
        11222244.,  11222444.,  11222044.,  11220044.,  11220044.,
        11220044.,  11220044.,  11220044.,  11220055.,  11220066.,
        11220077.])

这个数组是1*16即1行16列

这很显然不是我们想要的,因此要把数组转换一下,变成16*8的情况

这样才能处理.把前面的数组做下面改变,变成16*8的情况

x=np.array(
[
[1 ,1, 2, 2, 0, 0, 4, 4],
[1, 1 ,2, 2, 0, 0, 4, 4],
[1, 1 ,2, 2, 0, 0, 4, 4],
[1, 1, 2, 2, 0, 0, 3, 3],
[1, 1, 2, 2, 2, 2, 4, 4],
[1, 1, 2, 2, 2, 2, 4, 4],
[1, 1, 2, 2, 2, 4, 4, 4],
[1, 1, 2, 2, 2, 0, 4, 4],
[1, 1, 2, 2, 0, 0, 4, 4],
[1, 1, 2, 2, 0, 0, 4, 4],
[1, 1, 2, 2, 0, 0, 4, 4],
[1, 1, 2, 2, 0, 0, 4, 4],
[1, 1, 2, 2, 0, 0, 4 ,4],
[1, 1, 2, 2, 0, 0, 5, 5],
[1, 1, 2, 2, 0, 0, 6, 6],
[1, 1, 2, 2, 0, 0, 7, 7]
]
)

from keras.utils import np_utils
Using TensorFlow backend.
>>> y_train=np_utils.to_categorical(y_train,nb_classes=2) --把分类标志变成categorical格式,这里是二分类的情况,所以nb_classes=2

>>> print y_train[:3]
[[ 1.  0.]
 [ 1.  0.]
 [ 1.  0.]]

 

>>> X_test,y_test=np.loadtxt('/home/hadoop/py_test.txt',unpack='true')
>>> y_test=np_utils.to_categorical(y_test,nb_classes=2)
>>> from keras.models import Sequential
>>> from keras.layers import Dense,Activation
>>> from keras.optimizers import RMSprop
>>> model=Sequential([Dense(4,input_dim=8),Activation('relu'),Dense(2),Activation('softmax')])
>>>rmsprop=RMSprop(lr=0.001,rho=0.9,epsilon=1e-08,decay=0.0)
>>> model.compile(optimizer=rmsprop,loss='categorical_crossentropy',metrics=['accuracy'])

>>> model.fit(x,y_train,nb_epoch=2,batch_size=4)
#训练两次的结果

Epoch 1/2
16/16 [==============================] - 0s - loss: 3.1792 - acc: 0.3125     
Epoch 2/2
16/16 [==============================] - 0s - loss: 2.9554 - acc: 0.3125     
<keras.callbacks.History object at 0x7f075db1af90>
>>> model.fit(x,y_train,nb_epoch=100,batch_size=4)
Epoch 1/100
16/16 [==============================] - 0s - loss: 2.7971 - acc: 0.3125     
Epoch 2/100
16/16 [==============================] - 0s - loss: 2.6670 - acc: 0.3125     
Epoch 3/100
16/16 [==============================] - 0s - loss: 2.5527 - acc: 0.3125     
Epoch 4/100
16/16 [==============================] - 0s - loss: 2.4442 - acc: 0.3125     
Epoch 5/100
16/16 [==============================] - 0s - loss: 2.3397 - acc: 0.3125     
...   
Epoch 72/100
16/16 [==============================] - 0s - loss: 0.5435 - acc: 0.8750     
Epoch 73/100
16/16 [==============================] - 0s - loss: 0.5418 - acc: 0.8750     
Epoch 74/100
16/16 [==============================] - 0s - loss: 0.5381 - acc: 0.8750     
Epoch 75/100
16/16 [==============================] - 0s - loss: 0.5349 - acc: 0.8750     
Epoch 76/100
16/16 [==============================] - 0s - loss: 0.5333 - acc: 0.8750     
Epoch 77/100
16/16 [==============================] - 0s - loss: 0.5329 - acc: 0.8750     
Epoch 78/100
16/16 [==============================] - 0s - loss: 0.5292 - acc: 0.8750     
Epoch 79/100
16/16 [==============================] - 0s - loss: 0.5266 - acc: 0.8750     
Epoch 80/100
16/16 [==============================] - 0s - loss: 0.5281 - acc: 0.8750     
Epoch 81/100
16/16 [==============================] - 0s - loss: 0.5235 - acc: 0.8750     
Epoch 82/100
16/16 [==============================] - 0s - loss: 0.5232 - acc: 0.8750     
Epoch 83/100
16/16 [==============================] - 0s - loss: 0.5208 - acc: 0.8750     
Epoch 84/100
16/16 [==============================] - 0s - loss: 0.5205 - acc: 0.8750     
Epoch 85/100
16/16 [==============================] - 0s - loss: 0.5181 - acc: 0.8750     
Epoch 86/100
16/16 [==============================] - 0s - loss: 0.5178 - acc: 0.8750     
Epoch 87/100
16/16 [==============================] - 0s - loss: 0.5153 - acc: 0.8750     
Epoch 88/100
16/16 [==============================] - 0s - loss: 0.5138 - acc: 0.8750     
Epoch 89/100
16/16 [==============================] - 0s - loss: 0.5143 - acc: 0.8750     
Epoch 90/100
16/16 [==============================] - 0s - loss: 0.5110 - acc: 0.8750     
Epoch 91/100
16/16 [==============================] - 0s - loss: 0.5108 - acc: 0.8750     
Epoch 92/100
16/16 [==============================] - 0s - loss: 0.5092 - acc: 0.8750     
Epoch 93/100
16/16 [==============================] - 0s - loss: 0.5084 - acc: 0.8750     
Epoch 94/100
16/16 [==============================] - 0s - loss: 0.5065 - acc: 0.8750     
Epoch 95/100
16/16 [==============================] - 0s - loss: 0.5058 - acc: 0.8750     
Epoch 96/100
16/16 [==============================] - 0s - loss: 0.5059 - acc: 0.8750     
Epoch 97/100
16/16 [==============================] - 0s - loss: 0.5046 - acc: 0.8750     
Epoch 98/100
16/16 [==============================] - 0s - loss: 0.5039 - acc: 0.8750     
Epoch 99/100
16/16 [==============================] - 0s - loss: 0.5028 - acc: 0.8750     
Epoch 100/100
16/16 [==============================] - 0s - loss: 0.5036 - acc: 0.8750     
<keras.callbacks.History object at 0x7f075d940f10>
上面是寻量100次的结果,可见loss一直在降,但是准确率却没有再上升,停留在0.875

下面是在测试集合上验证
>>> loss,accuracy=model.evaluate(x,y_test)
16/16 [==============================] - 0s
>>> print loss
0.501516342163
>>> print accuracy
0.875


 

posted @ 2016-12-27 11:12  wangq17  阅读(385)  评论(0编辑  收藏  举报