我已经在keras功能API上开发了以下网络,该网络接收形状为[35,15,1]的多个输入序列,并预测形状为[35,1]的单个输出:
# Model
def create_keras_model(window_size, n_inputs):
region_input = tf.keras.Input(shape = (window_size), name = 'region')
freight_rev_input = tf.keras.Input(shape = (window_size), name = 'freight_rev')
meas_freight_expense_input = tf.keras.Input(shape = (window_size), name = 'meas_freight_expense')
prod_recipe_input = tf.keras.Input(shape = (window_size), name = 'prod_recipe')
item_qty_input = tf.keras.Input(shape = (window_size), name = 'item_qty')
calc_qty_input = tf.keras.Input(shape = (window_size), name = 'calc_qty')
dlvy_qty_input = tf.keras.Input(shape = (window_size), name = 'dlvy_qty')
#conv_input = tf.keras.Input(shape = (), name = 'conv')
# Reshape features for LSTM
region_feature = tf.keras.layers.Reshape([window_size, 1])(region_input)
freight_rev_feature = tf.keras.layers.Reshape([window_size, 1])(freight_rev_input)
meas_freight_expense_feature = tf.keras.layers.Reshape([window_size, 1])(meas_freight_expense_input)
prod_recipe_feature = tf.keras.layers.Reshape([window_size, 1])(prod_recipe_input)
item_qty_feature = tf.keras.layers.Reshape([window_size, 1])(item_qty_input)
calc_qty_feature = tf.keras.layers.Reshape([window_size, 1])(calc_qty_input)
dlvy_qty_feature = tf.keras.layers.Reshape([window_size, 1])(dlvy_qty_input)
#conv_feature = tf.keras.layers.Reshape([window_size, 1])(conv_input)
# Concatenate features
x = tf.keras.layers.concatenate([region_feature,freight_rev_feature,meas_freight_expense_feature,
prod_recipe_feature,item_qty_feature,calc_qty_feature,dlvy_qty_feature,
#conv_feature
])
# Apply 1st LSTM
x = tf.keras.layers.LSTM(n_inputs, return_sequences = True)(x)
# Apply 2nd LSTM
x = tf.keras.layers.LSTM(n_inputs, return_sequences = True)(x)
# Apply 3rd LSTM
x = tf.keras.layers.LSTM(n_inputs)(x)
# Calculate Dense Output
y = tf.keras.layers.Dense(1, activation = tf.nn.relu, name='prediction')(x)
# Create Model
model = tf.keras.Model(inputs = [region_input,freight_rev_input,meas_freight_expense_input,
prod_recipe_input,item_qty_input,calc_qty_input,dlvy_qty_input,
# conv_input
],
outputs = [y])
model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
,loss = tf.keras.losses.MSE
,metrics=[tf.keras.losses.MSE])
return model
在model.fit(features,target)上运行它,没关系;当我将其转换为tf.estimator时,在节点'loss / prediction_loss / SquaredDifference'上出现不兼容的形状问题:
#Build Keras model
model = create_keras_model(lookback, num_features)
#Convert Keras model to an Estimator
estimator = tf.keras.estimator.model_to_estimator(
keras_model = model,
model_dir = output_dir,
config = tf.estimator.RunConfig(save_checkpoints_secs = min_eval_frequency))
当调用train_and_evaluate时,出现此错误:
InvalidArgumentError: Incompatible shapes: [35,1] vs. [0,1]
[[node loss/prediction_loss/SquaredDifference (defined at <ipython-input-67-b45d1de25e2a>:47) ]]
Errors may have originated from an input operation.
Input Source operations connected to node loss/prediction_loss/SquaredDifference:
ExpandDims (defined at <ipython-input-8-ea21aacd12c5>:18)
我相信它与在净输出上定义的固定张量形状有关,但是我找不到解决它的方法。有人可以帮我吗?