博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Making training mini-batches
阅读量:7212 次
发布时间:2019-06-29

本文共 3325 字,大约阅读时间需要 11 分钟。

Here is where we'll make our mini-batches for training. Remember that we want our batches to be multiple sequences of some desired number of sequence steps. Considering a simple example, our batches would look like this:

We have our text encoded as integers as one long array in encoded. Let's create a function that will give us an iterator for our batches. I like using  to do this. Then we can pass encoded into this function and get our batch generator.

The first thing we need to do is discard some of the text so we only have completely full batches. Each batch contains N×MN×M characters, where NN is the batch size (the number of sequences) and MM is the number of steps. Then, to get the number of batches we can make from some array arr, you divide the length of arr by the batch size. Once you know the number of batches and the batch size, you can get the total number of characters to keep.

After that, we need to split arr into NN sequences. You can do this using arr.reshape(size) where size is a tuple containing the dimensions sizes of the reshaped array. We know we want NN sequences (n_seqs below), let's make that the size of the first dimension. For the second dimension, you can use -1 as a placeholder in the size, it'll fill up the array with the appropriate data for you. After this, you should have an array that is N×(MK)N×(M∗K) where KK is the number of batches.

Now that we have this array, we can iterate through it to get our batches. The idea is each batch is a N×MN×M window on the array. For each subsequent batch, the window moves over by n_steps. We also want to create both the input and target arrays. Remember that the targets are the inputs shifted over one character. You'll usually see the first input character used as the last target character, so something like this:

y[:, :-1], y[:, -1] = x[:, 1:], x[:, 0]

where x is the input batch and y is the target batch.

The way I like to do this window is use range to take steps of size n_steps from 00 to arr.shape[1], the total number of steps in each sequence. That way, the integers you get from range always point to the start of a batch, and each window is n_steps wide.

 

1 def get_batches(arr, n_seqs, n_steps): 2     '''Create a generator that returns batches of size 3        n_seqs x n_steps from arr. 4         5        Arguments 6        --------- 7        arr: Array you want to make batches from 8        n_seqs: Batch size, the number of sequences per batch 9        n_steps: Number of sequence steps per batch10     '''11     # Get the number of characters per batch and number of batches we can make12     characters_per_batch = n_seqs * n_steps13     n_batches = len(arr) // characters_per_batch14     15     # Keep only enough characters to make full batches16     arr = arr[:n_batches*characters_per_batch]17     18     # Reshape into n_seqs rows19     arr = arr.reshape((n_seqs, -1))20     21     for n in range(0, arr.shape[1], n_steps):22         # The features23         x = arr[:, n:n+n_steps]24         # The targets, shifted by one25         y = np.zeros_like(x)26         y[:, :-1], y[:, -1] = x[:, 1:], x[:, 0]27         yield x, y28 29 batches = get_batches(encoded, 10, 50)30 x, y = next(batches)31 32 print('x\n', x[:10, :10])33 print('\ny\n', y[:10, :10])

 

转载于:https://www.cnblogs.com/knownx/p/8082445.html

你可能感兴趣的文章
Lock-Free 编程
查看>>
AutoCompleteTextView 和 TextWatcher 详解
查看>>
2.5. SciTE
查看>>
自制简单表单验证relative与absolute定位
查看>>
C标准函数库中获取时间与日期、对时间与日期数据操作及格式化
查看>>
WebGIS中解决使用Lucene进行兴趣点搜索排序的两种思路
查看>>
驱动继电器实验
查看>>
技术宅---我的网上抢火车票攻略
查看>>
Android 使用dagger2进行依赖注入(基础篇)
查看>>
如何让帝国CMS7.2搜索模板支持动态标签调用
查看>>
《Oracle DBA工作笔记》第一章
查看>>
全面剖析Redis Cluster原理和应用 (good)
查看>>
PostgreSQL学习手册(常用数据类型)
查看>>
Visual Studio 2013 Xamarin for iOS 环境搭建
查看>>
服务端I/O性能:Node、PHP、Java、Go的对比
查看>>
注解的原理又是怎么一回事
查看>>
【PMP】Head First PMP 学习笔记 第十章 沟通管理
查看>>
阿里巴巴发布AliOS品牌 重投汽车及IoT领域
查看>>
怎么建立网站?
查看>>
剖析 epoll ET/LT 触发方式的性能差异误解(定性分析)
查看>>