Reputation: 1245
I have a Keras model with 4 tensor input and one array output. It works properly using model.fit
method
model.fit([inputUM, inputMU, inputUU, inputMM],outputY , validation_data=([inputTestUM, inputTestMU, inputTestUU, inputTestMM],outputTest), batch_size=None, epochs=3, steps_per_epoch=200,validation_steps=200, callbacks=[checkpointer])
Now I change model.fit
to model.fit_generator
as
batchSize = 10
samples = int(outputY.shape[0]) #number of all samples
stepsPerEpoch = int(samples/batchSize)
model.fit_generator(dataGenerator(inputUM, inputMU, inputUU, inputMM, outputY, batchSize),
steps_per_epoch=stepsPerEpoch,
epochs=2,
verbose=1,
callbacks=[checkpointer],
validation_data=dataGenerator(inputTestUM, inputTestMU, inputTestUU, inputTestMM, outputTest, batchSize),
validation_steps=stepsPerEpoch))
In the dataGenerator
each tensor is sliced as following
def dataGenerator(inputUM, inputMU, inputUU, inputMM, outputY, batchSize):
samples = int(outputY.shape[0]) #number of all samples
batchNumber = int(samples/batchSize) #number of batches
counter=0
while True:
if counter > batchNumber: #restart counter at the end of each epoch
counter = 0
inUM = tf.slice(inputUM,[counter*batchSize,0,0],[batchSize,60,1])
inMU = tf.slice(inputMU,[counter*batchSize,0,0],[batchSize,60,1])
inUU = tf.slice(inputUU,[counter*batchSize,0,0],[batchSize,60,1])
inMM = tf.slice(inputMM,[counter*batchSize,0,0],[batchSize,60,1])
outY = outputY[counter*batchSize:(counter+1)*batchSize]
counter += 1
yield ([inUM, inMU, inUU, inMM], outY)
However I get this error:
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "mycode.py", line 529, in <module>
main(data)
File "mycode.py", line 236, in main
initial_epoch=0)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
initial_epoch=initial_epoch)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training_generator.py", line 223, in fit_generator
callbacks.on_batch_end(batch_index, batch_logs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\callbacks.py", line 115, in on_batch_end
callback.on_batch_end(batch, logs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\callbacks.py", line 238, in on_batch_end
self.totals[k] = v * batch_size
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 410, in __rmul__
return self * other
TypeError: unsupported operand type(s) for *: 'Dimension' and 'float'
I know it needs some integer variable, however it gets other type data. I can't understand which parameter type is wrong. I cast all1 variables like stepsPerEpoch
and samples
, to integer. Also it seems the generator works properly, in debug mode it returns [<tf.Tensor 'Slice_48:0' shape=(100, 60, 1) dtype=float32>, <tf.Tensor 'Slice_49:0' shape=(100, 60, 1) dtype=float32>, <tf.Tensor 'Slice_50:0' shape=(100, 60, 1) dtype=float32>, <tf.Tensor 'Slice_51:0' shape=(100, 60, 1) dtype=float32>]
as input and array([4., 5., 4., ..., 4., 4., 5.])
as output.
Upvotes: 1
Views: 409
Reputation: 21
In model definition, you could use name
parameter to give the name of each layer. For example:
x1in = Input(shape = x1[0].shape, name='in1')
x2in = Input(shape = x2[0].shape, name='in2')
In your generator, you can yield a dict to fit the data:
yield {'in1' : in1, 'in2' : in2}, out
Finally, just fit the generator as usual:
model.fit_generator(datagen, epochs=300, steps_per_epoch=int(data_size/batch_size))
Upvotes: 2