当前位置:网站首页>一篇文章读懂人工神经网络
一篇文章读懂人工神经网络
2022-07-27 20:14:00 【InfoQ】
1.概述


2.原理

3.训练步骤
1.网络初始化
hidden_floors_num:隐藏层的个数
every_hidden_floor_num:每层隐藏层的神经元个数
learning_rate:学习速率
activation:激活函数
regularization:正则化方式
regularization_rate:正则化比率
total_step:总的训练次数
train_data_path:训练数据路径
model_save_path:模型保存路径 2.隐含层输出计算


3.输出层输出计算

4.误差计算

5.权值更新

6.阈值更新

7.判断算法迭代是否完成
4.案例
1.生成数据
# 生成测试数据
import numpy as np
import pandas as pd
# 训练集和验证集样本总个数
sample = 2000
train_data_path = 'train.csv'
validate_data_path = 'validate.csv'
predict_data_path = 'test.csv'
# 构造生成数据的模型
X1 = np.zeros((sample, 1))
X1[:, 0] = np.random.normal(1, 1, sample)
X2 = np.zeros((sample, 1))
X2[:, 0] = np.random.normal(2, 1, sample)
X3 = np.zeros((sample, 1))
X3[:, 0] = np.random.normal(3, 1, sample)
# 模型
Y = 6 * X1 - 3* X2 + X3 * X3 + np.random.normal(0, 0.1, [sample, 1])
# 将所有生成的数据放到data里面
data = np.zeros((sample, 4))
data[:, 0] = X1[:, 0]
data[:, 1] = X2[:, 0]
data[:, 2] = X3[:, 0]
data[:, 3] = Y[:, 0]
# 将data分成测试集和训练集
num_traindata = int(0.8*sample)
# 将训练数据保存
traindata = pd.DataFrame(data[0:num_traindata, :], columns=['x1', 'x2', 'x3', 'y'])
traindata.to_csv(train_data_path, index=False)
print('训练数据保存在: ', train_data_path)
# 将验证数据保存
validate_data = pd.DataFrame(data[num_traindata:, :], columns=['x1', 'x2', 'x3', 'y'])
validate_data.to_csv(validate_data_path, index=False)
print('验证数据保存在: ', validate_data_path)
# 将预测数据保存
predict_data = pd.DataFrame(data[num_traindata:, 0:-1], columns=['x1', 'x2', 'x3'])
predict_data.to_csv(predict_data_path, index=False)
print('预测数据保存在: ', predict_data_path)

