我想使用tensorflow数据集迭代器方法来馈送模型。但是,我不确定该如何进行。任何建议将不胜感激。谢谢。
batch_size=10
tf_X_train=tf.placeholder(tf.float32, shape=[None, 410,1,10])
tf_Y_train=tf.placeholder(tf.float32, shape=[None])
train_dataset = tf.data.Dataset.from_tensor_slices((tf_X_train, tf_Y_train))
train_dataset = train_dataset.batch(batch_size)
iterator = tf.data.Iterator.from_structure(train_dataset.output_types, train_dataset.output_shapes)
data_X, data_y = iterator.get_next()
train_iterator = iterator.make_initializer(train_dataset)
with tf.Session() as sess:
tf.global_variables_initializer()
learning_rate=0.0001
EPOCHS = 200
optimizer = tf.train.AdamOptimizer(learning_rate, 0.99)
model = cnn_model_fn(learning_rate)
model.compile(loss='mean_squared_error',
optimizer=optimizer,
metrics=['mean_absolute_error', 'mean_squared_error'])
model.fit_generator(train_iterator,epochs=EPOCHS,steps_per_epoch=32,callbacks=[PrintDot()])
答案 0 :(得分:0)
tensorflow 1.4+的答案,您不必使用迭代器:
model.fit(data_X, data_y,epochs=EPOCHS,steps_per_epoch=32,callbacks=[PrintDot()])
答案 1 :(得分:0)
不确定您使用的是哪个版本,如果tf2.3且模型为tf.keras.model,则只需执行
batch_size=10
tf_X_train=tf.placeholder(tf.float32, shape=[None, 410,1,10])
tf_Y_train=tf.placeholder(tf.float32, shape=[None])
train_dataset = tf.data.Dataset.from_tensor_slices((tf_X_train, tf_Y_train))
train_dataset = train_dataset.batch(batch_size)
learning_rate=0.0001
EPOCHS = 200
optimizer = tf.train.AdamOptimizer(learning_rate, 0.99)
model = cnn_model_fn(learning_rate)
model.compile(loss='mean_squared_error',
optimizer=optimizer,
metrics=['mean_absolute_error', 'mean_squared_error'])
model.fit(train_dataset, epochs=EPOCHS,steps_per_epoch=32,callbacks=[PrintDot()]
您已将数据包装为tf.dataset格式,model.fit可以将数据集作为输入 https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit