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
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 ...
随机推荐
- 数据库连接池:Druid
转自: http://www.cnblogs.com/windlaughing/p/3287501.html Java连接数据库方法概述 java.sql提供了一些接口和类,用于支持数据库增删改查等相 ...
- log4j+mongodb
maven 配置: <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java ...
- [CC]LOD技术
ccGLWindow::paintGL() | ccGLWindow::fullRenderingPass(...) | ccGLWindow::drawBackground(context, ren ...
- MVC中的成员资格,授权,安全性
使用 Authorize 特性登录 Authorize 是 ASP.NET MVC 自带的默认授权过滤器, 可用来限制用户对操作方法的访问. 保护控制器操作 Authorize 特性在表单身份验证和 ...
- jsp作为服务端,ajax请求回应
刚学ajax,想以jsp作为服务端,来回应ajax的请求: 代码如下: <!DOCTYPE html> <html> <head lang="en"& ...
- Linux opencv安装与编译
参考http://blog.csdn.net/solomon1558/article/details/51967280 1安装cmake以及依赖库 $ sudo apt-get install cma ...
- WebForm 内置对象2
Session: 与Cookies相比 相同点:每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 以上所有内容,都跟cookies一样 不同点: 1.Sess ...
- LeetCode: Ransom Note
public class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] rans ...
- 关于nodejs能同时接受多少个请求的问题?////zzz
关于nodejs能同时接受多少个请求的问题? 最近学习node,看了很多教程,都在赞扬nodejs的异步I/O,异步I/O的特点就是,每接收一个请求,使用异步调用处理请求,不用等待结果,可以继续运行其 ...
- HTML5--拖动02-dragstart、drag、dragenter、dragover、dragleave、drop、dragend属性
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...