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
Keras的更多相关文章
-
[Keras] Develop Neural Network With Keras Step-By-Step
简单地训练一个四层全连接网络. Ref: http://machinelearningmastery.com/tutorial-first-neural-network-python-keras/ 1 ...
-
keras 中如何自定义损失函数
http://lazycoderx.com/2016/10/09/keras%E4%BF%9D%E5%AD%98%E6%A8%A1%E5%9E%8B%E6%97%B6%E4%BD%BF%E7%94%A ...
-
keras安装
找对工具真的很重要,周末和学霸折腾了一天才装了几个包,问了同事找了一个方便的包,装起来不要太快啊.二十分钟全部搞定. 一.Anaconda 真是大杀器,牛到飞起来,一键部署,所有常用的机器学习包全部包 ...
-
ubuntu系统theano和keras的安装
说明:系统是unbuntu14.04LTS,32位的操作系统,以前安装了python3.4,现在想要安装theano和keras.步骤如下: 1,安装pip sudo apt-get install ...
-
neurosolutions 人工神经网络集成开发环境 keras
人工神经网络集成开发环境 : http://www.neurosolutions.com/ keras: https://github.com/fchollet/keras 文档 http ...
-
Keras官方Example里Mnist-cnn的调试运行
问题:老板让测试运行Keras官网里的Mnist-cnn.py,结果从下载数据就是一路坑-- 当前环境:Ubuntu12.04.python2.7.Keras 1.1.1(不知道这个版本号对不对,在启 ...
-
[Keras] mnist with cnn
典型的卷积神经网络. Keras傻瓜式读取数据:自动下载,自动解压,自动加载. # X_train: array([[[[ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0. ...
-
[Keras] Install and environment setting
Documentation: https://keras.io/ 1. 利用anaconda 管理python库是明智的选择. conda update conda conda update anac ...
-
linux install Theano+Tensorflow+Keras
安装过程中,网络状态一定要好,如果安装过程中出现time out的提示信息,今天就可以洗洗睡啦,等明天网络状态好的时候再安装. 安装过程出现不知名的错误的时候,执行第一步,update一下 1.#up ...
-
Keras学习~试用卷积~跑CIFAR-10
import numpy as np import cPickle import keras as ks from keras.layers import Dense, Activation, Fla ...
随机推荐
-
负margin的原理以及应用
负margin在布局中往往起到意想不到的效果,比如在多栏等高布局中就是用该技巧. 虽说网络上关于负margin的实践有很多,但对margin负值为什么会出现这样的效果却没有多少讲解,本篇的目的就是阐述 ...
-
audio和video元素
目录 [1]HTML元素 audio video source track[2]API 方法 属性 事件 audio专有 前面的话 HTML5新增了两个与媒体相关的标签,让开发人员不必依赖任何插件就能 ...
-
【过程改进】 windows下jenkins常见问题填坑
没有什么高深的东西,1 2天的时间大多数人都能自己摸索出来,这里将自己遇到过的问题分享出来避免其他同学再一次挖坑. 目录 1. 主从节点 2. Nuget自动包还原 3. powershell部署 4 ...
-
react native TypeError network request failed
如果使用fetch获取数据,用的是POST方法,注意headers要添加请求头.当请求为GET时不能用body,当为POST时必须包含body,设置头部之后就一切正常了. fetch(&q ...
-
怎么学习计算电磁学【QUORA】
链接 There are several resources. But it depends on what you actually want to learn...Let me explain: ...
-
iOS平台网络类型检测
老大新增个需求,连接服务器时要区分内外网,需求理解了,现实很骨感啊,没办法,干. #import <Foundation/Foundation.h> /* 依赖于以下Framework S ...
-
hdu 5311 Hidden String
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5311 Hidden String Description Today is the 1st anniv ...
-
-_-#【jQuery插件】Spinner 数字选择器
Spinner (jQuery UI)
-
解不定方程ax+by=m的最小解
给出方程a*x+b*y=c,其中所有数均是整数,且a,b,c是已知数,求满足那个等式的x,y值?这个方程可能有解也可能没解也可能有无穷多个解(注意:这里说的解都是整数解)? 既然如此,那我们就得找出有 ...
-
【原创】leetCodeOj --- Jump Game II 解题报告
原题地址: https://oj.leetcode.com/problems/jump-game-ii/ 题目内容: Given an array of non-negative integers, ...