【深度学习笔记2.2.2】AlexNet训练mnist

2024-06-06 05:58

本文主要是介绍【深度学习笔记2.2.2】AlexNet训练mnist,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实验1:AlexNet Tensorflow 实现

代码示例如下(详见文献[2]AlexNet1.py):

import numpy as np
import cv2
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as pltdatapath = '/home/pathto/res/MNIST_data'
mnist_data_set = input_data.read_data_sets(datapath, validation_size=0, one_hot=True)def image_shape_scale(batch_xs):images = np.reshape(batch_xs, [batch_xs.shape[0], 28, 28])imlist = [][imlist.append(cv2.resize(img, (227, 227))) for img in images]images = np.array(imlist)# cv2.imwrite('scale1.jpg', images[0]*200)# cv2.imwrite('scale2.jpg', images[1]*200)# batch_xs = np.reshape(images, [batch_xs.shape[0], 227 * 227 * input_image_channel])batch_xs = np.reshape(images, [batch_xs.shape[0], 227, 227, input_image_channel])return batch_xsinput_image_channel = 1
learning_rate = 1e-4
training_epoch = 50
batch_size = 200
n_classes = 10
n_fc1 = 6*6*256
n_fc2 = 4096
n_fc3 = 4096
dropout_rate = 0.5X = tf.placeholder(tf.float32, [None, 227, 227, input_image_channel])
y = tf.placeholder(tf.float32, [None, n_classes])W_conv = {'conv1': tf.Variable(tf.truncated_normal([11, 11, input_image_channel, 96])),'conv2': tf.Variable(tf.truncated_normal([5, 5, 96, 256])),'conv3': tf.Variable(tf.truncated_normal([3, 3, 256, 384])),'conv4': tf.Variable(tf.truncated_normal([3, 3, 384, 384])),'conv5': tf.Variable(tf.truncated_normal([3, 3, 384, 256])),'fc1': tf.Variable(tf.truncated_normal([n_fc1, n_fc2])),'fc2': tf.Variable(tf.truncated_normal([n_fc2, n_fc3])),'output': tf.Variable(tf.truncated_normal([n_fc3, n_classes]))
}b_conv = {'conv1': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[96])),'conv2': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[256])),'conv3': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[384])),'conv4': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[384])),'conv5': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[256])),'fc1': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[n_fc2])),'fc2': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[n_fc3])),'output': tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[n_classes]))
}X_image = tf.reshape(X, [-1, 227, 227, input_image_channel])# 卷积层1
conv1 = tf.nn.conv2d(X_image, W_conv['conv1'], strides=[1, 4, 4, 1], padding='VALID')
conv1 = tf.nn.bias_add(conv1, b_conv['conv1'])
conv1 = tf.nn.relu(conv1)
conv1 = tf.nn.local_response_normalization(conv1, depth_radius=2, alpha=2e-05, beta=0.75, bias=1.0)
# 此时 conv1.shape = [-1, 55, 55, 96]# 池化层1
pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID')
# pool1.shape = [-1, 27, 27, 96]# 卷积层2
conv2 = tf.nn.conv2d(pool1, W_conv['conv2'], strides=[1, 1, 1, 1], padding='SAME')
conv2 = tf.nn.bias_add(conv2, b_conv['conv2'])
conv2 = tf.nn.relu(conv2)
conv2 = tf.nn.local_response_normalization(conv2, depth_radius=2, alpha=2e-05, beta=0.75, bias=1.0)
# 此时 conv2.shape = [-1, 27, 27, 256]# 池化层2
pool2 = tf.nn.max_pool(conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID')
# 此时 pool2.shape = [-1, 13, 13, 256]# 卷积层3
conv3 = tf.nn.conv2d(pool2, W_conv['conv3'], strides=[1, 1, 1, 1], padding='SAME')
conv3 = tf.nn.bias_add(conv3, b_conv['conv3'])
conv3 = tf.nn.relu(conv3)
# 此时 conv3.shape = [-1, 13, 13, 384]# 卷积层4
conv4 = tf.nn.conv2d(conv3, W_conv['conv4'], strides=[1, 1, 1, 1], padding='SAME')
conv4 = tf.nn.bias_add(conv4, b_conv['conv4'])
conv4 = tf.nn.relu(conv4)
# 此时 conv4.shape = [-1, 13, 13, 384]# 卷积层5
conv5 = tf.nn.conv2d(conv4, W_conv['conv5'], strides=[1, 1, 1, 1], padding='SAME')
conv5 = tf.nn.bias_add(conv5, b_conv['conv5'])
conv5 = tf.nn.relu(conv5)
# 此时 conv5.shape = [-1, 13, 13, 256]# 池化层5
pool5 = tf.nn.max_pool(conv5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID')
# 此时pool5.shape = [-1, 6, 6, 256]# 全连接层1
reshape = tf.reshape(pool5, [-1, n_fc1])
# 此时reshape.shape = [-1, 9216]
fc1 = tf.add(tf.matmul(reshape, W_conv['fc1']), b_conv['fc1'])
fc1 = tf.nn.relu(fc1)
fc1 = tf.nn.dropout(fc1, dropout_rate)
# 此时fc1.shape = [-1, 4096]# 全连接层2
fc2 = tf.add(tf.matmul(fc1, W_conv['fc2']), b_conv['fc2'])
fc2 = tf.nn.relu(fc2)
fc2 = tf.nn.dropout(fc2, dropout_rate)
# 此时fc2.shape = [-1, 4096]# 输出层
output = tf.add(tf.matmul(fc2, W_conv['output']), b_conv['output'])
# 此时output.shape = [-1. 10]# 定义交叉熵损失函数(有两种方法):
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 方法1: 自己实现交叉熵
y_output = tf.nn.softmax(output)  # 对网络最后一层的输出做softmax, 这通常是求取输出属于某一类的概率
cross_entropy = -tf.reduce_sum(y * tf.log(y_output))  # 用softmax的输出向量和样本的实际标签做一个交叉熵.
loss = tf.reduce_mean(cross_entropy)  # 对交叉熵求均值就是loss
# loss = -tf.reduce_mean(y * tf.log(y_output))  # 交叉熵本应是一个向量,但tf.reduce_mean可以直接求取tensor所有维度的和,所以这里可以用tf.reduce_mean一句代替上述三步。# 方法2:使用tensorflow自带的tf.nn.softmax_cross_entropy_with_logits函数实现交叉熵
# loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y))
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~train_step = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss)# 评估模型
correct_pred = tf.equal(tf.argmax(y_output, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))init = tf.global_variables_initializer()loss_buf = []
accuracy_buf = []
with tf.device("/gpu:0"):# with tf.Graph().as_default():gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0)config = tf.ConfigProto(gpu_options=gpu_options, allow_soft_placement=True, log_device_placement=True)# config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)with tf.Session(config=config) as sess:sess.run(init)total_batch = mnist_data_set.train.num_examples // batch_sizefor i in range(training_epoch):for iteration in range(total_batch):batch_xs, batch_ys = mnist_data_set.train.next_batch(batch_size)batch_xs = image_shape_scale(batch_xs)sess.run(train_step, feed_dict={X: batch_xs, y: batch_ys})test_accuracy = sess.run(accuracy, feed_dict={X: batch_xs, y: batch_ys})print("step {}, iteration {}, training accuracy {}".format(i, iteration, test_accuracy))batch_xs, batch_ys = mnist_data_set.test.images[0:1000, :], mnist_data_set.test.labels[0:1000, :]batch_xs = image_shape_scale(batch_xs)loss_val = sess.run(loss, feed_dict={X: batch_xs, y: batch_ys})test_accuracy = sess.run(accuracy, feed_dict={X: batch_xs, y: batch_ys})loss_buf.append(loss_val)accuracy_buf.append(test_accuracy)print("step {}, loss {}, testing accuracy {}".format(i, loss_val, test_accuracy))# 画出准确率曲线
accuracy_ndarray = np.array(accuracy_buf)
accuracy_size = np.arange(len(accuracy_ndarray))
plt.plot(accuracy_size, accuracy_ndarray, 'b+', label='accuracy')loss_ndarray = np.array(loss_buf)
loss_size = np.arange(len(loss_ndarray))
plt.plot(loss_size, loss_ndarray, 'r*', label='loss')plt.show()# 保存loss和测试准确率到csv文件
with open('AlexNet.csv', 'w') as fid:for loss, acc in zip(loss_buf, accuracy_buf):strText = str(loss) + ',' + str(acc) + '\n'fid.write(strText)
fid.close()print('end')

训练步骤打印结果如下:
step 0, loss nan, testing accuracy 0.08500000089406967
step 1, loss nan, testing accuracy 0.08500000089406967
step 2, loss nan, testing accuracy 0.08500000089406967
step 3, loss nan, testing accuracy 0.08500000089406967
… …
可以看到,模型无法收敛。

实验2:通过改变权重初始化方法进行优化

后参考文献[1]代码,排查到模型无法收敛的原因可能是weights、biases的初始化不当。上述代码中,tf.truncated_normal默认使用均值mean为0、标准差stddev为1的截断正态分布来初始化W_conv和b_conv。
这里我们可以对上述代码AlexNet1.py做如下修改(其他不变,此部分完整代码详见文献[2]AlexNet2.py):

W_conv = {'conv1': tf.Variable(tf.truncated_normal([11, 11, input_image_channel, 96], mean=0, stddev=0.01)),'conv2': tf.Variable(tf.truncated_normal([5, 5, 96, 256], mean=0, stddev=0.01)),'conv3': tf.Variable(tf.truncated_normal([3, 3, 256, 384], mean=0, stddev=0.01)),'conv4': tf.Variable(tf.truncated_normal([3, 3, 384, 384], mean=0, stddev=0.01)),'conv5': tf.Variable(tf.truncated_normal([3, 3, 384, 256], mean=0, stddev=0.01)),'fc1': tf.Variable(tf.truncated_normal([n_fc1, n_fc2], mean=0, stddev=0.01)),'fc2': tf.Variable(tf.truncated_normal([n_fc2, n_fc3], mean=0, stddev=0.01)),'output': tf.Variable(tf.truncated_normal([n_fc3, n_classes], mean=0, stddev=0.01))
}b_conv = {'conv1': tf.Variable(tf.truncated_normal([96], mean=0.005, stddev=0.1)),'conv2': tf.Variable(tf.truncated_normal([256], mean=0.005, stddev=0.1)),'conv3': tf.Variable(tf.truncated_normal([384], mean=0.005, stddev=0.1)),'conv4': tf.Variable(tf.truncated_normal([384], mean=0.005, stddev=0.1)),'conv5': tf.Variable(tf.truncated_normal([256], mean=0.005, stddev=0.1)),'fc1': tf.Variable(tf.truncated_normal([n_fc2], mean=0.005, stddev=0.1)),'fc2': tf.Variable(tf.truncated_normal([n_fc3], mean=0.005, stddev=0.1)),'output': tf.Variable(tf.truncated_normal([n_classes], mean=0.005, stddev=0.1))
}

训练步骤打印结果如下:
step 1, loss 2299.4792, testing accuracy 0.122
step 2, loss 2299.8113, testing accuracy 0.117
… …
step 11, loss 2293.2812, testing accuracy 0.125
step 12, loss 1955.1497, testing accuracy 0.391
step 13, loss 353.29596, testing accuracy 0.866
step 14, loss 146.65419, testing accuracy 0.954
… …
step 44, loss 36.49175, testing accuracy 0.99
step 45, loss 22.933325, testing accuracy 0.986
step 46, loss 35.3011, testing accuracy 0.99
step 47, loss nan, testing accuracy 0.085
step 48, loss nan, testing accuracy 0.085
step 49, loss nan, testing accuracy 0.085
step 50, loss nan, testing accuracy 0.085
可以看到,此时算法模型可以正常收敛,但在中途会突然梯度爆炸。

实验3:中途调低学习率避免梯度爆炸

为了解决梯度爆炸,我们可以在梯度下降接近低谷附近时调低学习率。这里需要继续在上述代码AlexNet2.py的基础上做些改进,主要是将学习率设为占位符变量,在训练的过程中动态设置学习率。主要改进如下(此部分完整代码详见文献[2]AlexNet3.py):

learning_rate_holder = tf.placeholder(tf.float32)
train_step = tf.train.GradientDescentOptimizer(learning_rate=learning_rate_holder).minimize(loss)... ...with tf.Session(config=config) as sess:sess.run(init)total_batch = mnist_data_set.train.num_examples // batch_sizefor i in range(training_epoch):for iteration in range(total_batch):... ...if i < 30:sess.run(train_step, feed_dict={X: batch_xs, y: batch_ys, learning_rate_holder: learning_rate})elif i < 50:sess.run(train_step, feed_dict={X: batch_xs, y: batch_ys, learning_rate_holder: learning_rate / 10.0})elif i < 70:sess.run(train_step, feed_dict={X: batch_xs, y: batch_ys, learning_rate_holder: learning_rate / 100.0})else:sess.run(train_step, feed_dict={X: batch_xs, y: batch_ys, learning_rate_holder: learning_rate / 1000.0})

训练步骤打印结果如下:
step 1, loss 2300.436, testing accuracy 0.117
step 2, loss 2299.375, testing accuracy 0.13
… …
step 10, loss 2290.4155, testing accuracy 0.154
step 11, loss 678.09644, testing accuracy 0.788
step 12, loss 178.35306, testing accuracy 0.948
… …
step 49, loss 35.42096, testing accuracy 0.988
step 50, loss 34.458153, testing accuracy 0.988
梯度爆炸问题得以解决。

实验4:使用tf.get_variable创建变量,使用tf.global_variables_initializer初始化

上面代码都是使用 tf.Variable(tf.truncated_normal(…)) 来创建权重矩阵的,我们现在尝试使用 tf.get_variable() 来创建权重矩阵。tf.Variable 和 tf.get_variable的区别是前者每次调用都会创建新的对象;而对于后者来说,如果变量已经存在则直接将该变量返回,否则它才会创建一个新的变量 [3]。

在AlexNet1.py的基础上做如下改进,其他不变:(此部分完整代码参见文献[2]AlexNet4.py)

W_conv = {'conv1': tf.get_variable('conv1/weights', shape=[11, 11, input_image_channel, 96]),'conv2': tf.get_variable('conv2/weights', shape=[5, 5, 96, 256]),'conv3': tf.get_variable('conv3/weights', shape=[3, 3, 256, 384]),'conv4': tf.get_variable('conv4/weights', shape=[3, 3, 384, 384]),'conv5': tf.get_variable('conv5/weights', shape=[3, 3, 384, 256]),'fc1': tf.get_variable('fc1/weights', shape=[n_fc1, n_fc2], trainable=True),'fc2': tf.get_variable('fc2/weights', shape=[n_fc2, n_fc3], trainable=True),'output': tf.get_variable('output/weights', shape=[n_fc3, n_classes], trainable=True)
}b_conv = {'conv1': tf.get_variable('conv1/biases', shape=[96]),'conv2': tf.get_variable('conv2/biases', shape=[256]),'conv3': tf.get_variable('conv3/biases', shape=[384]),'conv4': tf.get_variable('conv4/biases', shape=[384]),'conv5': tf.get_variable('conv5/biases', shape=[256]),'fc1': tf.get_variable('fc1/biases', shape=[n_fc2], trainable=True),'fc2': tf.get_variable('fc2/biases', shape=[n_fc3], trainable=True),'output': tf.get_variable('output/biases', shape=[n_classes], trainable=True)
}

训练步骤打印结果如下:
step 1, loss 170.07048, testing accuracy 0.936
step 2, loss 86.70665, testing accuracy 0.972
… …
step 50, loss 26.004112, testing accuracy 0.99
可以看到,算法模型step1时测试准确率就达到93.6%了,且后面没有梯度爆炸。

  在这里,由于我们并没有事先为权重创建变量,所以tf.get_variable会自己创建变量,然后使用tf.global_variables_initializer()初始化所有的变量,可见tf.get_variable和tf.global_variables_initializer()使用了一种较好的初始化策略。

本实验说明了良好的权重初始化对算法模型的训练是非常重要的。

实验5:对卷积层分组

在AlexNet2.py的基础上进行改进,将 W_conv 中的conv4、conv5分成两组,具体改进如下:(代码详见文献[2]AlexNet5.py)

W_conv = {... ...'conv4_1': tf.Variable(tf.truncated_normal([3, 3, 384//2, 384//2], mean=0, stddev=0.01)),'conv4_2': tf.Variable(tf.truncated_normal([3, 3, 384//2, 384//2], mean=0, stddev=0.01)),'conv5_1': tf.Variable(tf.truncated_normal([3, 3, 384//2, 256//2], mean=0, stddev=0.01)),'conv5_2': tf.Variable(tf.truncated_normal([3, 3, 384//2, 256//2], mean=0, stddev=0.01)),... ...
}

对卷积层4、卷积层5的具体改进如下:

# 卷积层4
conv3groups = tf.split(axis=3, num_or_size_splits=2, value=conv3)
conv4_1 = tf.nn.conv2d(conv3groups[0], W_conv['conv4_1'], strides=[1, 1, 1, 1], padding='SAME')
conv4_2 = tf.nn.conv2d(conv3groups[1], W_conv['conv4_2'], strides=[1, 1, 1, 1], padding='SAME')
conv4 = tf.concat(axis=3, values=[conv4_1, conv4_2])
conv4 = tf.nn.bias_add(conv4, b_conv['conv4'])
conv4 = tf.nn.relu(conv4)# 卷积层5
conv4groups = tf.split(axis=3, num_or_size_splits=2, value=conv4)
conv5_1 = tf.nn.conv2d(conv4groups[0], W_conv['conv5_1'], strides=[1, 1, 1, 1], padding='SAME')
conv5_2 = tf.nn.conv2d(conv4groups[1], W_conv['conv5_2'], strides=[1, 1, 1, 1], padding='SAME')
conv5 = tf.concat(axis=3, values=[conv5_1, conv5_2])
conv5 = tf.nn.bias_add(conv5, b_conv['conv5'])
conv5 = tf.nn.relu(conv5)

其他代码不变,训练测试结果打印如下:
step 1, loss 2297.4663, testing accuracy 0.104
step 2, loss 2299.2734, testing accuracy 0.124
… …
step 3, loss 2114.1978, testing accuracy 0.301
step 4, loss 366.07996, testing accuracy 0.872
… …
step 5, loss 44.795494, testing accuracy 0.992
step 6, loss 37.983482, testing accuracy 0.988
和AlexNet2.py相比,此时算法梯度下降速度更快,且不会出现梯度爆炸。

实验6:使用Batch Normalization优化

在AlexNet2.py的基础上,使用Batch Normalization算法优化模型,并且去掉lrn,主要改进如下:(代码详见文献[2]AlexNet6.py)

def batch_norm(inputs, is_training, is_conv_out=True, decay=0.999):scale = tf.Variable(tf.ones([inputs.get_shape()[-1]]))beta = tf.Variable(tf.zeros([inputs.get_shape()[-1]]))pop_mean = tf.Variable(tf.zeros([inputs.get_shape()[-1]]), trainable=False)pop_var = tf.Variable(tf.ones([inputs.get_shape()[-1]]), trainable=False)if is_training:if is_conv_out:batch_mean, batch_var = tf.nn.moments(inputs, [0, 1, 2])else:batch_mean, batch_var = tf.nn.moments(inputs, [0])train_mean = tf.assign(pop_mean, pop_mean*decay+batch_mean*(1-decay))train_var = tf.assign(pop_var, pop_var*decay+batch_var*(1-decay))with tf.control_dependencies([train_mean, train_var]):return tf.nn.batch_normalization(inputs, batch_mean, batch_var, beta, scale, 0.001)else:return tf.nn.batch_normalization(inputs, pop_mean, pop_var, beta, scale, 0.001)... ...
... ...conv1 = tf.nn.bias_add(...)
conv1 = batch_norm(conv1, True)
... ...conv2 = tf.nn.bias_add(...)
conv2 = batch_norm(conv2, True)
... ...conv3 = tf.nn.bias_add(...)
conv3 = batch_norm(conv3, True)
... ...conv4 = tf.nn.bias_add(...)
conv4 = batch_norm(conv4, True)
... ...conv5 = tf.nn.bias_add(...)
conv5 = batch_norm(conv5, True)
... ...fc1 = tf.add(...)
fc1 = batch_norm(fc1, True, False)
... ...fc2 = tf.add(...)
fc2 = batch_norm(fc2, True, False)
... ...

训练测试结果打印如下:
step 1, loss 44.443394, testing accuracy 0.979
step 1, loss 34.873466, testing accuracy 0.988
… …
step 1, loss 13.521537, testing accuracy 0.992
step 1, loss 10.757448, testing accuracy 0.991
很明显,使用BN算法优化效果非常显著。

AlexNet6.py优化中,我们使用了tf.truncated_normal并且指定mean、stddev来创建参数矩阵,后来我实验发现,如果使用AlexNet1.py中创建参数矩阵的方法的话(即使用tf.truncated_normal创建参数矩阵但mean和stddev却使用默认的0和1,详见文献[2]AlexNet7.py),则模型仍然是无法收敛的,这说明即使是使用BN优化,但不恰当的参数初始化仍然无法使模型收敛。

最后附上上述实验的loss和accuracy曲线图:
enter image description here

enter image description here

实验总结

  1. 合适的参数初始化是非常重要的;
  2. 动态调整学习率;
  3. 当我们不确定如何手动初始化参数矩阵时,可以使用 tf.get_variable + tf.global_variables_initializer 默认的初始化策略;
  4. 对卷积层分组是一个很好优化思路;
  5. Batch Normalization算法优化效果非常显著。

参考文献

[1] finetune_alexnet_with_tensorflow
[2] 我的handml仓库
[3] tensorflow学习笔记(二十三):variable与get_variable

这篇关于【深度学习笔记2.2.2】AlexNet训练mnist的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035271

相关文章

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、

最新Spring Security实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)

《最新SpringSecurity实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)》本章节介绍了如何通过SpringSecurity实现从配置自定义登录页面、表单登录处理逻辑的配置,并简单模拟... 目录前言改造准备开始登录页改造自定义用户名密码登陆成功失败跳转问题自定义登出前后端分离适配方案结语前言

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Redis 内存淘汰策略深度解析(最新推荐)

《Redis内存淘汰策略深度解析(最新推荐)》本文详细探讨了Redis的内存淘汰策略、实现原理、适用场景及最佳实践,介绍了八种内存淘汰策略,包括noeviction、LRU、LFU、TTL、Rand... 目录一、 内存淘汰策略概述二、内存淘汰策略详解2.1 ​noeviction(不淘汰)​2.2 ​LR

Python与DeepSeek的深度融合实战

《Python与DeepSeek的深度融合实战》Python作为最受欢迎的编程语言之一,以其简洁易读的语法、丰富的库和广泛的应用场景,成为了无数开发者的首选,而DeepSeek,作为人工智能领域的新星... 目录一、python与DeepSeek的结合优势二、模型训练1. 数据准备2. 模型架构与参数设置3