【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建

你好,我是小航,一个正在变秃、变强的文艺倾年。
笔记来自B站UP主Ele实验室的《小白也能听懂的人工智能原理》。
本文讲解初识Keras:轻松完成神经网络模型搭建,一起卷起来叭!

目录

  • 一、“矩阵、向量”
  • 二、Keras
  • 三、代码实现

一、“矩阵、向量”

【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建_第1张图片

在之前我们都是将每个元素做运算,这样做看着费时费力,现在我们从万物皆向量的角度重新看待这个问题,用向量的运算去表示线性函数。

【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建_第2张图片
【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建_第3张图片
现在我们使用代码来实现以上过程:

之前的代码:

import numpy as np
import dataset
import plot_utils

m = 100
xs, ys = dataset.get_beans(m)
print(xs)
print(ys)

plot_utils.show_scatter(xs, ys)

w1 = 0.1
w2 = 0.2
b = 0.1

## [[a,b][c,d]]
## x1s[a,c]
## x2s[b,d]
## 逗号,区分的是维度,冒号:区分的是索引,省略号… 用来代替全索引长度
# 在所有的行上,把第0列切割下来形成一个新的数组
x1s = xs[:, 0]
x2s = xs[:, 1]

# 前端传播
def forward_propgation(x1s, x2s):
    z = w1 * x1s + w2 * x2s + b
    a = 1 / (1 + np.exp(-z))
    return a

plot_utils.show_scatter_surface(xs, ys, forward_propgation)

for _ in range(500):
    for i in range(m):
        x = xs[i] ## 豆豆特征
        y = ys[i] ## 豆豆是否有毒
        x1 = x[0]
        x2 = x[1]

        a = forward_propgation(x1, x2)

        e = (y - a) ** 2

        deda = -2 * (y - a)
        dadz = a * (1 - a)
        dzdw1 = x1
        dzdw2 = x2
        dzdb = 1

        dedw1 = deda * dadz * dzdw1
        dedw2 = deda * dadz * dzdw2
        dedb = deda * dadz * dzdb

        alpha = 0.01
        w1 = w1 - alpha * dedw1
        w2 = w2 - alpha * dedw2
        b = b - alpha * dedb

plot_utils.show_scatter_surface(xs, ys, forward_propgation)

使用向量矩阵后的代码:

import numpy as np
import dataset
import plot_utils

m = 100
X, Y = dataset.get_beans(m)
print(X)
print(Y)

plot_utils.show_scatter(X, Y)

# w1 = 0.1
# w2 = 0.2
W = np.array([0.1, 0.1])
# b = 0.1
B = np.array([0.1])

# 前端传播
def forward_propgation(X):
    # z = w1 * x1s + w2 * x2s + b
    # ndarray的dot函数:点乘运算
    # ndarray的T属性:转置运算
    Z = X.dot(W.T) + B
    # a = 1 / (1 + np.exp(-z))
    A = 1 / (1 + np.exp(-Z))
    return A

plot_utils.show_scatter_surface(X, Y, forward_propgation)

for _ in range(500):
    for i in range(m):
        Xi = X[i] ## 豆豆特征
        Yi = Y[i] ## 豆豆是否有毒

        A = forward_propgation(Xi)

        E = (Yi - A) ** 2

        dEdA = -2 * (Yi - A)
        dAdZ = A * (1 - A)
        dZdW = Xi
        dZdB = 1

        dEdW = dEdA * dAdZ * dZdW
        dEdB = dEdA * dAdZ * dZdB

        alpha = 0.01
        W = W - alpha * dEdW
        B = B - alpha * dEdB

plot_utils.show_scatter_surface(X, Y, forward_propgation)

二、Keras

【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建_第4张图片
诚如你所见,你恰好发现了 Keras。Keras 是一个用 Python 编写的高级神经网络 API,它能够以 TensorFlow, CNTK, 或者 Theano 作为后端运行。Keras 的开发重点是支持快速的实验。能够以最小的时延把你的想法转换为实验结果,是做好研究的关键。

参考链接:Keras官网

环境准备:

安装Tensorflow和Keras:

pip install keras -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install tensorflow -i https://pypi.mirrors.ustc.edu.cn/simple/

参考链接:tensorflow和keras版本对应关系

如果遇到ddl缺失问题,我们只需要去微软的官网下载一个Visual C++软件包:参考链接

【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建_第5张图片

三、代码实现

我们对上述过程代码实现:

豆豆数据集模拟:dataset.py

import numpy as np

def get_beans(counts):
	xs = np.random.rand(counts,2)*2
	ys = np.zeros(counts)
	for i in range(counts):
		x = xs[i]
		if (x[0]-0.5*x[1]-0.1)>0:
			ys[i] = 1
	return xs,ys

def get_beans1(counts):
	xs = np.random.rand(counts)
	xs = np.sort(xs)
	ys = np.zeros(counts)
	for i in range(counts):
		x = xs[i]
		yi = 0.7*x+(0.5-np.random.rand())/50+0.5
		if yi > 0.8:
			ys[i] = 1
		else:
			ys[i] = 0
	return xs,ys

def get_beans2(counts):
	xs = np.random.rand(counts)*2
	xs = np.sort(xs)
	ys = np.zeros(counts)
	for i in range(counts):
		x = xs[i]
		yi = 0.7*x+(0.5-np.random.rand())/50+0.5
		if yi > 0.8 and yi < 1.4:
			ys[i] = 1


	return xs,ys

