当前位置: 首页 > 编程日记 > 正文

扶稳!四大步“上手”超参数调优教程,就等你出马了 | 附完整代码

640?wx_fmt=png


作者 | Matthew Stewart

译者 | Monanfei

责编 | Jane

出品 | AI科技大本营(ID: rgznai100)

 

【导读】在本文中,我们将为大家介绍如何对神经网络的超参数进行优化调整,以便在 Beale 函数上获得更高性能,Beale 函数是评价优化有效性的众多测试函数之一。


Beale 函数


当应用数学家开发一种新的优化算法时,常用的做法是在测试函数上测试该算法,主要的评价指标如下:

 

  • 收敛速度(求得解的速度有多快)

  • 精度(接近真值的距离有多远)

  • 稳健性(在整体上表现良好,还是仅在一部分上表现较好)

  • 一般性能(例如计算复杂性等)

 

Beale 函数的可视化如下:


640?wx_fmt=png

 

Beale 函数评估了在非常浅梯度的平坦区域中优化算法的表现。在这种情况下,基于梯度的优化程序很难达到最小值,因为它们无法有效地进行学习。

 

Beale 函数的曲面类似于神经网络的损失表面,在训练神经网络时,希望通过执行某种形式的优化来找到损失表面上的全局最小值 ,而最常采用的方法就是随机梯度下降。

 

首先定义 Beale 函数:

# define Beale's function which we want to minimize	
def objective(X):	x = X[0]; y = X[1]	return (1.5 - x + x*y)**2 + (2.25 - x + x*y**2)**2 + (2.625 - x + x*y**3)**2
 

接下来设置 Beale 函数的边界,以及格网的步长:

# function boundaries	
xmin, xmax, xstep = -4.5, 4.5, .9	
ymin, ymax, ystep = -4.5, 4.5, .9


然后,根据上述设置来制作格网,并准备寻找函数最小值

# Let's create some points	
x1, y1 = np.meshgrid(np.arange(xmin, xmax + xstep, xstep), np.arange(ymin, ymax + ystep, ystep))
 

我们先做一个初步猜测(通常很糟糕)

# initial guess	
x0 = [4., 4.]	
f0 = objective(x0)	
print (f0)


然后使用 scipy.optimize.minimize 函数并查看优化结果

bnds = ((xmin, xmax), (ymin, ymax))	
minimum = minimize(objective, x0, bounds=bnds)	
print(minimum)

640?wx_fmt=png


神经网络中的优化


神经网络的优化可以定义为如下的过程:网络预测——计算误差——再次预测测——试图最小化这个误差——再次预测...——直到误差不再降低。

 

在神经网络中,最常用的优化算法是梯度下降,梯度下降中使用的目标函数就是想要最小化的损失函数。

 

由于本教程的神经网络构建和优化过程是基于 Keras 搭建,所以在介绍优化过程之前,我们先回顾一下 Keras 的基本内容, 这将有助于理解后续的优化操作。


Keras 简介


Keras 是一个深度学习的 Python 库,它旨在快速简便地开发深度学习模型。Keras 建立在模型的基础上。Keras 有两种构建模型的方式,一种是 Sequential 模型,它是神经网络层的线性堆栈。另一种是 基于函数 API 构建模型,这是一种定义复杂模型的方法。

 

以 Sequential 方式为例,构建 Keras 深度学习模型的流程如下:


  1. 定义模型:创建 Sequential 模型并添加网络层。

  2. 编译模型:指定损失函数和优化器,并调用 .compile() 函数对模型进行编译。

  3. 模型训练:通过调用 .fit() 函数在数据上训练模型。

  4. 进行预测:调用 .evaluate() 或 .predict() 函数来对新数据进行预测。


为了在模型运行时检查模型的性能,需要用到回调函数(callbacks)


回调函数:在训练时记录模型性能


回调是在训练过程的给定阶段执行的一组函数,可以使用回调来获取训练期间模型内部状态和模型统计信息的视图。常用的回调函数如下:

 

  1. keras.callbacks.History() 记录模型训练的历史信息,该函数默认包含在 .fit() 中

  2. keras.callbacks.ModelCheckpoint()将模型的权重保存在训练中的某个节点。如果模型运行了很长时间并且中途可能发生系统故障,该函数将非常有用。

  3. keras.callbacks.EarlyStopping()当监控值停止改善时停止训练

  4. keras.callbacks.LearningRateScheduler() 在训练过程中改变学习率


