当前位置:网站首页>Read an article to understand artificial neural network
Read an article to understand artificial neural network
2022-07-27 23:14:00 【InfoQ】
1. summary


2. principle

3. Training steps
1. Network initialization
hidden_floors_num: Number of hidden layers
every_hidden_floor_num: The number of neurons in each hidden layer
learning_rate: Learning rate
activation: Activation function
regularization: Regularization approach
regularization_rate: Regularization ratio
total_step: Total training times
train_data_path: Training data path
model_save_path: Model save path 2. Hidden layer output calculation


3. Output layer output calculation

4. Error calculation

5. Weight update

6. Threshold update

7. Judge whether the algorithm iteration is completed
4. Case study
1. Generate the data
# Generate test data
import numpy as np
import pandas as pd
# The total number of training set and verification set samples
sample = 2000
train_data_path = 'train.csv'
validate_data_path = 'validate.csv'
predict_data_path = 'test.csv'
# Construct a model that generates data
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)
# Model
Y = 6 * X1 - 3* X2 + X3 * X3 + np.random.normal(0, 0.1, [sample, 1])
# Put all the generated data into data Inside
data = np.zeros((sample, 4))
data[:, 0] = X1[:, 0]
data[:, 1] = X2[:, 0]
data[:, 2] = X3[:, 0]
data[:, 3] = Y[:, 0]
# take data Divided into test set and training set
num_traindata = int(0.8*sample)
# Save the training data
traindata = pd.DataFrame(data[0:num_traindata, :], columns=['x1', 'x2', 'x3', 'y'])
traindata.to_csv(train_data_path, index=False)
print(' Training data is stored in : ', train_data_path)
# Save the validation data
validate_data = pd.DataFrame(data[num_traindata:, :], columns=['x1', 'x2', 'x3', 'y'])
validate_data.to_csv(validate_data_path, index=False)
print(' The validation data is saved in : ', validate_data_path)
# Save the forecast data
predict_data = pd.DataFrame(data[num_traindata:, 0:-1], columns=['x1', 'x2', 'x3'])
predict_data.to_csv(predict_data_path, index=False)
print(' The forecast data is saved in : ', predict_data_path)

2. Training validation model
import tensorflow as tf
import pandas as pd
import numpy as np
createVar = locals()
'''
Build a network with variable structure BP Neural network general code :
The significance of various parameters during training :
hidden_floors_num: Number of hidden layers
every_hidden_floor_num: The number of neurons in each hidden layer
learning_rate: Learning rate
activation: Activation function
regularization: Regularization approach
regularization_rate: Regularization ratio
total_step: Total training times
train_data_path: Training data path
model_save_path: Model save path
The significance of each parameter when using the trained model to verify the verification set :
model_save_path: Model save path
validate_data_path: Verify set path
precision: precision
The significance of each parameter when using the trained model to predict :
model_save_path: Save path of model
predict_data_path: Predict the data path
predict_result_save_path: Save path of prediction results
'''
# Training model global parameters
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'
# Use the model to verify the verification set and return the correct rate
model_save_path = 'model/predict_model'
validate_data_path = 'validate.csv'
precision = 0.5
# Use the model to predict the global parameters
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):
# Initialize hidden layer 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
# Training models
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]
# Save parameters to and model_save_path Under the same folder , When restoring the model to predict, load these parameters to create a neural network
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)
# The accuracy recorded on the training set
train_accuracy = tf.reduce_mean(tf.cast(tf.abs(y_pre - y_real) < precision, tf.float32))
print(y_pre)
# Save the model
saver = tf.train.Saver()
# Start data flow graph in a session object , Construction process
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):
# ********************** according to model_save_path Deduce the model parameter path , It is concluded that all_floors_num and 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()
# Parse from the read content 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])]
# Parse from the read content activation
activation = lines[1].split(':')[-1]
hidden_floors_num = len(all_floors_num) - 2
# ********************** Read validation data *************************************
X, Y = inputs(validate_data_path)
X_dim = X.shape[1]
# ********************** Creating neural networks ************************************
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)
# The accuracy recorded on the verification set
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:
# Read the model
try:
saver.restore(sess, model_save_path)
print(' The model is loaded successfully !')
except:
print(' The model does not exist , Please train the model first !')
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):
# ********************** according to model_save_path Deduce the model parameter path , It is concluded that all_floors_num and 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()
# Parse from the read content 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])]
# Parse from the read content activation
activation = lines[1].split(':')[-1]
hidden_floors_num = len(all_floors_num) - 2
# ********************** Read prediction data *************************************
predict_data = pd.read_csv(predict_data_path)
X = np.array(predict_data.iloc[:, :])
X_dim = X.shape[1]
# ********************** Creating neural networks ************************************
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:
# Read the model
try:
saver.restore(sess, model_save_path)
print(' The model is loaded successfully !')
except:
print(' The model does not exist , Please train the model first !')
return
y_pre_value = sess.run(y_pre, feed_dict={x: X[0:, :]})
# Write the prediction results to csv file
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(' The forecast results are stored in :', predict_result_save_path)
if __name__ == '__main__':
mode = "train"
if mode == 'train':
# Training models
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':
# Use the model to test the correctness of the verification set
validate(model_save_path, validate_data_path, precision)
if mode == 'predict':
# Using models to make predictions
predict(model_save_path, predict_data_path, predict_result_save_path)



3. Forecast data

边栏推荐
- Jstack stuff
- 2022/3/22考试总结
- Feed stream application reconfiguration - Architecture
- 2022/3/22 examination summary
- Leetcode-461. Hamming distance
- 干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
- 物联网架构完全指南
- Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (medium)
- 对象创建过程及对象布局
- Another fire broke out in Samsung storage factory!
猜你喜欢

Harmonyos third operation

可能导致索引失效的原因

The security dilemma of software supply chain faced by enterprises

物联网架构完全指南

catch all in one draft! Introduction to 10 data visualization software

Fluorescence imaging of cle19 polypeptide in cells preparation of fluorescence quenching quantum dots of bovine serum albumin

Cloud native enthusiast weekly: a complete collection of client go examples

2022年五大网络管理趋势

On data management of data warehouse

Deploy dolphin scheduler high availability cluster based on rainbow
随机推荐
Oppo find x2 series release: 3k+120hz curved screen, DxO score first, top version 6999 yuan!
Take byte offer in four rounds and answer the interview questions
Pyqt5 rapid development and practice 4.10 window drawing controls
只会Excel想做图表可视化,让数据动起来?可以,快来围观啦(附大量模板下载)
Arm32进行远程调试
Do you want to be dismissed? Let's take a look at the "exit tips" of programmers
containerd ctr运行ansible容器执行ansible-playbook任务完整命令
组件的传参
微信安装包11年膨胀575倍,UP主:“98%的文件是垃圾”;苹果应用商店被曝大量色情App;四大科技巨头呼吁废除闰秒|极客头条
Tips and extensions of graph theory
Five network management trends in 2022
Metersphere financial company landing experience sharing
The ASML lithography machine purchased by SMIC international entered the factory smoothly, but it is not a non EUV lithography machine!
2022/4/8 exam summary
US officials suggested trump prevent Infineon from acquiring cypress
Basic SQL general syntax and classification
干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
2022/6/9 exam summary
2022/6/5考试总结
2022/3/11 exam summary