嗨,我试图在一个模型中输入多个数据集。这是我的问题的一个例子,但是在我的例子中,我的一个模型有两个输入参数,而另一个有一个。我遇到的错误是:
Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'tensorflow.python.data.ops.dataset_ops.BatchDataset'>", "<class 'tensorflow.python.data.ops.dataset_ops.TakeDataset'>"}), <class 'NoneType'>代码:
import tensorflow as tf
# Create first model
model1 = tf.keras.Sequential()
model1.add(tf.keras.layers.Dense(1))
model1.compile()
model1.build([None,3])
# Create second model
model2 = tf.keras.Sequential()
model2.add(tf.keras.layers.Dense(1))
model2.compile()
model2.build([None,3])
# Concatenate
fusion_model = tf.keras.layers.Concatenate()([model1.output, model2.output])
t = tf.keras.layers.Dense(1, activation='tanh')(fusion_model)
model = tf.keras.models.Model(inputs=[model1.input, model2.input], outputs=t)
model.compile()
#Datasets
ds1 = tf.data.Dataset.from_tensors(([1,2,3],1))
ds2 = tf.data.Dataset.from_tensors(([1,2,3], 2))
print(ds1)
print(ds2)
# Fit
model.fit([ds1,ds2])运行此示例代码将产生以下结果:
Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'tensorflow.python.data.ops.dataset_ops.TensorDataset'>"}), <class 'NoneType'>我需要使用dataset模块,因为它们在构建的延迟加载中提供数据。
发布于 2020-10-04 04:43:52
正如注释中所指出的,TensorFlow .fit函数在TensorFlow模型中不支持数据集列表。
如果您真的想使用数据集,可以使用字典作为输入,并指定输入层来匹配dict。
你是怎么做到的:
model1 = tf.keras.Sequential(name="layer_1")
model2 = tf.keras.Sequential(name="layer_2")model.summary()
ds1 = tf.data.Dataset.from_tensors(({"layer_1": [[1,2,3]],
                                     "layer_2": [[1,2,3]]}, [[2]]))
model.fit(ds1)一个更简单的选择是简单地使用张量而不是数据集。.fit支持作为输入的张量列表,所以只需使用它。
model = tf.keras.models.Model(inputs=[model1.input, model2.input], outputs=t)
model.compile(loss='mse')
model.summary()
a = tf.constant([[1, 2, 3]])
b = tf.constant([[1, 2, 3]])
c = tf.constant([[1]])
model.fit([a, b], c)发布于 2022-08-19 14:49:44
如果您感兴趣,还可以使用tf.data.Dataset.zip()和字典解决多输入问题。最近,我遇到了一个类似的问题,我需要将图像和值向量输入到单个模型中,在这个模型中,它们可以连接中间模型。
我使用tfdata_unzip()函数从here中将图像张量从最初使用image_dataset_from_directory()函数创建的标签张量中解压缩。然后,我使用tf.data.Dataset.zip()重新压缩数据集。
在定义模型时,我使用了Functional并为每个输入层分配了一个名称:
import tensorflow as tf
from tensorflow.keras.layers import *
# create input image layer
in_image = Input(shape=(1305,2457,3), name='input_image')
# create input vector layer
in_vector = Input(shape=(1,), name='input_vector')我的整个工作流程类似于以下内容:
# use tfdata_unzip() to separate input images from labels
input_images, input_labels = tfdata_unzip(input_dat)
# input vector was created using tf.data.Dataset.from_tensor_slices()
# using [1,2,3,4] as a placeholder for my original vector of values
in_vector = tf.data.Dataset.from_tensor_slices([1,2,3,4])
# create a new input Dataset using .zip()
# data is structured as (1) a dictionary of inputs (input_images,in_vector) and (2) their associated labels (input_labels)
model_inputs = tf.data.Dataset.zip(({"input_image":input_images, "input_vector":in_vector}, input_labels))
# if you then wanted to batch, cache, and/or prefetch the dataset you could do so using the following
batchsize = 32
model_inputs = model_inputs.batch(batchsize).cache().prefetch(buffer_size=tf.data.AUTOTUNE)然后,可以通过调用类似于以下内容的内容来匹配该模型:
model.fit(inputs=model_inputs, outputs=predicted_class)因为model_inputs是一个带有标签的数据集,所以不需要在model.fit()调用中定义一个y=input_labels。
我还应该提到,我对验证数据进行了相同的数据重构,并通过添加model.fit()函数将其传递给validation_data=model_validation_data,其中"model_validation_data“类似于model_inputs结构。
这正是我如何能够解决这个问题的多输入到一个TF多模模型。乐于讨论任何出现的问题或其他解决方案。
发布于 2022-02-18 09:39:22
在使用两个使用text_dataset_from_directory函数构建的数据集时,我也遇到了同样的问题。对我来说,连接数据集并不是一个解决方案,因为每个数据集都可能通过不同的Keras层。所以我所做的就是构建一个定制的"fit_generator“。这将将Dataset对象转换为Keras支持的多输入数组。
def fit_generator(dataset, batch_size):
  X = []
  y = []
  for string_, int_ in dataset.batch(1):
    for i in range(0, len(int_[0])):
      X.append(string_[0][i].numpy())
      y.append(int_[0][i].numpy())
  X_ret = pd.DataFrame(X).to_numpy()
  y_ret = pd.DataFrame(y).to_numpy()
  return X_ret, y_ret然后就可以去构造数据集了。
train_X1, train_y1 = fit_generator(train_ds_1, batch_size)
train_X2, train_y2 = fit_generator(train_ds_2, batch_size)
val_X1, val_y1 = fit_generator(val_ds_1, batch_size)
val_X2, val_y2 = fit_generator(val_ds_2, batch_size)然后,您可以使用命名输入创建字典。
train_X = {'Input1': train_X1, 'Input2': train_X2}
train_y = {'Input1': train_y1, 'Input2': train_y2}
val_X = {'Input1': val_X1, 'Input2': val_X2}
val_y = {'Input1': val_y1, 'Input2': val_y2}然后,您可以像这样调用fit方法
model.fit(x=train_X, y=train_y1, validation_data=[val_X,val_y1], batch_size=batch_size, epochs=10)https://stackoverflow.com/questions/63979750
复制相似问题