keras/theanokeras训练自己的数据速度为什么这么慢

theano和keras使用过程中遇到的一些问题记录 - 清晨的天空
来源:互联网
编辑:刘梓楠
有网友碰到过这样的问题:theano和keras使用过程中遇到的一些问题记录 - 清晨的天空,问题详细内容为:挺不错的博文:theano和keras使用过程中遇到的一些问题记录 - 清晨的天空,我搜你通过互联网收集了相关的一些解决方案,希望对有过相同或者相似问题的网友提供帮助,具体如下:
1.使用theano调用relu激活函数提示''It looks like your version of theano is out of date''
bug信息显然是提示更新theano版本,可是一看官网最新版本就是0.7.0,使用pip更新后也提示已经是最新版本。
经过检查后发现,原来theano有两个版本,一个是稳定版,通过&pip install theano获得的就是稳定版,还有一个叫做&bleeding-edge version&,即最新版,必须通过git更新。命令如下pip install git+git:///Theano/Theano.git --upgrade --no-deps(&no-deps的意思是单独安装theano,各种依赖我已经装好了),注意电脑上必须先装有git.
2.内存足够却还是提示内存不足
MemoryError: Error allocating
bytes of device memory (an illegal memory access was encountered).&Apply node that caused the error: GpuElemwise{mul,no_inplace}(CudaNdarrayConstant{error while transferring the value: error copying data to host}, GpuDimShuffle{x,x}.0, image_hidden_w)&Inputs types: [CudaNdarrayType(float32, (True, True)), CudaNdarrayType(float32, (True, True)), CudaNdarrayType(float32, matrix)]&
方法还是将theano更新到开发者版本
记录两个命令:
pip install --upgrade --no-deps git+ git:///fchollet/keras.git
pip install --upgrade --no-deps git+git:///Theano/Theano.git
  分别是更新最新版本theano和keras
