Keras(1):Keras安装与简介

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mmc2015/article/details/50976776

先安装上再说:

sudo pip install 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


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

import numpy
import theano
import theano.tensor as T
rng = numpy.random

N = 400                                   # training sample size
feats = 784                               # number of input variables

# generate a dataset: D = (input_values, target_class)
D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
training_steps = 10000

# Declare Theano symbolic variables
x = T.matrix("x")
y = T.vector("y")

# initialize the weight vector w randomly
#
# this and the following bias variable b
# are shared so they keep their values
# between training iterations (updates)
w = theano.shared(rng.randn(feats), name="w")

# initialize the bias term
b = theano.shared(0., name="b")

print("Initial model:")
print(w.get_value())
print(b.get_value())

# Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b))   # Probability that target = 1
prediction = p_1 > 0.5                    # The prediction thresholded
xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function
cost = xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize
gw, gb = T.grad(cost, [w, b])             # Compute the gradient of the cost
                                          # w.r.t weight vector w and
                                          # bias term b
                                          # (we shall return to this in a
                                          # following section of this tutorial)

# Compile
train = theano.function(
          inputs=[x,y],
          outputs=[prediction, xent],
          updates=((w, w - 0.1 * gw), (b, b - 0.1 * gb)))
predict = theano.function(inputs=[x], outputs=prediction)

# Train
for i in range(training_steps):
    pred, err = train(D[0], D[1])

print("Final model:")
print(w.get_value())
print(b.get_value())
print("target values for D:")
print(D[1])
print("prediction on D:")
print(predict(D[0]))


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

0)预处理数据

# generate a dataset: D = (input_values, target_class)

1)定义变量

# Declare Theano symbolic variables

2)构建(图)模型

# Construct Theano expression graph

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

# Compile

4)训练模型

5)预测新数据

# Train

print(predict(D[0]))


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

http://keras.io/


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

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



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

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

from keras.models import Sequential
model = Sequential() #1定义变量

from keras.layers.core import Dense, Activation
model.add(Dense(output_dim=64, input_dim=100, init="glorot_uniform")) #2构建图模型
model.add(Activation("relu"))
model.add(Dense(output_dim=10, init="glorot_uniform"))
model.add(Activation("softmax"))

from keras.optimizers import SGD
model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.01, momentum=0.9, nesterov=True)) #3编译模型

model.fit(X_train, Y_train, nb_epoch=5, batch_size=32) #4训练模型

objective_score = model.evaluate(X_test, Y_test, batch_size=32)

classes = model.predict_classes(X_test, batch_size=32) #5预测模型
proba = model.predict_proba(X_test, batch_size=32)


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






阅读更多
换一批

没有更多推荐了,返回首页