Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将数据对象转换为具有多个输入的Keras模型_Python_Tensorflow_Tensorflow Datasets_Tf.keras - Fatal编程技术网

Python 将数据对象转换为具有多个输入的Keras模型

Python 将数据对象转换为具有多个输入的Keras模型,python,tensorflow,tensorflow-datasets,tf.keras,Python,Tensorflow,Tensorflow Datasets,Tf.keras,我在使用tf.Data和多输入的Keras时遇到问题 我正在使用Python生成器从PostgreSQL表读取数据,该生成器返回三个数组: class PairGenerator(object): return {'numerical_inputs': features, 'cat_input': idx}, response 使用。从\u generator,我正在创建一个数据集对象: training_generator = PairGenerator(sql_query = s

我在使用tf.Data和多输入的Keras时遇到问题

我正在使用Python生成器从PostgreSQL表读取数据,该生成器返回三个数组:

class PairGenerator(object):

    return {'numerical_inputs': features, 'cat_input': idx}, response
使用
。从\u generator
,我正在创建一个数据集对象:

training_generator = PairGenerator(sql_query = sql_query, config_file = 'config.json', column_dtypes = ColsDtypes, n_steps = n_steps, num_obs = 1000, batch_size = batch_size)

train_dataset = tf.data.Dataset.from_generator(lambda: training_generator, output_types=({'numerical_inputs': tf.float32, 'cat_input': tf.string}, tf.int32), output_shapes=({'numerical_inputs': tf.TensorShape([None, 10, 36]), 'cat_input': tf.TensorShape([None,10])}, tf.TensorShape([None,10, 1]))).prefetch(1)
#<DatasetV1Adapter shapes: ({numerical_inputs: (None, 10, 36), cat_input: (None, 10)}, (None, 10, 1)), types: ({numerical_inputs: tf.float32, cat_input: tf.string}, tf.int32)>
所以我用Tensorflow 2.0的Keras定义了一个模型

batch_size = 32
num_obs = 1000
num_cats = 1 # number of categorical features
n_steps = 10 # number of timesteps in each sample
n_numerical_feats = 36 # number of numerical features in each sample
cat_size = 32465 # number of unique categories in each categorical feature
embedding_size = 1 # embedding dimension for each categorical feature

numerical_inputs = keras.layers.Input(shape=(n_steps, n_numerical_feats), name='numerical_inputs')
#<tf.Tensor 'numerical_inputs:0' shape=(?, 10, 36) dtype=float32>

cat_input = keras.layers.Input(shape=(n_steps,), name='cat_input')
#<tf.Tensor 'cat_input:0' shape=(None, 10) dtype=float32>

cat_embedded = keras.layers.Embedding(cat_size, embedding_size, embeddings_initializer='uniform')(cat_input)
#<tf.Tensor 'embedding_1/Identity:0' shape=(None, 10, 1) dtype=float32>

merged = keras.layers.concatenate([numerical_inputs, cat_embedded])
#<tf.Tensor 'concatenate_1/Identity:0' shape=(None, 10, 37) dtype=float32>

lstm_out = keras.layers.LSTM(64, return_sequences=True)(merged)
#<tf.Tensor 'lstm_2/Identity:0' shape=(None, 10, 64) dtype=float32>

Dense_layer1 = keras.layers.Dense(32, activation='relu', use_bias=True)(lstm_out)
#<tf.Tensor 'dense_4/Identity:0' shape=(None, 10, 32) dtype=float32>
Dense_layer2 = keras.layers.Dense(1, activation='linear', use_bias=True)(Dense_layer1 )
#<tf.Tensor 'dense_5/Identity:0' shape=(None, 10, 1) dtype=float32>

model = keras.models.Model(inputs=[numerical_inputs, cat_input], outputs=Dense_layer2)
现在是适合模型的时候了。从Tensorflow 1.9开始,可以将
tf.data.Dataset
对象直接传递到
keras.Model.fit()

然而,在这一步中,什么也没有发生。Jupyter内核已打开,它似乎正在运行,但未显示任何结果

如果我不使用
tf.data.Dataset
对象并直接从numpy数组馈送数据,它的工作方式就像魅力一样

#fit the model
#you can use input layer names instead
history = model.fit({'numerical_inputs': X_numeric, 
       'cat_input': X_cat1.reshape(-1, n_steps)}, 
                y = target,
                batch_size = batch_size
                epochs=EPOCHS,  
                verbose=1,
                initial_epoch=0)
github上存在此问题,没有解决方案!


我真的不知道还能做什么。有人能帮我吗?

你解决过这个问题吗?我遇到了同样的问题:(我想遇到同样的问题,请告诉我们您是否已经解决了它?
#compile model
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(loss='mse',
          optimizer=optimizer,
          metrics=['mae', 'mse'])
EPOCHS =5
#fit the model
history = model.fit(train_dataset,
                epochs=EPOCHS,
                verbose=1,
                initial_epoch=0)
#fit the model
#you can use input layer names instead
history = model.fit({'numerical_inputs': X_numeric, 
       'cat_input': X_cat1.reshape(-1, n_steps)}, 
                y = target,
                batch_size = batch_size
                epochs=EPOCHS,  
                verbose=1,
                initial_epoch=0)