调用gpu运行程序
CUDA_LAUNCH_BLOCKING=1 THEANO_FLAGS=device=gpu,floatX=float32,cuda.root=/usr/local/cuda-6.5/ python code.py&
posted @ 请注意,本站信息均收集自互联网,相关信息仅供参考,医疗等重要信息请以正规途径为最终意见,本站不承担任何责任!
您可能还关注18:22:40 UTC
@高源_robot_learning:Keras是我见到的最好的深度学习框架,设计简直可以算是史诗级别.准备用Keras代替nntools
There is a 99% probability that the idea you're implementing is simple. This simplicity should be refle else redo.
Keras: Theano-based Deep Learning library
You have just found Keras.
Keras is a minimalist, highly modular neural network library in the spirit of Torch, written in Python / Theano so as not to have to deal with the dearth of ecosystem in Lua. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research.
Use Keras if you need a deep learning library that:- allows for easy and fast prototyping (through total modularity, minimalism, and extensibility).- supports both convolutional networks (for vision) and recurrent networks (for sequence data). As well as combinations of the two. - runs seamlessly on the CPU and the GPU.
Read the documentation at .
Keras is compatible with Python 2.7-3.4.
Guiding principles
Modularity. A model is understood as a sequence of standalone, fully-configurable modules that can be plugged together with as little restrictions as possible. In particular, neural layers, cost functions, optimizers, initialization schemes, activation functions and dropout are all standalone modules that you can combine to create new models.
Minimalism. Each module should be kept short and simple (&100 lines of code). Every piece of code should be transparent upon first reading. No black magic: it hurts iteration speed and ability to innovate.
Easy extensibility. New features (a new module, per the above definition, or a new way to combine modules together) are dead simple to add (as new classes/functions), and existing modules provide ample examples.
Work with Python. No separate models configuration files in a declarative format (like in Caffe or PyLearn2). Models are described in Python code, which is compact, easier to debug, benefits from syntax highlighting, and most of all, allows for ease of extensibility. See for yourself with the examples below.
Multilayer Perceptron (MLP):
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
model = Sequential()
model.add(Dense(20, 64, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, 64, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, 1, init='uniform'))
model.add(Activation('softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
pile(loss='mean_squared_error', optimizer=sgd)
model.fit(X_train, y_train, nb_epoch=20, batch_size=16)
score = model.evaluate(X_test, y_test, batch_size=16)
Alternative implementation of MLP:
model = Sequential()
model.add(Dense(20, 64, init='uniform', activation='tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, 64, init='uniform', activation='tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, 1, init='uniform', activation='softmax')
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
pile(loss='mean_squared_error', optimizer=sgd)
VGG-like convnet:
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD
model = Sequential()
model.add(Convolution2D(32, 3, 3, 3, border_mode='full'))
model.add(Activation('relu'))
model.add(Convolution2D(32, 32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(64, 32, 3, 3, border_mode='full'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64*8*8, 256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(256, 10))
model.add(Activation('softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
pile(loss='categorical_crossentropy', optimizer=sgd)
model.fit(X_train, Y_train, batch_size=32, nb_epoch=1)
Sequence classification with LSTM:
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Embedding
from keras.layers.recurrent import LSTM
model = Sequential()
model.add(Embedding(max_features, 256))
model.add(LSTM(256, 128, activation='sigmoid', inner_activation='hard_sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(128, 1))
model.add(Activation('sigmoid'))
pile(loss='binary_crossentropy', optimizer='rmsprop')
model.fit(X_train, Y_train, batch_size=16, nb_epoch=10)
score = model.evaluate(X_test, Y_test, batch_size=16)
Architecture for learning image captions with a convnet and a Gated Recurrent Unit:
(word-level embedding, caption of maximum length 16 words).
Note that getting this to actually "work" will require using a bigger convnet, initialized with pre-trained weights.Displaying readable results will also require an embedding decoder.
max_caption_len = 16
model = Sequential()
model.add(Convolution2D(32, 3, 3, 3, border_mode='full'))
model.add(Activation('relu'))
model.add(Convolution2D(32, 32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Convolution2D(64, 32, 3, 3, border_mode='full'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Convolution2D(128, 64, 3, 3, border_mode='full'))
model.add(Activation('relu'))
model.add(Convolution2D(128, 128, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Flatten())
model.add(Dense(128*4*4, 256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Repeat(max_caption_len))
# the GRU below returns sequences of max_caption_len vectors of size 256 (our word embedding size)
model.add(GRU(256, 256, return_sequences=True))
pile(loss='mean_squared_error', optimizer='rmsprop')
# "images" is a numpy array of shape (nb_samples, nb_channels=3, width, height)
# "captions" is a numpy array of shape (nb_samples, max_caption_len=16, embedding_dim=256)
# captions are supposed already embedded (dense vectors).
model.fit(images, captions, batch_size=16, nb_epoch=100)
In the examples folder, you will find example models for real datasets:- CIFAR10 small images classification: Convnet with realtime data augmentation- IMDB movie review sentiment classification: LSTM over sequences of words- Reuters newswires topic classification: Multilayer Perceptron
Current capabilities
For complete coverage of the API, check out .
A few highlights: convnets, LSTM, GRU, word2vec-style embeddings, PReLU, batch normalization...
Installation
Keras uses the following dependencies:
numpy, scipy
See installation instructions:
h5py (optional, required if you use model saving/loading functions)
Optional but recommended if you use CNNs: cuDNN.
Once you have the dependencies installed, cd to the Keras folder and run the install command:
sudo python setup.py install
Why this name, Keras?
Keras (κ?ρα?) means horn in Greek. It is a reference to a literary image from ancient Greek and Latin literature, first found in the Odyssey, where dream spirits (Oneiroi, singular Oneiros) are divided between those who deceive men with false visions, who arrive to Earth through a gate of ivory, and those who announce a future that will come to pass, who arrive through a gate of horn. It's a play on the words κ?ρα? (horn) / κρα?νω (fulfill), and ?λ?φα? (ivory) / ?λεφα?ρομαι (deceive).
Keras was developed as part of the research effort of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System).
"Oneiroi are beyond our unravelling --who can be sure what tale they tell? Not all that men look for comes to pass. Two gates there are that give passage to fleeting O one is made of horn, one of ivory. The Oneiroi that pass through sawn ivory are deceitful, bearing a message that w those that come out through polished horn have truth behind them, to be accomplished for men who see them." Homer, Odyssey 19. 562 ff (Shewring translation).基于Theano的深度学习(Deep Learning)框架Keras学习随笔-03-优化器
原地址:http://blog.csdn.net/niuwei22007/article/details/可以查看更多文章
optimizers(优化器)。机器学习包括两部分内容,一部分是如何构建模型,另一部分就是如何训练模型。训练模型就是通过挑选最佳的优化器去训练出最优的模型。 Keras包含了很多优化方法。比如最常用的随机梯度下降法(SGD),还有Adagrad、Adadelta、RMSprop、Adam等。下面通过具体的代码介绍一下优化器的使用方法。最重要的就是SGD,其他的优化器有兴趣的可以自行了解一下。 一、优化器的使用方法 在编译一个Keras模型时,优化器是2个参数之一(另外一个是损失函数)。看如下代码:
[python] view plain copymodel=Sequential() model.add(Dense(64,init='uniform',input_dim=10)) model.add(Activation('tanh')) model.add(Activation('softmax'))
sgd=SGD(lr=0.1,decay=1e-6,momentum=0.9,nesterov=True) <pile(loss='mean_squared_error',optimizer=sgd)这个例子中是在调用compile之前实例化了一个优化器。我们也可以通过传递名字的方式调用默认的优化器。代码如下:
[python] view plain copy#passoptimizerbyname:defaultparameterswillbeused <pile(loss='mean_squared_error',optimizer='sgd')二、基类
[python] view plain copykeras.optimizers.Optimizer(**kwargs)所有的优化器都继承自上面这个带有keyword参数的类。当然基类是不能用来训练模型的。 三、SGD(随机梯度下降优化器,性价比最好的算法)
[python] view plain copykeras.optimizers.SGD(lr=0.01,momentum=0.,decay=0.,nesterov=False)参数:
lr :float>=0,学习速率
momentum :float>=0 参数更新的动量
decay : float>=0 每次更新后学习速率的衰减量
nesterov :Boolean 是否使用Nesterov动量项
四、Adagrad(参数推荐使用默认值)
[python] view plain copykeras.optimizers.Adagrad(lr=0.01,epsilon=1e-6)参数:
lr : float>=0,学习速率
epsilon :float>=0
五、Adadelta(参数推荐使用默认值)
[python] view plain copykeras.optimizers.Adadelta(lr=1.0,rho=0.95,epsilon=1e-6)参数:
lr :float>=0,学习速率
rho : float>=0
epsilon :float>=0 模糊因子
六、RMSprop(参数推荐使用默认值)
[python] view plain copykeras.optimizers.RMSprop(lr=0.001,rho=0.9,epsilon=1e-6)参数:
lr:float>=0,学习速率
rho : float>=0
epsilon : float>=0 模糊因子
七、Adam(参数推荐使用默认值)
[python] view plain copykeras.optimizers.Adam(lr=0.001,beta_1=0.9,beta_2=0.999,epsilon=1e-8)参数:
lr : float>=0,学习速率
beta_1, beta_2:floats, 0 < beta < 1 通常都是接近于1
epsilon :float>=0 模糊因子
参考资料:
/entry/188683
转载来自:http://blog.csdn.net/niuwei22007/article/details/
最新教程周点击榜
微信扫一扫&#160;基于Theano的深度学习(Deep Learning)框架Keras学习随笔-03-优化器
&#160;基于Theano的深度学习(Deep Learning)框架Keras学习随笔-03-优化器
& & & & 原地址:可以查看更多文章
optimizers(优化器)。机器学习包括两部分内容,一部分是如何构建模型,另一部分就是如何训练模型。训练模型就是通过挑选最佳的优化器去训练出最优的模型。
& & & & Keras包含了很多优化方法。比如最常用的随机梯度下降法(SGD),还有Adagrad、Adadelta、RMSprop、Adam等。下面通过具体的代码介绍一下优化器的使用方法。最重要的就是SGD,其他的优化器有兴趣的可以自行了解一下。
一、优化器的使用方法
& & &在编译一个Keras模型时,优化器是2个参数之一(另外一个是损失函数)。看如下代码:
model&=&Sequential()&&model.add(Dense(64,&init='uniform',&input_dim=10))&&model.add(Activation('tanh'))&&model.add(Activation('softmax'))&&&&&sgd&=&SGD(lr=0.1,&decay=1e-6,&momentum=0.9,&nesterov=True)&&<pile(loss='mean_squared_error',&optimizer=sgd)&&
& & &这个例子中是在调用compile之前实例化了一个优化器。我们也可以通过传递名字的方式调用默认的优化器。代码如下:
#&passoptimizer&by&name:&default&parameters&will&be&used&&<pile(loss='mean_squared_error',&optimizer='sgd')&&
keras.optimizers.Optimizer(**kwargs)&&
& & &所有的优化器都继承自上面这个带有keyword参数的类。当然基类是不能用来训练模型的。
三、SGD(随机梯度下降优化器,性价比最好的算法)
keras.optimizers.SGD(lr=0.01,&momentum=0.,&decay=0.,&nesterov=False)&&
& & &参数:
lr :float&=0,学习速率momentum :float&=0 参数更新的动量decay : float&=0 每次更新后学习速率的衰减量nesterov :Boolean 是否使用Nesterov动量项
四、Adagrad(参数推荐使用默认值)&
keras.optimizers.Adagrad(lr=0.01,&epsilon=1e-6)&&
& & &参数:
lr : float&=0,学习速率epsilon :float&=0
五、Adadelta(参数推荐使用默认值)
keras.optimizers.Adadelta(lr=1.0,&rho=0.95,&epsilon=1e-6)&&
& & &参数:
lr :float&=0,学习速率rho : float&=0epsilon :float&=0 模糊因子
六、RMSprop(参数推荐使用默认值)
keras.optimizers.RMSprop(lr=0.001,&rho=0.9,&epsilon=1e-6)&&
& & 参数:
lr:float&=0,学习速率rho : float&=0epsilon : float&=0 模糊因子
七、Adam(参数推荐使用默认值)
keras.optimizers.Adam(lr=0.001,&beta_1=0.9,&beta_2=0.999,&epsilon=1e-8)&&
& & &参数:
lr : float&=0,学习速率beta_1, beta_2:floats, 0 & beta & 1 通常都是接近于1epsilon :float&=0 模糊因子&
参考资料:
发表评论:
馆藏&34601
TA的推荐TA的最新馆藏[转]&1446人阅读
原地址可以查看更多文章
本文主要介绍一下Keras的问答部分,其实很简单,后边可能不会详细说到,提前凉一下,便于翻看。
Keras介绍:
& & & &&Keras是一个极度简化、高度模块化的神经网络第三方库。基于Python&#43;Theano开发,充分发挥了GPU和CPU操作。其开发目的是为了更快的做神经网络实验。适合前期的网络原型设计、支持卷积网络和反复性网络以及两者的结果、支持人工设计的其他网络、在GPU和CPU上运行能够无缝连接。
No.0 windows用户如何安装Keras框架?
& & & & 用pip方式安装很简单。即打开cmd,输入pip install keras,然后等待安装完毕即可。会自动安装需要的组件。如果pip命令失败,则建议按照去安装Python&#43;Theano。然后再安装Keras即可。。
No.1怎么保存Keras模型?
不推荐使用pickle或cPickle。
(1) 如果只保存模型结构,代码如下:
(2) 如果需要保存数据:
No.2为什么训练损失比测试损失要大?
Keras有两种模型:训练和测试。规则化,比如Dropout和L1/L2,在测试时关闭了。
另外,训练损失是每一次训练batch的平均损失。模型因为在时刻变化,最开始的batch损失肯定要比最后的batches损失要高。另一方面,每一次epoch损失使用最后的epoch计算,因此返回的结果就比较小。
&No.3如何将中间层的输出可视化?
通过Theano function的output。示例如下:
No.4如何用Keras处理不适合存放在内存中的数据集?
Batch trainingusingmodel.train_on_batch(X, y)&和model.test_on_batch(X, y)参考文档:。
You can also see batch training in action inour&.
&No.5当验证损失不再继续降低时,如何中断训练?
用EarlyStopping回调函数,代码如下:
参考文档:
&No.6在训练时,数据会被随机打乱吗?
如果model.fit中的参数suffle=True时,会随机打算每一次epoch的数据。(默认打乱)
但是验证数据默认不会打乱。
&No.7如何记录每一次epoch的训练/验证损失/准确度?
Model.fit函数会返回一个 History 回调,该回调有一个属性history包含一个封装有连续损失/准确的lists。代码如下:
No.8如何让我的Keras脚本每次产生确定的数据?
在引入Kerans之前,引入numpy,并且用其random.seed(种子)产生一个随机数对象。这样在相同硬件的机器上运行时,每次产生的随机数的顺序都是一样的。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:16251次
排名:千里之外
原创:26篇
转载:20篇
(2)(1)(5)(1)(12)(4)(6)(5)(11)}

我要回帖

更多关于 基于theano的keras 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信