接下来导入 keras 中一些必要的库和函数:

import tensorflow as tf	
import keras	
from keras import layers	
from keras import models	
from keras import utils	
from keras.layers import Dense	
from keras.models import Sequential	
from keras.layers import Flatten	
from keras.layers import Dropout	
from keras.layers import Activation	
from keras.regularizers import l2	
from keras.optimizers import SGD	
from keras.optimizers import RMSprop	
from keras import datasets	from keras.callbacks import LearningRateScheduler	
from keras.callbacks import History	from keras import losses	
from sklearn.utils import shuffle	print(tf.VERSION)	
print(tf.keras.__version_


如果希望网络使用随机数工作,而且期望结果可以复现,可以使用随机种子,相同的随机种子每次都会产生相同的数字序列。

# fix random seed for reproducibility	
np.random.seed(5)


第一步:确定网络的拓扑结构


使用 MNIST 数据集进行实验,该数据集由 28x28 手写数字(0-9)的灰度图像组成。每个像素为 8 位,取值范围为 0 到 255。获取数据的代码如下:

mnist = keras.datasets.mnist	
(x_train,y_train),(x_test,y_test)= mnist.load_data()	
x_train.shape,y_train.shape


X 和 Y 的尺寸分别为(60000,28,28)和(60000,1),我们可以使用如下代码来可视化数据集:

plt.figure(figsize=(10,10))	
for i in range(10):	plt.subplot(5,5,i+1)	plt.xticks([])	plt.yticks([])	plt.grid(False)	plt.imshow(x_train[i], cmap=plt.cm.binary)	plt.xlabel(y_train[i])


640?wx_fmt=png

 

最后,我查一下训练集和测试集的维度:

print(f'We have {x_train.shape[0]} train samples')	
print(f'We have {x_test.shape[0]} test samples')


我们一共有60,000张训练图像和10,000张测试图像,接下来要对数据进行预处理。


数据预处理


  • 首先,需要将 2D 图像转为 1D序列(展平),numpy.reshape()和 keras.layers.Flatten都可以实现展平操作。

  • 然后,使用如下公式来对数据进行标准化(0~1 标准化)

 

640?wx_fmt=png

 

在本例中,最小值为 0,最大值为255,因此公式简化为 ?:=? / 255,代码如下:

# normalize the data	
x_train, x_test = x_train / 255.0, x_test / 255.0	# reshape the data into 1D vectors	
x_train = x_train.reshape(60000, 784)	
x_test = x_test.reshape(10000, 784)	num_classes = 10	# Check the column length	
x_train.shape[1]


接下来对数据进行 one-hot 编码

# Convert class vectors to binary class matrices	
y_train = keras.utils.to_categorical(y_train, num_classes)	
y_test = keras.utils.to_categorical(y_test, num_classes)est,num_classes)


最后,就可以构建自己的模型了。


第二步:调整学习率


最常见的优化算法之一是随机梯度下降(SGD),SGD中可以进行优化的超参数有 learning rate,momentum,decay 和 nesterov。

 

Learning rate 控制每个 batch 结束时的模型权重,momentum控制先前权重更新对当前权重更新的影响程度,decay表示每次更新时的学习率衰减,nesterov 用于选择是否要使用 Nesterov动量,其取值为 “True” 或 “False” 。

 

这些超参数的典型值是 lr = 0.01,decay = 1e-6,momentum = 0.9,nesterov = True。

 

在训练过程中,不同学习率对 loss 的影响如下图所示:

 

640?wx_fmt=png


Keras 在 SGD 优化器中具有默认的学习率调整器,该调整器根据随机梯度下降优化算法,在训练期间降低学习速率,学习率的调整公式如下:

 

640?wx_fmt=png

 

接下来,我们将在 Keras 中实现学习率调整。将学习率的初始值设置为 0.1,然后将学习率衰减设为 0.0016,并将模型训练 60 个 epochs。此外,将动量值设为 0.8 。

epochs=60	
learning_rate = 0.1	
decay_rate = learning_rate / epochs	
momentum = 0.8	sgd = SGD(lr=learning_rate, momentum=momentum, decay=decay_rate, nesterov=False)

构建神经网络模型:

