我正在尝试构建一个Tensorflow估算器以用于SageMaker
。主要功能训练和评估估计器。尽管尽了最大的努力,但我仍然遇到以下错误:
ValueError:图层输入的输入0与图层不兼容:预期ndim = 3,找到的ndim = 2。收到的完整图形:[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)
答案 0 :(得分:0)
from_tensors
将输入张量转换为单个大张量。例如,如果运行以下示例:
import tensorflow as tf
tf.enable_eager_execution()
dataset2 = tf.data.Dataset.from_tensors(
(tf.random_uniform([52388, 50, 41], maxval=10, dtype=tf.int32),
tf.random_uniform([52388, 11], maxval=10, dtype=tf.int32)))
for i, item in enumerate(dataset2):
print('element: ' + str(i), item[0], item[1])
您会注意到,我们仅迭代一次数据集,而我们希望将其迭代52388次!
现在假设我们要将此单个大张量馈入模型。 Tensorflow转换为[None, 1]
是我们的批量大小的None
。另一方面,用[None, 41]
指定模型的输入,这意味着模型期望输入形状为[None, None, 41]
的输入。因此,这种不一致会导致错误。
如何解决?
仍然给我尺寸错误,如何解决?定义LSTM的输入尺寸:
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.LSTM(32, name='inputs', input_shape=(50, 41)))
model.add(tf.keras.layers.Dense(11, activation='softmax', name='dense'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])