简体   繁体   English

如何为Tensorflow估算器输入正确的形状?

[英]How to input the correct shape for a Tensorflow Estimator?

I am trying to build a Tensorflow estimator to use on SageMaker . 我正在尝试构建一个Tensorflow估计器以在SageMaker上使用。 The main function trains and evaluates the estimator. 主要功能训练和评估估计器。 Despite my best attempts, I keep getting the following error: 尽管尽了最大的努力,我仍然遇到以下错误:

ValueError: Input 0 of layer inputs is incompatible with the layer: expected ndim=3, found ndim=2. ValueError:图层输入的输入0与图层不兼容:预期ndim = 3,找到的ndim = 2。 Full shape received: [50, 41] 收到的完整形状:[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)

My inputs and output shapes are: 我的输入和输出形状是:

(52388, 50, 41) (52388, 11) (52388,50,41)(52388,11)

from_tensors convert the input tensor to single big Tensor. from_tensors将输入张量转换为单个大张量。 For example, if you run the below example: 例如,如果运行以下示例:

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])

You notice, we only iterate dataset once while we expect to iterate it 52388 times! 您会注意到,我们仅迭代一次数据集,而我们希望将其迭代52388次!

Now assume we want to feed this single big tensor to our model. 现在假设我们要将这个大张量馈送到我们的模型中。 Tensorflow converts to [None, 1] which None is our batch size. Tensorflow转换为[None, 1]其中None是我们的批量大小。 On the other hand, you specify the input of the model with [None, 41] which means the model expect an input with shape [None, None, 41] . 另一方面,用[None, 41]指定模型的输入,这意味着模型期望输入的形状为[None, None, 41] Hence, this inconsistency causes the error. 因此,这种不一致导致错误。

How to fix it? 如何解决?

Use from_tensor_slices . 使用from_tensor_slices

Still giving me dimension error, how to fix it? 仍给我尺寸错误,如何解决? Define the input dimension for LSTM: 定义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'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM