我正在尝试构建一个Tensorflow估计器来使用SageMaker
。主要功能是训练和评估估计器。尽管我尽了最大的努力,我还是犯了以下错误:
ValueError: Input 0 of layer inputs is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [50, 41]
def keras_model_fn(hyperparameters):
"""keras_model_fn receives hyperparameters from the training job and returns a compiled keras model.
The model will be transformed into a TensorFlow Estimator before training and it will be saved in a
TensorFlow Serving SavedModel at the end of training.
Args:
hyperparameters: The hyperparameters passed to the SageMaker TrainingJob that runs your TensorFlow
training script.
Returns: A compiled Keras model
"""
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.LSTM(32, name='inputs', input_shape=( None, 41)))
model.add(tf.keras.layers.Dense(11, activation='softmax', name='dense'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
return model
def train_input_fn(training_dir=None, hyperparameters=None):
# invokes _input_fn with training dataset
dataset = tf.data.Dataset.from_tensors(({INPUT_TENSOR_NAME: x_train}, y_train))
dataset = dataset.repeat()
return dataset.make_one_shot_iterator().get_next()
def eval_input_fn(training_dir=None, hyperparameters=None):
# invokes _input_fn with evaluation dataset
dataset = tf.data.Dataset.from_tensors(({INPUT_TENSOR_NAME: x_test}, y_test))
return dataset.make_one_shot_iterator().get_next()
if __name__ == '__main__':
print(x_train.shape, y_train.shape)
tf.logging.set_verbosity(tf.logging.INFO)
model = keras_model_fn(0)
estimator = tf.keras.estimator.model_to_estimator(keras_model=model)
train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, max_steps=1000)
eval_spec = tf.estimator.EvalSpec(input_fn=eval_input_fn)
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
我的输入和输出形状是:
(52388, 50, 41) (52388, 11)
^{} 将输入张量转换为单个大张量。例如,如果运行以下示例:
您注意到,我们只迭代一次数据集,而我们期望它迭代52388次!在
现在假设我们要把这个大张量输入到我们的模型中。Tensorflow转换为
[None, 1]
,这是我们的批处理大小。另一方面,使用[None, 41]
指定模型的输入,这意味着模型需要一个形状为[None, None, 41]
的输入。因此,这种不一致性导致了错误。在如何修复它?
使用^{} 。在
仍然给我尺寸误差,如何修复?定义LSTM的输入维度:
^{pr2}$相关问题 更多 >
编程相关推荐