2.训练验证模型
import tensorflow as tf
import pandas as pd
import numpy as np
createVar = locals()
'''
建立一个网络结构可变的BP神经网络通用代码:
在训练时各个参数的意义:
hidden_floors_num:隐藏层的个数
every_hidden_floor_num:每层隐藏层的神经元个数
learning_rate:学习速率
activation:激活函数
regularization:正则化方式
regularization_rate:正则化比率
total_step:总的训练次数
train_data_path:训练数据路径
model_save_path:模型保存路径
利用训练好的模型对验证集进行验证时各个参数的意义:
model_save_path:模型保存路径
validate_data_path:验证集路径
precision:精度
利用训练好的模型进行预测时各个参数的意义:
model_save_path:模型的保存路径
predict_data_path:预测数据路径
predict_result_save_path:预测结果保存路径
'''
# 训练模型全局参数
hidden_floors_num = 1
every_hidden_floor_num = [50]
learning_rate = 0.00001
activation = 'tanh'
regularization = 'L1'
regularization_rate = 0.0001
total_step = 200000
train_data_path = 'train.csv'
model_save_path = 'model/predict_model'
# 利用模型对验证集进行验证返回正确率
model_save_path = 'model/predict_model'
validate_data_path = 'validate.csv'
precision = 0.5
# 利用模型进行预测全局参数
model_save_path = 'model/predict_model'
predict_data_path = 'test.csv'
predict_result_save_path = 'test_predict.csv'
def inputs(train_data_path):
train_data = pd.read_csv(train_data_path)
X = np.array(train_data.iloc[:, :-1])
Y = np.array(train_data.iloc[:, -1:])
return X, Y
def make_hidden_layer(pre_lay_num, cur_lay_num, floor):
createVar['w' + str(floor)] = tf.Variable(tf.random_normal([pre_lay_num, cur_lay_num], stddev=1))
createVar['b' + str(floor)] = tf.Variable(tf.random_normal([cur_lay_num], stddev=1))
return eval('w'+str(floor)), eval('b'+str(floor))
def initial_w_and_b(all_floors_num):
# 初始化隐藏层的w, b
for floor in range(2, hidden_floors_num+3):
pre_lay_num = all_floors_num[floor-2]
cur_lay_num = all_floors_num[floor-1]
w_floor, b_floor = make_hidden_layer(pre_lay_num, cur_lay_num, floor)
createVar['w' + str(floor)] = w_floor
createVar['b' + str(floor)] = b_floor
def cal_floor_output(x, floor):
w_floor = eval('w'+str(floor))
b_floor = eval('b'+str(floor))
if activation == 'sigmoid':
output = tf.sigmoid(tf.matmul(x, w_floor) + b_floor)
if activation == 'tanh':
output = tf.tanh(tf.matmul(x, w_floor) + b_floor)
if activation == 'relu':
output = tf.nn.relu(tf.matmul(x, w_floor) + b_floor)
return output
def inference(x):
output = x
for floor in range(2, hidden_floors_num+2):
output = cal_floor_output(output, floor)
floor = hidden_floors_num+2
w_floor = eval('w'+str(floor))
b_floor = eval('b'+str(floor))
output = tf.matmul(output, w_floor) + b_floor
return output
def loss(x, y_real):
y_pre = inference(x)
if regularization == 'None':
total_loss = tf.reduce_sum(tf.squared_difference(y_real, y_pre))
if regularization == 'L1':
total_loss = 0
for floor in range(2, hidden_floors_num + 3):
w_floor = eval('w' + str(floor))
total_loss = total_loss + tf.contrib.layers.l1_regularizer(regularization_rate)(w_floor)
total_loss = total_loss + tf.reduce_sum(tf.squared_difference(y_real, y_pre))
if regularization == 'L2':
total_loss = 0
for floor in range(2, hidden_floors_num + 3):
w_floor = eval('w' + str(floor))
total_loss = total_loss + tf.contrib.layers.l2_regularizer(regularization_rate)(w_floor)
total_loss = total_loss + tf.reduce_sum(tf.squared_difference(y_real, y_pre))
return total_loss
def train(total_loss):
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)
return train_op
# 训练模型
def train_model(hidden_floors_num, every_hidden_floor_num, learning_rate, activation, regularization,
regularization_rate, total_step, train_data_path, model_save_path):
file_handle = open('acc.txt', mode='w')
X, Y = inputs(train_data_path)
X_dim = X.shape[1]
all_floors_num = [X_dim] + every_hidden_floor_num + [1]
# 将参数保存到和model_save_path相同的文件夹下, 恢复模型进行预测时加载这些参数创建神经网络
temp = model_save_path.split('/')
model_name = temp[-1]
parameter_path = ''
for i in range(len(temp)-1):
parameter_path = parameter_path + temp[i] + '/'
parameter_path = parameter_path + model_name + '_parameter.txt'
with open(parameter_path, 'w') as f:
f.write("all_floors_num:")
for i in all_floors_num:
f.write(str(i) + ' ')
f.write('\n')
f.write('activation:')
f.write(str(activation))
x = tf.placeholder(dtype=tf.float32, shape=[None, X_dim])
y_real = tf.placeholder(dtype=tf.float32, shape=[None, 1])
initial_w_and_b(all_floors_num)
y_pre = inference(x)
total_loss = loss(x, y_real)
train_op = train(total_loss)
# 记录在训练集上的正确率
train_accuracy = tf.reduce_mean(tf.cast(tf.abs(y_pre - y_real) < precision, tf.float32))
print(y_pre)
# 保存模型
saver = tf.train.Saver()
# 在一个会话对象中启动数据流图,搭建流程
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for step in range(total_step):
sess.run([train_op], feed_dict={x: X[0:, :], y_real: Y[0:, :]})
if step % 1000 == 0:
saver.save(sess, model_save_path)
total_loss_value = sess.run(total_loss, feed_dict={x: X[0:, :], y_real: Y[0:, :]})
lxacc=sess.run(train_accuracy, feed_dict={x: X, y_real: Y})
print('train step is ', step, ', total loss value is ', total_loss_value,
', train_accuracy', lxacc,
', precision is ', precision)
file_handle.write(str(lxacc)+"\n")
saver.save(sess, model_save_path)
sess.close()
def validate(model_save_path, validate_data_path, precision):
# **********************根据model_save_path推出模型参数路径, 解析出all_floors_num和activation****************
temp = model_save_path.split('/')
model_name = temp[-1]
parameter_path = ''
for i in range(len(temp)-1):
parameter_path = parameter_path + temp[i] + '/'
parameter_path = parameter_path + model_name + '_parameter.txt'
with open(parameter_path, 'r') as f:
lines = f.readlines()
# 从读取的内容中解析all_floors_num
temp = lines[0].split(':')[-1].split(' ')
all_floors_num = []
for i in range(len(temp)-1):
all_floors_num = all_floors_num + [int(temp[i])]
# 从读取的内容中解析activation
activation = lines[1].split(':')[-1]
hidden_floors_num = len(all_floors_num) - 2
# **********************读取验证数据*************************************
X, Y = inputs(validate_data_path)
X_dim = X.shape[1]
# **********************创建神经网络************************************
x = tf.placeholder(dtype=tf.float32, shape=[None, X_dim])
y_real = tf.placeholder(dtype=tf.float32, shape=[None, 1])
initial_w_and_b(all_floors_num)
y_pre = inference(x)
# 记录在验证集上的正确率
validate_accuracy = tf.reduce_mean(tf.cast(tf.abs(y_pre - y_real) < precision, tf.float32))
sess = tf.Session()
saver = tf.train.Saver()
with tf.Session() as sess:
# 读取模型
try:
saver.restore(sess, model_save_path)
print('模型载入成功!')
except:
print('模型不存在,请先训练模型!')
return
validate_accuracy_value = sess.run(validate_accuracy, feed_dict={x: X, y_real: Y})
print('validate_accuracy is ', validate_accuracy_value)
return validate_accuracy_value
def predict(model_save_path, predict_data_path, predict_result_save_path):
# **********************根据model_save_path推出模型参数路径, 解析出all_floors_num和activation****************
temp = model_save_path.split('/')
model_name = temp[-1]
parameter_path = ''
for i in range(len(temp)-1):
parameter_path = parameter_path + temp[i] + '/'
parameter_path = parameter_path + model_name + '_parameter.txt'
with open(parameter_path, 'r') as f:
lines = f.readlines()
# 从读取的内容中解析all_floors_num
temp = lines[0].split(':')[-1].split(' ')
all_floors_num = []
for i in range(len(temp)-1):
all_floors_num = all_floors_num + [int(temp[i])]
# 从读取的内容中解析activation
activation = lines[1].split(':')[-1]
hidden_floors_num = len(all_floors_num) - 2
# **********************读取预测数据*************************************
predict_data = pd.read_csv(predict_data_path)
X = np.array(predict_data.iloc[:, :])
X_dim = X.shape[1]
# **********************创建神经网络************************************
x = tf.placeholder(dtype=tf.float32, shape=[None, X_dim])
initial_w_and_b(all_floors_num)
y_pre = inference(x)
sess = tf.Session()
saver = tf.train.Saver()
with tf.Session() as sess:
# 读取模型
try:
saver.restore(sess, model_save_path)
print('模型载入成功!')
except:
print('模型不存在,请先训练模型!')
return
y_pre_value = sess.run(y_pre, feed_dict={x: X[0:, :]})
# 将预测结果写入csv文件
predict_data_columns = list(predict_data.columns) + ['predict']
data = np.column_stack([X, y_pre_value])
result = pd.DataFrame(data, columns=predict_data_columns)
result.to_csv(predict_result_save_path, index=False)
print('预测结果保存在:', predict_result_save_path)
if __name__ == '__main__':
mode = "train"
if mode == 'train':
# 训练模型
train_model(hidden_floors_num, every_hidden_floor_num, learning_rate, activation, regularization,
regularization_rate, total_step, train_data_path, model_save_path)
if mode == 'validate':
# 利用模型对验证集进行正确性测试
validate(model_save_path, validate_data_path, precision)
if mode == 'predict':
# 利用模型进行预测
predict(model_save_path, predict_data_path, predict_result_save_path)