def get_beans3(counts):
	xs = np.random.rand(counts)*2
	xs = np.sort(xs)
	ys = np.zeros(counts)
	for i in range(counts):
		x = xs[i]
		yi = 0.7*x+(0.5-np.random.rand())/50+0.5
		if yi > 0.8 and yi < 1.4:
			ys[i] = 1

		if yi > 1.6 and yi < 1.8:
			ys[i] = 1
	return xs,ys

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

绘图工具:plot_utils.py

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from keras.models import Sequential#导入keras


def show_scatter_curve(X,Y,pres):
	plt.scatter(X, Y) 
	plt.plot(X, pres) 
	plt.show()

def show_scatter(X,Y):
	if X.ndim>1:
		show_3d_scatter(X,Y)
	else:
		plt.scatter(X, Y) 
		plt.show()


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()

def show_surface(x,z,forward_propgation):
	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)
	y = forward_propgation(X)
	fig = plt.figure()
	ax = Axes3D(fig)
	ax.plot_surface(x, z, y, cmap='rainbow')
	plt.show()



def show_scatter_surface(X,Y,forward_propgation):
	if type(forward_propgation) == Sequential:
		show_scatter_surface_with_model(X,Y,forward_propgation)
		return
	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]))))

	r = forward_propgation(X)
	y = r[0]
	if type(r) == np.ndarray:
		y = r

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

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()

def pre(X,Y,model):
	model.predict(X)

第一批豆豆:

import dataset
import plot_utils
from keras.models import Sequential
from keras.layers import Dense

m = 100
X, Y = dataset.get_beans1(m)
plot_utils.show_scatter(X, Y)

model = Sequential()
# 当前层神经元的数量为1,激活函数类型:sigmoid,输入数据特征维度:1
model.add(Dense(units=1, activation='sigmoid', input_dim=1))
# loss(损失函数、代价函数):mean_squared_error均方误差;
# optimizer(优化器):sgd(随机梯度下降算法);
# metrics(评估标准):accuracy(准确度);
model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['accuracy'])
# epochs:回合数(全部样本完成一次训练)、batch_size:批数量(一次训练使用多少个样本)
model.fit(X, Y, epochs=5000, batch_size=10)

pres = model.predict(X)

plot_utils.show_scatter_curve(X, Y, pres)

训练结果:
【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建_第6张图片
第二批豆豆:

import dataset
import plot_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD

m = 100
X, Y = dataset.get_beans2(m)
plot_utils.show_scatter(X, Y)

model = Sequential()
model.add(Dense(units=2, activation='sigmoid', input_dim=1))
model.add(Dense(units=1, activation='sigmoid'))
# 调整学习率为0.05
model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.05), metrics=['accuracy'])
model.fit(X, Y, epochs=5000, batch_size=10)

pres = model.predict(X)

plot_utils.show_scatter_curve(X, Y, pres)

训练结果:
【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建_第7张图片
第三批豆豆:

import dataset
import plot_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD

m = 100
X, Y = dataset.get_beans(m)
plot_utils.show_scatter(X, Y)

model = Sequential()
# 当前层神经元的数量为1,激活函数类型:sigmoid,输入数据特征维度:2
model.add(Dense(units=1, activation='sigmoid', input_dim=2))
# loss(损失函数、代价函数):mean_squared_error均方误差;
# optimizer(优化器):sgd(随机梯度下降算法);
# metrics(评估标准):accuracy(准确度);
model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.05), metrics=['accuracy'])
# epochs:回合数(全部样本完成一次训练)、batch_size:批数量(一次训练使用多少个样本)
model.fit(X, Y, epochs=5000, batch_size=10)

pres = model.predict(X)

plot_utils.show_scatter_surface(X, Y, model)

豆豆毒性分布:
【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建_第8张图片
训练结果:
【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建_第9张图片
第四批豆豆:

import dataset
import plot_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD

m = 100
X, Y = dataset.get_beans(m)
plot_utils.show_scatter(X, Y)

model = Sequential()
# 当前层神经元的数量为2,激活函数类型:sigmoid,输入数据特征维度:2
model.add(Dense(units=2, activation='sigmoid', input_dim=2))
model.add(Dense(units=1, activation='sigmoid'))
# loss(损失函数、代价函数):mean_squared_error均方误差;
# optimizer(优化器):sgd(随机梯度下降算法);
# metrics(评估标准):accuracy(准确度);
model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.05), metrics=['accuracy'])
# epochs:回合数(全部样本完成一次训练)、batch_size:批数量(一次训练使用多少个样本)
model.fit(X, Y, epochs=5000, batch_size=10)

pres = model.predict(X)

plot_utils.show_scatter_surface(X, Y, model)

豆豆毒性分布:
【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建_第10张图片
训练结果:
【人工智能原理自学】初识Keras:轻松完成神经网络模型搭建_第11张图片

 [ 笔者 ]   文艺倾年
 [ 更新 ]   2023.1.19
❌ [ 勘误 ]   /* 暂无 */
 [ 声明 ]   由于作者水平有限,本文有错误和不准确之处在所难免,
              本人也很想知道这些错误,恳望读者批评指正!
 [ 代码 ]   https://github.com/itxaiohanglover/ai_lesson

你可能感兴趣的:(人工智能,人工智能,keras,神经网络)