File size: 1,291 Bytes
83d5adb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 28 11:25:26 2023

@author: peter
"""
import keras
import tensorflow
import tqdm

class Batcher(keras.utils.Sequence):
    
    def __init__(self,source,batch_size=32):
        self.batches = None
        self.source=source
        self.batch_size=batch_size
        self.on_epoch_end()
        
    def __len__(self):
        return len(self.batches)
    
    def __getitem__(self, index):
        return self.batches[index]
    
    def on_epoch_end(self):
        self.batches = []
        n=0
        X=[]
        Y=[]
        Z=[]
        for (x,y,z) in tqdm.tqdm(self.source):
            X.append(x)
            Y.append(y)
            Z.append(z)
            n+=1
            if n==self.batch_size:
                self.batches.append((tensorflow.ragged.constant(X),
                                     tensorflow.ragged.constant(Y),
                                     tensorflow.ragged.constant(Z)))
                n=0
                X=[]
                Y=[]
                Z=[]
        if n!=0:
            self.batches.append((tensorflow.ragged.constant(X),
                                 tensorflow.ragged.constant(Y),
                                 tensorflow.ragged.constant(Z)))