3.预测数据

边栏推荐
- MySQL basic installation and startup
- 2022 review plan of joint provincial election
- An article to solve the bigkey problem in redis
- 2022 / 4 / 11 exam summary
- Keming food: the average increase in the sales price of various series of products is about 5%
- It's time to say goodbye gracefully to nullpointexception
- 带你掌握 Makefile 分析
- Safety foundation 2
- 多肽KC2S修饰白蛋白纳米粒/靶向肽GX1修饰人血清白蛋白纳米粒探针的研究制备
- 物联网架构完全指南
猜你喜欢

图论的小技巧以及扩展

可能导致索引失效的原因

Cloudcompare & PCL platform convex hull method to calculate crown volume

Introduction to the paper | distributed graph simulation

51 MCU internal peripherals: real time clock (SPI)

Preparation of peptide kc2s modified albumin nanoparticles / targeting peptide GX1 modified human serum albumin nanoparticles probe

Leetcode-461. Hamming distance

Pyqt5 rapid development and practice 4.10 window drawing controls

leetcode-461.汉明距离

JVM composition and memory model
随机推荐
leetcode-461.汉明距离
TFRecord的Shuffle、划分和读取
2022年软件开发的趋势
You don't know about redis. Let me explain the underlying data structure of redis in detail
初中三年回忆录
Exam summary on May 31, 2022
It's time to say goodbye gracefully to nullpointexception
Video human behavior detection
2022/3/22 examination summary
leetcode-470.用 Rand7() 实现 Rand10()
The wave of smart home is coming, how to make machines understand the world [there is information at the end]
Lianmai live broadcast system software - voice chat system
20字符短域名绕过复现
Exam summary on May 13, 2022
MySQL的B+Tree索引到底是咋回事?聚簇索引到底是如何长高的?
Six employees have been confirmed! Samsung closed the turtle tail mobile phone factory for the third time!
Convnext:a convnet for the 2020s - model Brief
物联网架构完全指南
Summary of exam on May 17, 2022
Px4 module design part 13: workqueue design