深度学习框架keras入门案例,初学者轻松入门

什么是Keras?

Keras是深度学习框架,是一个高层神经网络API,Keras由纯Python编写而成并基Tensorflow、Theano以及CNTK后端。Keras 为支持快速实验而生,能够把你的idea迅速转换为结果,如果你是初学者,请选择Keras框架,带你初步了解深度神经网络框架,

案例:一个二维特征,影响一个函数值,例如函数 z=f(x,y) ,  x,y是自变量,z与x,y存在函数f的映射关系,下面要做的事情是,随机生成一个若干个点,他们之间符和某一种函数关系,我们事先不知道,现在要利用神经网络框架,通过训练,得到预测函数f(x,y)  ,使得预测结果接近真实的数值。

代码如下:

一、导入模型

import numpy as np
from tensorflow.keras.models import Sequential # 导入keras
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.optimizers import SGD
import tushare as ts
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 生成相应的数据函数
def get_beans4(counts):
xs = np.random.rand(counts,2)*2
ys = np.zeros(counts)
for i in range(counts):
x = xs[i]
if (np.power(x[0]-1,2)+np.power(x[1]-0.3,2))<0.5:
ys[i] = 1

return xs,ys

# 画出数据的散点图
def show_scatter(X, Y):
if X.ndim > 1:
show_3d_scatter(X, Y)
else:
plt.scatter(X, Y)
plt.show()

#画3d散点图
def show_3d_scatter(X,Y):
x = X[:,0]
z = X[:,1]
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x, z, Y)
plt.show()

# 画3D图
def show_scatter_surface_with_model(X, Y, model):
# model.predict(X)
x = X[:, 0]
z = X[:, 1]
y = Y

fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x, z, y)

x = np.arange(np.min(x), np.max(x), 0.1)
z = np.arange(np.min(z), np.max(z), 0.1)
x, z = np.meshgrid(x, z)

X = np.column_stack((x[0], z[0]))

for j in range(z.shape[0]):
if j == 0:
continue
X = np.vstack((X, np.column_stack((x[0], z[j]))))

y = model.predict(X)

# return
# y = model.predcit(X)
y = np.array([y])
y = y.reshape(x.shape[0], z.shape[1])
ax.plot_surface(x, z, y, cmap='rainbow')
plt.show()

二、显示3D图像

深度学习框架keras入门案例,初学者轻松入门_第1张图片

 三、建立神经网络模型与训练

m = 100 # 数据量
X, Y = get_beans4(m)
show_scatter(X, Y)
print(X)
print(X.shape)

建立网络模型:
model = Sequential()
model.add(Dense(units=10, activation='sigmoid', input_dim=2))
# units 神经元个数, activation激活函数类型, 输了特征维度
model.add(Dense(units=1, activation='sigmoid')) # 输出层
# 编译网络
model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.3), metrics=['accuracy'])
# mean_squared_error 均方误差 sgd 随机梯度下降算法 accuracy 准确度

# 训练回合数epochs, batch_size 批数量,一次训练利用多少样本
model.fit(X, Y, epochs=8000, batch_size=64)

训练中....

Epoch 1/8000
2/2 [==============================] - 0s 2ms/step - loss: 0.2580 - accuracy: 0.3900
Epoch 2/8000
2/2 [==============================] - 0s 2ms/step - loss: 0.2305 - accuracy: 0.7300
Epoch 3/8000
2/2 [==============================] - 0s 1ms/step - loss: 0.2135 - accuracy: 0.7300
Epoch 4/8000
2/2 [==============================] - 0s 998us/step - loss: 0.2050 - accuracy: 0.7300
Epoch 5/8000
2/2 [==============================] - 0s 998us/step - loss: 0.1995 - accuracy: 0.7300
Epoch 6/8000
2/2 [==============================] - 0s 960us/step - loss: 0.1964 - accuracy: 0.7300
Epoch 7/8000
2/2 [==============================] - 0s 998us/step - loss: 0.1946 - accuracy: 0.7300
Epoch 8/8000
2/2 [==============================] - 0s 2ms/step - loss: 0.1929 - accuracy: 0.7300
Epoch 9/8000
2/2 [==============================] - 0s 998us/step - loss: 0.1930 - accuracy: 0.7300
Epoch 10/8000
2/2 [==============================] - 0s 996us/step - loss: 0.1917 - accuracy: 0.7300
Epoch 11/8000
2/2 [==============================] - 0s 998us/step - loss: 0.1908 - accuracy: 0.7300
Epoch 12/8000
2/2 [==============================] - 0s 952us/step - loss: 0.1904 - accuracy: 0.7300
Epoch 13/8000
2/2 [==============================] - 0s 999us/step - loss: 0.1900 - accuracy: 0.7300
Epoch 14/8000
2/2 [==============================] - 0s 958us/step - loss: 0.1893 - accuracy: 0.7300
Epoch 15/8000
2/2 [==============================] - 0s 997us/step - loss: 0.1892 - accuracy: 0.7300
Epoch 16/8000
2/2 [==============================] - 0s 1ms/step - loss: 0.1885 - accuracy: 0.7300
Epoch 17/8000
2/2 [==============================] - 0s 997us/step - loss: 0.1880 - accuracy: 0.7300
Epoch 18/8000
2/2 [==============================] - 0s 999us/step - loss: 0.1879 - accuracy: 0.7300
Epoch 19/8000
2/2 [==============================] - 0s 1ms/step - loss: 0.1872 - accuracy: 0.7300
Epoch 20/8000
2/2 [==============================] - 0s 996us/step - loss: 0.1868 - accuracy: 0.7300
Epoch 21/8000
2/2 [==============================] - 0s 998us/step - loss: 0.1864 - accuracy: 0.7300
Epoch 22/8000
2/2 [==============================] - 0s 999us/step - loss: 0.1860 - accuracy: 0.7300
Epoch 23/8000
2/2 [==============================] - 0s 1ms/step - loss: 0.1855 - accuracy: 0.7300
Epoch 24/8000
2/2 [==============================] - 0s 999us/step - loss: 0.1852 - accuracy: 0.7300
Epoch 25/8000
2/2 [==============================] - 0s 997us/step - loss: 0.1849 - accuracy: 0.7300

......

四、预测结果

# 预测函数
pres = model.predict(X)
show_scatter_surface_with_model(X, Y, model) # 三维的

五、预测结果曲面图

深度学习框架keras入门案例,初学者轻松入门_第2张图片

你可能感兴趣的:(深度学习实战项目,机器学习实战项目,深度学习,keras,神经网络,机器学习,python)