贼简单!5分钟入门Python深度学习库Keras

上面学习了在Window和Linux上安装keras环境。既然装了,下面花5分钟学习如何入门使用keras,很简单,不要怕!就像搭积木一样简单。

6313830-4828517a44ab28be
贼简单!5分钟入门Python深度学习库Keras

搭积木一样简单

Keras深度学习基础

Keras的主要结构是模型,它定义了深度学习网络的图层解雇。可以像搭积木一样,向现有模型添加更多图层,以构建项目所需的自定义模型。

以下是如何在深度学习中创建顺序模型和一些常用层

1.顺序模型

from keras.models import Sequential

from keras.layers import Dense, Activation,Conv2D,MaxPooling2D,Flatten,Dropout

model = Sequential()

2.卷积层

这是卷积层作为输入层的示例,输入形状为320x320x3,具有48个大小为3x3的滤波器,并使用ReLU作为激活函数。

input_shape=(320,320,3) #this is the input shape of an image 320x320x3

model.add(Conv2D(48, (3, 3), activation='relu', input_shape= input_shape))

另一种类型是

model.add(Conv2D(48, (3, 3), activation='relu'))

6313830-d57ef2dad41ccd02
贼简单!5分钟入门Python深度学习库Keras

卷积层

3. MaxPooling Layer

要对输入表示进行下采样,请使用MaxPool2d并指定内核大小

model.add(MaxPooling2D(pool_size=(2, 2)))

6313830-95ca9e1956a3f046
贼简单!5分钟入门Python深度学习库Keras

输入表示进行下采样

4.Dense Layer

添加完全连接的图层,只需指定输出尺寸

model.add(Dense(256,activation ='relu'))

5.DropOut层

以50%的概率添加DropOut层

model.add(Dropout(0.5))

编译,培训和评估

在定义模型之后,开始训练它们。首先需要使用loss函数和优化器函数编译网络。这将允许网络改变权重并最小化损失。

model.compile(loss ='mean_squared_error',optimizer ='adam')

现在开始训练,使用fit将训练和验证数据提供给模型。这将允许您批量训练网络并设置epochs。

model.fit(X_train,X_train,batch_size = 32,epochs = 10,validation_data =(x_val,y_val))

最后一步是使用测试数据评估模型。

score = model.evaluate(x_test,y_test,batch_size = 32)

让我们尝试使用简单的线性回归

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import keras
from keras.models import Sequential
from keras.layers import Dense, Activation
import numpy as np
import matplotlib.pyplot as plt

x = data = np.linspace(1,2,200)
y = x4 + np.random.randn(x.shape) * 0.3
model = Sequential()
model.add(Dense(1, input_dim=1, activation='linear'))
model.compile(optimizer='sgd', loss='mse', metrics=['mse'])
weights = model.layers[0].get_weights()
w_init = weights[0][0][0]
b_init = weights[1][0]
print('Linear regression model is initialized with weights w: %.2f, b: %.2f' % (w_init, b_init))
model.fit(x,y, batch_size=1, epochs=30, shuffle=False)
weights = model.layers[0].get_weights()
w_final = weights[0][0][0]
b_final = weights[1][0]
print('Linear regression model is trained to have weight w: %.2f, b: %.2f' % (w_final, b_final))
predict = model.predict(data)
plt.plot(data, predict, 'b', data , y, 'k.')
plt.show()

训练数据后,输出应如下所示

6313830-410d5510b3ffd4da
贼简单!5分钟入门Python深度学习库Keras

初始权重

Linear regression model is initialized with weights w: 0.37, b: 0.00

和最终的权重

Linear regression model is trained to have weight w: 3.70, b: 0.61

转载于:https://www.jianshu.com/p/5c9a77f1b6df

猜你喜欢

转载自blog.csdn.net/weixin_34378045/article/details/91264117