如何使用具有多个输入的keras生成序列数据?
How to generate sequence data with keras with multiple input?
我正在为 keras 中的序列到序列问题编写 VAE。解码器是一个自回归模型,所以我有两个不同的输入,一个用于编码器,一个用于解码器(移位 1,但这不是问题)。
我还想进行数据扩充,所以我决定使用 fit_generator() 方法,但我在 returning 两个输入时遇到了一些问题。
我试过return两个输入向量的列表,像这样
class DataGenerator(Sequence):
def __init__(....
def __getitem__(self, index):
data = create_data()
return [data, data]
或这样的字典
return {"encoder_input_name" : "data, decoder_input_name" : data }
其中数据是形状为 (batch_size、max_sequence_len、input_dimention) 的 numpy 张量。
我不能只使用相同的输入层,因为稍后两个输入会有点不同,正如我所说,解码器输入将移动一个具有不同的第一个元素和其他原因。
当我 return 列表 [data, data] 或出现此错误时:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays
当我 return 字典时我有这个错误:
batch_size = x.shape[0]
AttributeError: 'str' object has no attribute 'shape'
我该如何解决这个问题?
非常感谢!
编辑
我将 __getitem__
的输出更改为
[inpuut_1, input_2], []
成功了。
您应该 return 来自 generator/Sequence 实例的 元组 。元组的第一个元素是输入数组列表(如果您的模型有一个输入层,则只是一个数组),第二个元素是输出数组列表(如果您的模型有一个输出层,则只是一个数组)。
因此,__getitem__
应该 return 像这样:
def __getitem__(self, index):
# ...
return [inp_arr1, inp_arr2, ...], [out_arr1, out_arr2, ...] # IMPORTANT: this is a tuple
我正在为 keras 中的序列到序列问题编写 VAE。解码器是一个自回归模型,所以我有两个不同的输入,一个用于编码器,一个用于解码器(移位 1,但这不是问题)。 我还想进行数据扩充,所以我决定使用 fit_generator() 方法,但我在 returning 两个输入时遇到了一些问题。
我试过return两个输入向量的列表,像这样
class DataGenerator(Sequence):
def __init__(....
def __getitem__(self, index):
data = create_data()
return [data, data]
或这样的字典
return {"encoder_input_name" : "data, decoder_input_name" : data }
其中数据是形状为 (batch_size、max_sequence_len、input_dimention) 的 numpy 张量。
我不能只使用相同的输入层,因为稍后两个输入会有点不同,正如我所说,解码器输入将移动一个具有不同的第一个元素和其他原因。
当我 return 列表 [data, data] 或出现此错误时:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays
当我 return 字典时我有这个错误:
batch_size = x.shape[0]
AttributeError: 'str' object has no attribute 'shape'
我该如何解决这个问题?
非常感谢!
编辑
我将 __getitem__
的输出更改为
[inpuut_1, input_2], []
成功了。
您应该 return 来自 generator/Sequence 实例的 元组 。元组的第一个元素是输入数组列表(如果您的模型有一个输入层,则只是一个数组),第二个元素是输出数组列表(如果您的模型有一个输出层,则只是一个数组)。
因此,__getitem__
应该 return 像这样:
def __getitem__(self, index):
# ...
return [inp_arr1, inp_arr2, ...], [out_arr1, out_arr2, ...] # IMPORTANT: this is a tuple