程序结构&&程序设计
来源:互联网 发布:大数据舆情开源 编辑:程序博客网 时间:2024/06/04 11:45
程序结构&&程序设计
程序结构&&程序设计(二)
程序结构&&程序设计(三) ——递归
程序结构&&程序设计(三)
程序结构&&程序设计(四)
mask
设置掩码(0/1构成的数组或者矩阵)的目的是为了实现对原始数据的屏蔽(0)与选择(1)。
掩码生成的方式之一可以是:使用二项分布binomial distribution,
def get_corrupted_input(self, input, corruption_level): mask = self.theano_rng.binomial( n=1, p=1-corruption_level, size=input.shape, dtype=theano.config.floatX ) return mask*input
workhorse的运行时间
import timeit start_time = timeit.default_timer()for j in xrange(epochs): for i in xrange(n_train_batches): ...end_time = timeit.default_timer()print('Elapsed time %.2fm'%(end_time-start_time)/60.)
封装
if sys.platform == "win32": # On Windows, the best timer is time.clock() default_timer = time.clockelse: # On most other platforms the best timer is time.time() defalut_time = time.time
我们在更外层调用timeit.default_timer()
函数时,是不必关心具体的操作系统细节的。所以,封装的目的是为了屏蔽更底层的细节。
找寻序列中的最值
一般流程为:
best_validation_loss = np.inffor j in range(epochs): this_validation_loss = calc() if this_validation_loss < best_validation_loss: best_validation_loss = this_validation_loss # 以最小为最好
更为完整的代码如下:
best_validation_loss = np.inffor j in range(epochs): for i in range(n_train_batches): iter = j*n_train_batches+i if iter % validation_freq == 0: validation_losses = [valid_model(i) for i in range(n_valid_batches)] this_validation_loss = np.mean(validation_losses) if this_validation_loss < best_validation_loss: best_validation_loss = this_validation_loss
我们对上述问题进一步细分,也即再多加一个判断:
improvment_thresh = 0.95if this_validation_loss < best_validation_loss: if this_validation_loss < best_validation_loss*improvment_thresh: # 也即当前迭代改进幅度足够大,
patience 耐心值
也即是该值以下不予追究,姑妄听之。该值以上,则要采取另外的动作,也即「我的忍耐是有限度的」。判断是否到达耐心值,可以使用if判断,也可采用取模运算。如下代码所示:
patience = 10000validation_freq = min(n_train_batches, patience/2)for j in range(epochs): for minibatch_idx in n_train_batches: iter = j*n_train_batches + minibatch_idx if iter%100 == 0: print 'training @ iter = ', iter cost_ij = train_model(minibatch_idx) if (iter+1)%validation_freq == 0: ...
以两值中的较小值作为判断的基准
validation_freq = min(n_train_batches, patience/2)for j in range(epochs): for i in n_train_batches: iter = j*n_train_batches+i if iter % validation_freq == 0: ...
循环
考虑如下的双条件(and的关系)循环:
epoch = 0done_looping = Falsewhile epoch < epochs and (not done_looping): epoch += 1 。。。
两层循环
外层表示epoch,一次epoch,表示对全体样本进行一次学习或者叫训练。内层对全体样本进行分块mini_batch,内层循环表示的对样本块的遍历。
for j in epochs: for mini_batch_idx in n_train_index: iter = j*n_train_index + mini_batch_idx # iter 以mini_batch为单位 if iter%100 == 0: print 'training @ iter = ', iter
在迭代的不同阶段输出不同的信息
for j in xrange(epochs): for i in xrange(n_train_batches): iter = j*n_train_batches+i if iter % 100 == 0: print 'training @ iter = ', iter if (iter+1) % validation_freq == 0: print 'epoch {}, minibatch {}/{}'.format(j, i+1, n_train_batches)
python 中的for与while
当表达双条件限制时,可能while会比for更方便点:
# 单条件限制for j in range(epochs):epoch = 0while j < epochs: j += 1# 双条件限制时j = 0done_looping = Falsewhile j < epochs and not done_looping:
python程序的测试
def main(): passif __name__ == '__main__': main()
epochs
在进行每次的epoch时,我们需要记录执行完当前epoch时的一些中间结果(如使用list容器,training\_cost, training\_accuracy=[], []
),便于最后的过程可视化,如全局代价函数随epoch次数的变化情况,以及分类精确率随epochs次数的变化情况,顺便地此时也需要定义相关的calc_cost
以及calc_accuracy
,在程序运行的过程中,我们也可以在控制台打印相关的信息,print('Epoch {}: cost: {}, accuracy: {}'.format(j, cost, accuracy))
。
training_cost, training_accuracy = [], []for j in range(epochs): cost = calc_cost() accuracy = calc_accuracy() training_cost.append(cost) training_accuracy.append(accuracy) print('Epoch {}: cost: {}, accuracy: {}'.format(j, cost, accuracy))
统计分类正确出现的次数
sum(int(y_pred == y_test) for y_pred, y_test in zip(predications, test_label))
实现数据集的分块
n = X.shape[0]batches = [X[k:k+batch_size, :] for k in range(0, n, batch_size)]
在SGD(stochastic gradient descent)的学习算法中,常常在分块之前需要将数据集shuffle的过程:
n = X.shape[0]
记录某单词,某元素出现的次数,天然的使用字典或者map作为容器
// C++, mapstd::map<std::string, size_t> words;std::ifstream ifs(filename);std::string word;while(ifs >> word){ ++words[word];}
# python, defaultdictfrom collections import defaultdictdensities = defaultdict(float)digit_counts = defaultdict(int)for image, digit in zip(training_data[0], training_data[1]): digit_counts[digit] += 1 densities[digit] = sum(image)
实现一种自更新
这里
for w, b in zip(weights, biases): a = sigma(np.dot(w, a)+b)
设计函数接口时,尤其注意参数是向量还是矩阵
这种对参数向量还是矩阵的考虑,在进行机器学习相关算法的编程实践中,是需要注意的一个问题。这本身又跟python(numpy)对数组(一维还是二维)的处理方式不太一致有关。
- 程序结构&&程序设计
- 程序结构&&程序设计(二)
- 程序结构&&程序设计(三)
- 程序结构&&程序设计(四)
- 程序结构
- 程序结构
- 程序结构&&程序设计(三) ——递归及递归举例
- Arduino程序设计基础-2.1arduino语言及程序结构&&2.2C/C++语言基础
- JavaScript5程序结构
- Palm程序结构
- 2.1程序结构
- 游戏程序结构
- C#程序结构
- Delphi程序结构
- Android程序结构
- Delphi程序结构
- Java程序结构
- shell 程序结构
- 趣味动态图!机械原理与飞机是怎么飞的
- Myeclipse 10 Maven 构建 Java Web 项目
- 寻找山顶(C程序设计进阶第6周)
- Android官方文档阅读之旅——Application Fundamentals
- [C] 老鼠走迷宫(使用回圈+堆叠)
- 程序结构&&程序设计
- 使用Source Insight查看Android Framework 源码
- Temp
- 内核阅读之浅析Linux2.6.34内核桥接数据转发(二)
- poj3279 kuangbin带你飞 搜索 Fliptile
- POJ 1195 Mobile phones 【二维树状数组】
- 2015年Java代码之01
- 随意细解:UI --UITableView编辑
- shell脚本打印日志方法