将 x_train 作为 numpy 数组列表传递给 tf.data.Dataset 无效
Passing x_train as a list of numpy arrays to tf.data.Dataset is not working
我的问题是 tf.data.Dataset.from_tensor_slices(x_train, y_train) 中的 x_train 需要是一个列表。当我使用以下行将 [x1_train、x2_train] 传递给 tensorflow.data.Dataset.from_tensor_slices 时,出现错误(x1_train、x2_train 和 y_train是 numpy 数组):
Train=tensorflow.data.Dataset.from_tensor_slices(([x1_train,x2_train], y_train)).batch(batch_size)
错误:
Train=tensorflow.data.Dataset.from_tensor_slices(([x1_train,x2_train], y_train)).batch(batch_size)
return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Can't convert non-rectangular Python sequence to Tensor.
我该怎么办?
如果主要目标是将数据提供给具有多个输入层的模型,那么以下内容可能会有所帮助:
import tensorflow as tf
from tensorflow import keras
import numpy as np
def _input_fn(n):
x1_train = np.array([1, 2, 3, 4, 5, 6, 7, 8], dtype=np.int64)
x2_train = np.array([15, 25, 35, 45, 55, 65, 75, 85], dtype=np.int64)
labels = np.array([40, 30, 20, 10, 80, 70, 50, 60], dtype=np.int64)
dataset = tf.data.Dataset.from_tensor_slices(({"input_1": x1_train, "input_2": x2_train}, labels))
dataset = dataset.batch(2, drop_remainder=True)
dataset = dataset.repeat(n)
return dataset
input1 = keras.layers.Input(shape=(1,), name='input_1')
input2 = keras.layers.Input(shape=(1,), name='input_2')
model = keras.models.Model(inputs=[input_1, input_2], outputs=output)
基本上不是传递 python 列表,而是传递一个字典,其中键表示数组将被馈送到的层的名称。
就像上面的数组 x1_train
将被馈送到名称为 input_1 的张量 input1
。引用自
如果您有不同类型(float32、int 和 str)的数据框,则必须手动创建它。
遵循 Pratik 的语法:
tf.data.Dataset.from_tensor_slices(({"input_1": np.asarray(var_float).astype(np.float32), "imput_2": np.asarray(var_int).astype(np.int), ...}, labels))
我的问题是 tf.data.Dataset.from_tensor_slices(x_train, y_train) 中的 x_train 需要是一个列表。当我使用以下行将 [x1_train、x2_train] 传递给 tensorflow.data.Dataset.from_tensor_slices 时,出现错误(x1_train、x2_train 和 y_train是 numpy 数组):
Train=tensorflow.data.Dataset.from_tensor_slices(([x1_train,x2_train], y_train)).batch(batch_size)
错误:
Train=tensorflow.data.Dataset.from_tensor_slices(([x1_train,x2_train], y_train)).batch(batch_size)
return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Can't convert non-rectangular Python sequence to Tensor.
我该怎么办?
如果主要目标是将数据提供给具有多个输入层的模型,那么以下内容可能会有所帮助:
import tensorflow as tf
from tensorflow import keras
import numpy as np
def _input_fn(n):
x1_train = np.array([1, 2, 3, 4, 5, 6, 7, 8], dtype=np.int64)
x2_train = np.array([15, 25, 35, 45, 55, 65, 75, 85], dtype=np.int64)
labels = np.array([40, 30, 20, 10, 80, 70, 50, 60], dtype=np.int64)
dataset = tf.data.Dataset.from_tensor_slices(({"input_1": x1_train, "input_2": x2_train}, labels))
dataset = dataset.batch(2, drop_remainder=True)
dataset = dataset.repeat(n)
return dataset
input1 = keras.layers.Input(shape=(1,), name='input_1')
input2 = keras.layers.Input(shape=(1,), name='input_2')
model = keras.models.Model(inputs=[input_1, input_2], outputs=output)
基本上不是传递 python 列表,而是传递一个字典,其中键表示数组将被馈送到的层的名称。
就像上面的数组 x1_train
将被馈送到名称为 input_1 的张量 input1
。引用自
如果您有不同类型(float32、int 和 str)的数据框,则必须手动创建它。
遵循 Pratik 的语法:
tf.data.Dataset.from_tensor_slices(({"input_1": np.asarray(var_float).astype(np.float32), "imput_2": np.asarray(var_int).astype(np.int), ...}, labels))