# build the model	
input_dim = x_train.shape[1]	lr_model = Sequential()	
lr_model.add(Dense(64, activation=tf.nn.relu, kernel_initializer='uniform',	input_dim = input_dim))	
lr_model.add(Dropout(0.1))	
lr_model.add(Dense(64, kernel_initializer='uniform', activation=tf.nn.relu))	
lr_model.add(Dense(num_classes, kernel_initializer='uniform', activation=tf.nn.softmax))	# compile the model	
lr_model.compile(loss='categorical_crossentropy',	optimizer=sgd,	metrics=['acc']


下面进行模型训练:

%%time	
# Fit the model	
batch_size = int(input_dim/100)	lr_model_history = lr_model.fit(x_train, y_train,	batch_size=batch_size,	epochs=epochs,	verbose=1,	validation_data=(x_test, y_test))


模型训练完成后,画出精度和误差随 epoch 变化的曲线:

# Plot the loss function	
fig, ax = plt.subplots(1, 1, figsize=(10,6))	
ax.plot(np.sqrt(lr_model_history.history['loss']), 'r', label='train')	
ax.plot(np.sqrt(lr_model_history.history['val_loss']), 'b' ,label='val')	
ax.set_xlabel(r'Epoch', fontsize=20)	
ax.set_ylabel(r'Loss', fontsize=20)	
ax.legend()	
ax.tick_params(labelsize=20)	# Plot the accuracy	
fig, ax = plt.subplots(1, 1, figsize=(10,6))	
ax.plot(np.sqrt(lr_model_history.history['acc']), 'r', label='train')	
ax.plot(np.sqrt(lr_model_history.history['val_acc']), 'b' ,label='val')	
ax.set_xlabel(r'Epoch', fontsize=20)	
ax.set_ylabel(r'Accuracy', fontsize=20)	
ax.legend()	
ax.tick_params(labelsize=20)


误差曲线如下图所示:


640?wx_fmt=png

 

精度曲线如下:


640?wx_fmt=png




使用 LearningRateScheduler 对学习率进行调节


我们可以定制一个指数衰减的学习率调整器:

 

??=??₀ × ?^(−??)

 

该过程和上节中的过程非常相似,为了比较两者的差异,将两者的代码写在一起,如下所示:

# solution	
epochs = 60	
learning_rate = 0.1 # initial learning rate	
decay_rate = 0.1	
momentum = 0.8	# define the optimizer function	
sgd = SGD(lr=learning_rate, momentum=momentum, decay=decay_rate, nesterov=False)	input_dim = x_train.shape[1]	
num_classes = 10	
batch_size = 196
# build the model	
exponential_decay_model = Sequential()	
exponential_decay_model.add(Dense(64, activation=tf.nn.relu, kernel_initializer='uniform', input_dim = input_dim))	
exponential_decay_model.add(Dropout(0.1))	
exponential_decay_model.add(Dense(64, kernel_initializer='uniform', activation=tf.nn.relu))	
exponential_decay_model.add(Dense(num_classes, kernel_initializer='uniform', activation=tf.nn.softmax)
# compile the model	
exponential_decay_model.compile(loss='categorical_crossentropy',	optimizer=sgd,	metrics=['acc'])	
# define the learning rate change	
def exp_decay(epoch):	lrate = learning_rate * np.exp(-decay_rate*epoch)	return lrate
# learning schedule callback	
loss_history = History()	
lr_rate = LearningRateScheduler(exp_decay)	
callbacks_list = [loss_history, lr_rate]	# you invoke the LearningRateScheduler during the .fit() phase	
exponential_decay_model_history = exponential_decay_model.fit(x_train, y_train,	batch_size=batch_size,	epochs=epochs,	callbacks=callbacks_list,	verbose=1,	validation_data=(x_test, y_test))	


可以发现,两者唯一的区别就是 exp_decay的有无,以及是否在 LearningRateScheduler 中调用。我们可以画出使用 exp_decay的模型的学习率曲线,学习率的衰减过程显得很平滑,如下图所示:

 

640?wx_fmt=png

 

误差的变化曲线也变得更加平滑:


640?wx_fmt=png


从上述曲线中我们看出,使用合适的学习率衰减策略,有利于提高神经网络的性能。


第三步:选择优化器(optimizer)和误差函数(loss function)


在构建模型并使用它来进行预测时,通过定义损失函数(或 目标函数)来衡量预测结果的好坏。 

 

在某些情况下,损失函数和距离测量有关。 距离测量方式取决于数据类型和正在处理的问题。例如,在自然语言处理(分析文本数据)中,汉明距离的使用最为常见。


距离度量


  • 欧式距离

  • 曼哈顿距离

  • 其他距离,如汉明距离等


损失函数


  • MSE(回归问题)

  • 分类交叉熵(分类问题)

  • 二元交叉熵(分类问题)

# build the model	
input_dim = x_train.shape[1]	model = Sequential()	
model.add(Dense(64, activation=tf.nn.relu, kernel_initializer='uniform',	input_dim = input_dim)) # fully-connected layer with 64 hidden units	
model.add(Dropout(0.1))	
model.add(Dense(64, kernel_initializer='uniform', activation=tf.nn.relu))	
model.add(Dense(num_classes, kernel_initializer='uniform', activation=tf.nn.softmax))	# defining the parameters for RMSprop (I used the keras defaults here)	
rms = RMSprop(lr=0.001, rho=0.9, epsilon=None, decay=0.0)	model.compile(loss='categorical_crossentropy',	optimizer=rms,	metrics=['acc'])


第四步:决定 batch 大小和 epoch 的次数


batch 大小决定了每次前向传播中的样本数目。使用 batch 的好处如下(前提是 batch size 小于样本总数):

 

  • 需要的内存更少。 由于使用较少的样本训练网络,因此整体训练过程需要较少的内存。 如果数据集太大,无法全部放入机器的内存中,那么使用 batch 显得尤为重要。

  • 一般来讲,网络使用较小的 batch 来训练更快。这是因为在每次前向传播后,网络都会更新一次权重。

 

epoch 的次数决定了学习算法对整个训练数据集的迭代次数。

 

一个 epoch 将训练数据集中的每个样本,一个 epoch 由一个或多个 batch 组成。选择 batch 大小或 epoch 的次数没有硬性的限制,而且增加 epoch 次数并不能保证取得更好的结果。

%%time	
batch_size = input_dim	
epochs = 60	model_history = model.fit(x_train, y_train,	batch_size=batch_size,	epochs=epochs,	verbose=1,	validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)	
print('Test loss:', score[0])	
print('Test accuracy:', score[1])	fig, ax = plt.subplots(1, 1, figsize=(10,6))	
ax.plot(np.sqrt(model_history.history['acc']), 'r', label='train_acc')	
ax.plot(np.sqrt(model_history.history['val_acc']), 'b' ,label='val_acc')	
ax.set_xlabel(r'Epoch', fontsize=20)	
ax.set_ylabel(r'Accuracy', fontsize=20)	
ax.legend()	
ax.tick_params(labelsize=20)	fig, ax = plt.subplots(1, 1, figsize=(10,6))	
ax.plot(np.sqrt(model_history.history['loss']), 'r', label='train')	
ax.plot(np.sqrt(model_history.history['val_loss']), 'b' ,label='val')	
ax.set_xlabel(r'Epoch', fontsize=20)	
ax.set_ylabel(r'Loss', fontsize=20)	
ax.legend()	
ax.tick_params(labelsize=20)


第五步:随机重启


该方法在keras中没有直接的实现,我们可以通过更改 keras.callbacks.LearningRateScheduler 来实现,它主要用于在一定 epoch 之后重置有限次 epoch 的学习率。


使用交叉验证来调节超参数


使用 Scikit-Learn 的 GridSearchCV ,可以自动计算超参数的几个可能值,并比较它们的结果。

 

为了使用 keras 进行交叉验证,可以使用 Scikit-Learn API 的包装器,该包装器使得 Sequential 模型(仅支持单输入)成为 Scikit-Learn 工作流的一部分。

 

有两个包装器可供使用:


  • keras.wrappers.scikit_learn.KerasClassifier(build_fn=None, **sk_params),它实现了Scikit-Learn 分类器接口。

  • keras.wrappers.scikit_learn.KerasRegressor(build_fn=None, **sk_params),它实现了Scikit-Learn 回归器接口。

import numpy	
from sklearn.model_selection import GridSearchCV	
from keras.wrappers.scikit_learn import KerasClassifier


尝试不同的权重初始值

# let's create a function that creates the model (required for KerasClassifier)	
# while accepting the hyperparameters we want to tune	
# we also pass some default values such as optimizer='rmsprop'	
def create_model(init_mode='uniform'):	# define model	model = Sequential()	model.add(Dense(64, kernel_initializer=init_mode, activation=tf.nn.relu, input_dim=784))	model.add(Dropout(0.1))	model.add(Dense(64, kernel_initializer=init_mode, activation=tf.nn.relu))	model.add(Dense(10, kernel_initializer=init_mode, activation=tf.nn.softmax))	# compile model	model.compile(loss='categorical_crossentropy',	optimizer=RMSprop(),	metrics=['accuracy'])	return model

%%time	
seed = 7	
numpy.random.seed(seed)	
batch_size = 128	
epochs = 10	model_CV = KerasClassifier(build_fn=create_model, epochs=epochs,	batch_size=batch_size, verbose=1)
# define the grid search parameters	
init_mode = ['uniform', 'lecun_uniform', 'normal', 'zero',	'glorot_normal', 'glorot_uniform', 'he_normal', 'he_uniform']	
param_grid = dict(init_mode=init_mode)	
grid = GridSearchCV(estimator=model_CV, param_grid=param_grid, n_jobs=-1, cv=3)	
grid_result = grid.fit(x_train, y_train)

# print results	
print(f'Best Accuracy for {grid_result.best_score_} using {grid_result.best_params_}')	
means = grid_result.cv_results_['mean_test_score']	
stds = grid_result.cv_results_['std_test_score']	
params = grid_result.cv_results_['params']	
for mean, stdev, param in zip(means, stds, params):	print(f' mean={mean:.4}, std={stdev:.4} using {param}')
 

GridSearch 的结果如下:

 

640?wx_fmt=png

 

可以看到最好的结果是使用 lecun_uniform 初始化或 glorot_uniform 初始化,在这两种初始化的基础上,我们的网络取得了接近 97% 的准确度。



将模型保存到 JSON 文件中


分层数据格式(HDF5)是用于存储大数组的数据存储格式,这包括神经网络中权重的值。HDF5 的安装可以使用如下命令 :pip install h5py

 

Keras 使用JSON格式保存模型的代码如下:

from keras.models import model_from_json	# serialize model to JSON	
model_json = model.to_json()	with open("model.json", "w") as json_file:	json_file.write(model_json)	# save weights to HDF5	
model.save_weights("model.h5")	
print("Model saved")	# when you want to retrieve the model: load json and create model	
json_file = open('model.json', 'r')	
saved_model = json_file.read()	
# close the file as good practice	
json_file.close()	
model_from_json = model_from_json(saved_model)	
# load weights into new model	
model_from_json.load_weights("model.h5")	
print("Model loade")


对多个超参数同时进行交叉验证


使用 GridSearch,可以同时对多个参数进行交叉验证,并有效地尝试它们的组合。

 

例如,可以搜索以下参数的不同的取值:


  • batch 大小

  • epoch 次数

  • 初始模式


这些选项将被指定到字典中,该字典将传递给 GridSearchCV。

 

注意:神经网络中的交叉验证在计算上是很昂贵的,每个组合都将使用 k 折交叉验证评估。

 

# repeat some of the initial values here so we make sure they were not changed	
input_dim = x_train.shape[1]	
num_classes = 10	# let's create a function that creates the model (required for KerasClassifier)	
# while accepting the hyperparameters we want to tune	
# we also pass some default values such as optimizer='rmsprop'	
def create_model_2(optimizer='rmsprop', init='glorot_uniform'):	model = Sequential()	model.add(Dense(64, input_dim=input_dim, kernel_initializer=init, activation='relu'))	model.add(Dropout(0.1))	model.add(Dense(64, kernel_initializer=init, activation=tf.nn.relu))	model.add(Dense(num_classes, kernel_initializer=init, activation=tf.nn.softmax))	# compile model	model.compile(loss='categorical_crossentropy',	optimizer=optimizer,	metrics=['accuracy'])	return mode

%%time	
# fix random seed for reproducibility (this might work or might not work	
# depending on each library's implenentation)	
seed = 7	
numpy.random.seed(seed)

# create the sklearn model for the network	
model_init_batch_epoch_CV = KerasClassifier(build_fn=create_model_2, verbose=1)	# we choose the initializers that came at the top in our previous cross-validation!!	
init_mode = ['glorot_uniform', 'uniform']	
batches = [128, 512]	
epochs = [10, 20]

# grid search for initializer, batch size and number of epochs	
param_grid = dict(epochs=epochs, batch_size=batches, init=init_mode)	
grid = GridSearchCV(estimator=model_init_batch_epoch_CV, 	param_grid=param_grid,	cv=3)	
grid_result = grid.fit(x_train, y_train)

# print results	
print(f'Best Accuracy for {grid_result.best_score_:.4} using {grid_result.best_params_}')	
means = grid_result.cv_results_['mean_test_score']	
stds = grid_result.cv_results_['std_test_score']	
params = grid_result.cv_results_['params']	
for mean, stdev, param in zip(means, stds, params):	print(f'mean={mean:.4}, std={stdev:.4} using {param}')

在结束之前,还有留有最后一个问题:如果在 GridSearchCV 中,需要搜索的参数量和取值空间都特别大,我们该怎么办?

 

这是一个特别麻烦的问题,想象一下,假设要优化 5 个参数,每个参数有 10 个潜在值,那么组合数将是 10⁵,这意味着我们必须训练一个非常大的网络。 显然,这种方式不切实际,所以通常使用 RandomizedCV 作为替代方案。

 

RandomizedCV 允许指定所有的潜在参数,然后在交叉验证中的每折中,它将选择参数的一个随机子集,对该子集进行验证。 最后,可以选择最佳的参数集并将其作为近似解。

 

原文链接: 

https://towardsdatascience.com/simple-guide-to-hyperparameter-tuning-in-neural-networks-3fe03dad8594


(*本文为 AI科技大本营编译文章,转载请联系1092722531


精彩推荐


“只讲技术,拒绝空谈”2019 AI开发者大会将于9月6日-7日在北京举行,这一届AI开发者大会有哪些亮点?一线公司的大牛们都在关注什么?AI行业的风向是什么?2019 AI开发者大会,倾听大牛分享,聚焦技术实践,和万千开发者共成长。


目前,大会盲订票限量发售中~扫码购票,领先一步!

                       

640?wx_fmt=jpeg

推荐阅读

  • 码农们的「血与泪」:新零售「全渠道中台」的前世今身

  • 腾讯拥抱开源:首次公布开源路线图,技术研发向共享、复用和开源迈进

  • 混合云发展之路:前景广阔,巨头混战

  • 干货 | Python后台开发的高并发场景优化解决方案

  • 5G 浪潮来袭!程序员在风口中有何机遇?

  • 这次又坑多少人? 深度解析 Dash 钱包"关键"漏洞!

  • 壕!两万多名腾讯员工获 51 万港元股票奖励


640?wx_fmt=png你点的每个“在看”,我都认真当成了喜欢

相关文章:

读好书,写好程序

本人是做.NET开发的,以企业应用为主,以互联网为爱好,这里给大家推荐一些适合.NET程序员的书: 软件设计《企业应用架构模式》 Martin Fowler 的大作之一,总结了多种常见的企业应用架构模式,这些模式是脱离具…

SIFT特征点匹配中KD-tree与Ransac算法的使用

转自:http://blog.csdn.net/ijuliet/article/details/4471311 Step1:BBF算法,在KD-tree上找KNN。第一步做匹配咯~ 1.什么是KD-tree(fromwiki) K-Dimension tree,实际上是一棵平衡二叉树。 一般的KD-tree构造过程&a…

jQuery带缩略图的宽屏焦点图插件

在线演示 本地下载

追溯XLNet的前世今生:从Transformer到XLNet

作者丨李格映来源 | 转载自CSDN博客导读:2019 年 6 月,CMU 与谷歌大脑提出全新 XLNet,基于 BERT 的优缺点,XLNet 提出一种泛化自回归预训练方法,在 20 个任务上超过了 BERT 的表现,并在 18 个任务上取得了当…

微软MCITP系列课程

http://liushuo890.blog.51cto.com/5167996/d-1转载于:https://blog.51cto.com/showcart/1156172

在Ubuntu11.10中安装配置OpenCV2.3.1和CodeBlocks

1、 打开终端; 2、 执行指令,删除ffmpeg and x264旧版本:sudo apt-get removeffmpeg x264 libx264-dev 3、下载安装x264和ffmpeg所有的依赖:sudo apt-get update sudo apt-get installbuild-essential checkinstall git cmake…

深入浅出Rust Future - Part 1

本文译自Rust futures: an uneducated, short and hopefully not boring tutorial - Part 1,时间:2018-12-02,译者:motecshine, 简介:motecshine 欢迎向Rust中文社区投稿,投稿地址,好文将在以下地方直接展示 Rust中文社区首页Rust…

cmd 修改文件属性

现在的病毒基本都会采用一种方式,就是将病毒文件的属性设置为系统隐藏属性以逃避一般用户的眼睛,而且由于Windows系统的关系,这类文件在图形界面下是不能修改其属性的。但是好在Windows还算做点好事,留下了一个attrib命令可以让我…

Django 视图

Django之视图 目录 一个简单的视图CBV和FBV FBV版:CBV版:给视图加装饰器 使用装饰器装饰FBV使用装饰器装饰CBVrequest对象 请求相关的常用值属性方法Response对象 使用属性JsonResponse对象Django shortcut functions render()redirect()Django的View&am…

喜大普奔!GitHub官方文档推出中文版

原创整理 | Python开发者(ID:PythonCoder)最近程序员交友圈出了一个大新闻,GitHub 帮助文档正式推出中文版了,之前一直都是只有英文文档,看起来费劲不方便。这份中文文当非常详尽,可以说有了它 …

Linux中获取当前程序路径的方法

1、命令行实现:转自:http://www.linuxdiyf.com/viewarticle.php?id84177 #!/bin/sh cur_dir$(pwd) echo $cur_dir 注意:在cur_dir后没空格,后面也不能有空格,不然它会认为空格不是路径而报错 2、程序实现&#xf…

android 关于字符转化问题

今日在写android的客户端,发现字符转化是个大问题。 下面是Unicode转UTF-8的转化,便于以后使用 private static String decodeUnicode(String theString) { char aChar; int len theString.length(); StringBuffer outBuffer new Strin…

30分钟看懂XGBoost的基本原理

作者 | 梁云1991转载自Python与算法之美(ID: Python_Ai_Road)一、XGBoost和GBDTxgboost是一种集成学习算法,属于3类常用的集成方法(bagging,boosting,stacking)中的boosting算法类别。它是一个加法模型,基模型一般选择树模型&…

Linux下遍历文件夹的实现

转自&#xff1a;http://blog.csdn.net/wallwind/article/details/7528474 linux C 遍历目录及其子目录 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <dirent.h> #include <sys/stat.h> #include <unistd.h&…

如何用Python画一棵漂亮的树

Tree海龟绘图turtle 在1966年&#xff0c;Seymour Papert和Wally Feurzig发明了一种专门给儿童学习编程的语言——LOGO语言&#xff0c;它的特色就是通过编程指挥一个小海龟&#xff08;turtle&#xff09;在屏幕上绘图。 海龟绘图&#xff08;Turtle Graphics&#xff09;后来…

windows7下,Java中利用JNI调用c++生成的动态库的使用步骤

1、从http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u2-download-1377129.html下载jdk-7u2-windows-i586.exe&#xff0c;安装到D:\ProgramFiles\Java&#xff0c;并将D:\ProgramFiles\Java\jdk1.7.0_02\bin添加到环境变量中&#xff1b; 2、从http://www.ec…

外观模式 - 设计模式学习

外观模式(Facade)&#xff0c;为子系统中的一组接口提供一个一致的界面&#xff0c;此模式定义了一个高层接口&#xff0c;这个接口使得这一子系统更加容易使用。 怎么叫更加容易使用呢&#xff1f;多个方法变成一个方法&#xff0c;在外观看来&#xff0c;只需知道这个功能完成…

Google最新论文:大规模深度推荐模型的特征嵌入问题有解了!

转载自深度传送门&#xff08;ID: gh_5faae7b50fc5&#xff09;导读&#xff1a;本文主要介绍下Google在大规模深度推荐模型上关于特征嵌入的最新论文。 一、背景大部分的深度学习模型主要包含如下的两大模块&#xff1a;输入模块以及表示学习模块。自从NAS[1]的出现以来&#…

[20181204]低版本toad 9.6直连与ora-12505.txt

[20181204]低版本toad 9.6直连与ora-12505.txt--//我们生产系统还保留有一台使用AMERICAN_AMERICA.US7ASCII字符集的数据库,这样由于toad新版本不支持该字符集的中文显示.--//我一直保留toad 9.6的版本,并且这个版本是32位的,我必须在我的机器另外安装10g 32位版本的客户端,这样…

Google揭露美国政府通过NSL索要用户资料

当美国联邦调查局FB或其他美国执法机构进行有关国家安全的调查时&#xff0c;能通过一种“国家安全密函National Security &#xff0c;NSL)”向服务商索取其用户的个人资料&#xff0c;由于事关国家安全&#xff0c;因此该密函并不需经法院同意。但根据美国电子通讯隐私法的规…

Ubuntu下,Java中利用JNI调用codeblocks c++生成的动态库的使用步骤

1、 打开新立得包管理器&#xff0c;搜索JDK&#xff0c;选择openjdk-6-jdk安装&#xff1b; 2、 打开Ubuntu软件中心&#xff0c;搜索Eclipse&#xff0c;选择Eclipse集成开发环境&#xff0c;安装&#xff1b; 3、 打开Eclipse&#xff0c;File-->New-->Java Proj…

比Hadoop快至少10倍的物联网大数据平台,我把它开源了

作者 | 陶建辉转载自爱倒腾的程序员&#xff08;ID: taosdata&#xff09;导读&#xff1a;7月12日&#xff0c;涛思数据的TDengine物联网大数据平台宣布正式开源。涛思数据希望尽最大努力打造开发者社区&#xff0c;维护这个开源的商业模式&#xff0c;他们相信不将最核心的代…

Script:挖掘AWR实现查询SCN历史增长走势

AWR中记录了快照时间内calls to kcmgas的统计值&#xff0c;calls to kcmgas的意义在于通过递归调用获得一个新的SCN&#xff0c;该统计值可以看做SCN增长速度的主要依据&#xff0c;通过挖掘AWR可以了解SCN的增长走势&#xff0c;对于我们诊断SCN HEADROOM问题有所帮助&#x…

运动目标检测__光流法

以下内容摘自一篇硕士论文《视频序列中运动目标检测与跟踪算法的研究》&#xff1a; 1950年Gibson首先提出了光流的概念&#xff0c;光流(optical flow)法是空间运动物体在观测成像面上的像素运动的瞬时速度。物体在运动的时候&#xff0c;它在图像上对应点的亮度模式也在做相…

读完这45篇论文,“没人比我更懂AI了”

作者 | 黄海广 转载自机器学习爱好者&#xff08;ID:ai-start-com&#xff09; 导读&#xff1a;AI领域的发展会是IT中最快的。我们所看到的那些黑科技&#xff0c;其后无不堆积了大量论文&#xff0c;而且都是最新、最前沿的论文。从某种角度来讲&#xff0c;它们所用的技术跟…

深入理解JVM——虚拟机GC

对象是否存活 Java的GC基于可达性分析算法(Python用引用计数法)&#xff0c;通过可达性分析来判定对象是否存活。这个算法的基本思想是通过一系列"GC Roots"的对象作为起始点&#xff0c;从这些节点开始向下搜索&#xff0c;搜索所走过的路径称为引用链&#xff0c;当…

​2019年最新华为、BAT、美团、头条、滴滴面试题目及答案汇总

作者 | 苏克1900来源 | 高级农民工&#xff08;ID&#xff1a;Mocun6&#xff09;【导语】最近 GitHub 上一个库火了&#xff0c;总结了 阿里、腾讯、百度、美团、头条等国内主流大厂的技术面试题目&#xff0c;目前 Star 2000&#xff0c;还在持续更新中&#xff0c;预计会火下…

华胜天成ivcs云系统初体验2

重启完成以后&#xff0c;就看到传统的linux init3级别的登录界面。输入用户名root 密码&#xff1a;123456 &#xff08;默认&#xff09;接下来的工作是配置一些东西&#xff0c;让它跑起来。首先&#xff0c;要修改IP地址&#xff0c;还有机器名。输入命令&#xff1a;ivcs…

OpenCV中响应鼠标信息cvSetMouseCallback函数的使用

转自&#xff1a;http://blog.csdn.net/haihong84/article/details/6599838 程序代碼如下&#xff1a; #include <cv.h> #include <highgui.h> #include <stdio.h void onMouse(int event,int x,int y,int flags,void* param ); int main(int argc, char** …