Keras Notes: Keras安装与简介

来源:互联网 发布:java response json 编辑:程序博客网 时间:2024/05/22 17:07

reference: http://blog.csdn.net/mmc2015/article/details/50976776

先安装上再说:

sudo pipinstall keras


或者手动安装:

下载:Git clone git://github.com/fchollet/keras.git

传到相应机器上

安装:cd to the Keras folder and run the install command:

sudo python setup.py install



keras在theano之上,在学习keras之前,先理解了这几篇内容:

http://blog.csdn.NET/mmc2015/article/details/42222075(LR)

http://www.deeplearning.Net/tutorial/gettingstarted.html和http://www.deeplearning.net/tutorial/logreg.html(Classifying MNIST digits using Logistic Regression

总参考:http://www.deeplearning.net/tutorial/contents.html


以第一个链接中给出的代码为例(比较简单):

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import numpy  
  2. import theano  
  3. import theano.tensor as T  
  4. rng = numpy.random  
  5.   
  6. N = 400                                   # training sample size  
  7. feats = 784                               # number of input variables  
  8.   
  9. # generate a dataset: D = (input_values, target_class)  
  10. D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))  
  11. training_steps = 10000  
  12.   
  13. # Declare Theano symbolic variables  
  14. x = T.matrix("x")  
  15. y = T.vector("y")  
  16.   
  17. # initialize the weight vector w randomly  
  18. #  
  19. # this and the following bias variable b  
  20. # are shared so they keep their values  
  21. # between training iterations (updates)  
  22. w = theano.shared(rng.randn(feats), name="w")  
  23.   
  24. # initialize the bias term  
  25. b = theano.shared(0., name="b")  
  26.   
  27. print("Initial model:")  
  28. print(w.get_value())  
  29. print(b.get_value())  
  30.   
  31. # Construct Theano expression graph  
  32. p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b))   # Probability that target = 1  
  33. prediction = p_1 > 0.5                    # The prediction thresholded  
  34. xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function  
  35. cost = xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize  
  36. gw, gb = T.grad(cost, [w, b])             # Compute the gradient of the cost  
  37.                                           # w.r.t weight vector w and  
  38.                                           # bias term b  
  39.                                           # (we shall return to this in a  
  40.                                           # following section of this tutorial)  
  41.   
  42. # Compile  
  43. train = theano.function(  
  44.           inputs=[x,y],  
  45.           outputs=[prediction, xent],  
  46.           updates=((w, w - 0.1 * gw), (b, b - 0.1 * gb)))  
  47. predict = theano.function(inputs=[x], outputs=prediction)  
  48.   
  49. # Train  
  50. for i in range(training_steps):  
  51.     pred, err = train(D[0], D[1])  
  52.   
  53. print("Final model:")  
  54. print(w.get_value())  
  55. print(b.get_value())  
  56. print("target values for D:")  
  57. print(D[1])  
  58. print("prediction on D:")  
  59. print(predict(D[0]))  


我们发现,使用theano构建模型一般需要如下步骤:

0)预处理数据

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # generate a dataset: D = (input_values, target_class)  

1)定义变量

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # Declare Theano symbolic variables  

2)构建(图)模型

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # Construct Theano expression graph  

3)编译模型,theano.function()

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # Compile  

4)训练模型

5)预测新数据

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # Train  

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. print(predict(D[0]))  


那么,theano和keras区别在哪呢?

http://keras.io/


原来是层次不同,keras封装的更好,编程起来更方便(调试起来更麻烦了。。);theano编程更灵活,自定义完全没问题,适合科研人员啊。

另外,keras和tensorFlow完全兼容。。。



keras有两种模型,序列和图,不解释。

我们看下keras构建模型有多快,以序列为例:

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. from keras.models import Sequential  
  2. model = Sequential() #1定义变量  
  3.   
  4. from keras.layers.core import Dense, Activation  
  5. model.add(Dense(output_dim=64, input_dim=100, init="glorot_uniform")) #2构建图模型  
  6. model.add(Activation("relu"))  
  7. model.add(Dense(output_dim=10, init="glorot_uniform"))  
  8. model.add(Activation("softmax"))  
  9.   
  10. from keras.optimizers import SGD  
  11. model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.01, momentum=0.9, nesterov=True)) #3编译模型  
  12.   
  13. model.fit(X_train, Y_train, nb_epoch=5, batch_size=32#4训练模型  
  14.   
  15. objective_score = model.evaluate(X_test, Y_test, batch_size=32)  
  16.   
  17. classes = model.predict_classes(X_test, batch_size=32#5预测模型  
  18. proba = model.predict_proba(X_test, batch_size=32)  


最后给出keras架构,自己去学吧:

0 0
原创粉丝点击