所以我试图建立一个模型,需要数百个输入,每个输入是一行的图像(它不是一个真正的图像,数据刚刚放在那种形式)。
所以我有两个生成器,一个创建单个X训练示例,另一个加载单个Y训练示例。y生成器工作得很好,但是X生成器给我带来了问题。我觉得这与我如何设置特定的output_types有关,但似乎什么都不起作用:
代码片段:
def x_data_gen():
for i in range(num_exp):
filepath = fns[i]
im = Image.open(filepath)
im = np.array(im, dtype=np.float32)
inputs = []
for n in range(num_inputs):
inputs.append( np.reshape(im[n,:], (1,6000,1)) )
yield inputs
def y_data_gen():
for i in range(num_exp):
filepath = fns_y
im = Image.open(filepath)
y = np.reshape(np.array(im, dtype=np.float32), (1,6000,1))
yield y
x_types = [ tf.float32 for _ in range(num_exp) ]
y_types = tf.float32
inputs = tf.data.Dataset.from_generator(x_data_gen, output_types=x_types)
y = tf.data.Dataset.from_generator(y_data_gen, output_types=y_types)
误差
File "start_spikes2_mutliinput2.py", line 67, in <module>
inputs = tf.data.Dataset.from_generator(x_data_gen, output_types=x_types)
File "C:UserswmkcAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythonutildeprecation.py", line 535, in new_func
return func(*args, **kwargs)
File "C:UserswmkcAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythondataopsdataset_ops.py", line 904, in from_generator
output_signature = nest.map_structure_up_to(output_types,
File "C:UserswmkcAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythondatautilnest.py", line 444, in map_structure_up_to
results = [func(*tensors) for tensors in zip(*all_flattened_up_to)]
File "C:UserswmkcAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythondatautilnest.py", line 444, in <listcomp>
results = [func(*tensors) for tensors in zip(*all_flattened_up_to)]
File "C:UserswmkcAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythonframeworktensor_spec.py", line 56, in __init__
self._dtype = dtypes.as_dtype(dtype)
File "C:UserswmkcAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythonframeworkdtypes.py", line 647, in as_dtype
raise TypeError("Cannot convert value %r to a TensorFlow DType." %
TypeError: Cannot convert value [tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32] to a TensorFlow DType.
在x_data_gen()
中尝试此循环for n in range(num_inputs):
a,b = im[n,:].shape
inputs.append( np.reshape(im[n,:], (1,a*b,1)))