I want to create a data generator in Python, based on the "fit_generator" function at https://keras.io/models/sequential/. The code for the function is:
def generate_arrays_from_file():
while True:
with open('data.npz') as f:
for line in f:
# TODO
yield ({'input': x}, {'output': y})
In the line TODO
, I need to assign some data from f
to x
and y
.
Now, the file 'data.npz' is actually a zipped NumPy file. This was created by:
x = random_numpy_array() # Create a NumPy array (details not important)
y = random_numpy_array()
np.savez('data.npz', x=x, y=y)
Usually, you would read x
and y
by using:
data = np.load('data.npz')
x = data['x']
y = data['y']
However, in my example (the first block of code above), I have not loaded the data using np.load()
. Instead, I have loaded it using with open('data.npz') as f
.
To read x
and y
from f
, I have tried:
x = f['x']
y = f['y']
But this gives me the error:
TypeError: '_io.TextIOWrapper' object is not subscriptable
So how can I read f
and extract x
and y
?