具有多个张量输入的Keras fit_generator

时间:2019-04-06 20:07:35

标签: python tensorflow keras

我有一个Keras模型,带有4个张量输入和一个数组输出。使用model.fit方法可以正常工作

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])

现在我将model.fit更改为model.fit_generator

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))

dataGenerator中,每个张量都按如下方式切片

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)

但是我收到此错误:

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'

我知道它需要一些整数变量,但是它会获取其他类型的数据。我不明白哪种参数类型是错误的。我将所有{1 {1}}和stepsPerEpoch之类的变量都转换为整数。同样,似乎生成器工作正常,在调试模式下,它返回samples作为输入,返回[<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>]作为输出。

1 个答案:

答案 0 :(得分:2)

在模型定义中,您可以使用name参数指定每个图层的名称。例如:

x1in = Input(shape = x1[0].shape, name='in1')
x2in = Input(shape = x2[0].shape, name='in2')

在生成器中,您可以生成一个字典以适合数据:

yield {'in1' : in1, 'in2' : in2}, out

最后,像往常一样安装发电机:

model.fit_generator(datagen, epochs=300, steps_per_epoch=int(data_size/batch_size))