当前位置:网站首页>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

边栏推荐
- Purple light FPGA solves the mask problem! Boost the overall speed-up of mask production
- C语言详解系列——函数的认识(5)函数递归与迭代
- 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
- Preparation of peptide kc2s modified albumin nanoparticles / targeting peptide GX1 modified human serum albumin nanoparticles probe
- 2022 / 4 / 11 exam summary
- TFRecord的Shuffle、划分和读取
- 51单片机内部外设:实时时钟(SPI)
- cron 表达式
- Basic SQL DML
猜你喜欢

Quartus:Instantiation of ‘sdram_model_plus‘ failed. The design unit was not found.

Jeninkins offline deployment

Harmonyos third operation

细胞CLE19多肽荧光成像牛血清白蛋白荧光猝灭量子点的制备
C language explanation series -- understanding of functions (5) function recursion and iteration

网络开发套接字以及UDP、TCP协议

你想被开除吗?来看看程序员「离职小技巧」吧
![[C language] simulate and implement string functions (Part 1)](/img/13/afb015de3448e20a0b7b09c1aca4ad.png)
[C language] simulate and implement string functions (Part 1)

Cloudcompare & PCL platform convex hull method to calculate crown volume

Lianmai live broadcast system software - voice chat system
随机推荐
Here comes Gree mask! Kn95 mask only costs 5.5 yuan!
Analysis of cloud native application security organization structure
Trends in software development in 2022
浅谈数仓的数据治理
对象创建过程及对象布局
Arm32 for remote debugging
微信安装包11年膨胀575倍,UP主:“98%的文件是垃圾”;苹果应用商店被曝大量色情App;四大科技巨头呼吁废除闰秒|极客头条
Zhihu data analysis training camp all-round class
Pyqt5 rapid development and practice 4.9 dialog controls
2022 / 4 / 11 exam summary
网络开发套接字以及UDP、TCP协议
2022/3/11 exam summary
2022年软件开发的趋势
[noi2018] bubble sort (combination + Cartland number +dp+ tree array)
2022/3/11 考试总结
Jeninkins offline deployment
数据仓库项目从来不是技术项目
ADI, Shijian and Junlong technology jointly donated 2.3 million yuan to help fight the epidemic in Hubei
In depth analysis - file operation
Six employees have been confirmed! Samsung closed the turtle tail mobile phone factory for the third time!