如何在Keras和估计器中解决尺寸问题

时间:2019-05-25 04:43:58

标签: keras tensorflow-estimator

我正尝试使用estimator转换此代码,因为我想学习如何分发训练,并且我了解在Tensorflow 1.13中将estimator与train_and_evaluate一起使用

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(512, activation=tf.nn.relu),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)

请注意,对于此代码,我创建了input_fn以吸收数据,我相信这是问题所在,但我不知道如何解决。

from __future__ import absolute_import, division, print_function
import tensorflow as tf
import numpy as np

# Recoleccion de la data
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()

# Normalizacion de datos
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train.shape, x_test.shape

# Data ingest
def read_dataset(x, y, mode, batch_size=20):
  x_ds = tf.data.Dataset.from_tensor_slices(x)
  y_ds = tf.data.Dataset.from_tensor_slices(tf.cast(y, tf.int64))

  ds = tf.data.Dataset.zip((x_ds, y_ds))

  if mode == tf.estimator.ModeKeys.TRAIN:
    num_epochs = None
    ds = ds.apply(tf.data.experimental.shuffle_and_repeat(buffer_size=batch_size*10))
  else:
    num_epochs = 1

  ds = ds.batch(batch_size).repeat(num_epochs).prefetch(tf.data.experimental.AUTOTUNE)
  return ds

def train_input_fn():
  return read_dataset(x = x_train,#<-------
                      y = y_train,#<-------
                      mode = tf.estimator.ModeKeys.TRAIN)

def eval_input_fn():
  return read_dataset(x = x_test,#<-------
                      y = y_test,#<-------
                      mode = tf.estimator.ModeKeys.EVAL)

# Modelo: Keras creacion
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(512, activation=tf.nn.relu),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer='adam',
             loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])
model.summary()

estimator = tf.keras.estimator.model_to_estimator(keras_model=model)

# Produccion: data
train_spec = tf.estimator.TrainSpec(input_fn = train_input_fn,
                                   max_steps = 20)
eval_spec = tf.estimator.EvalSpec(input_fn = eval_input_fn)

# Entrenamiento
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

但是我有这个错误,不知道为什么

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1660   except errors.InvalidArgumentError as e:
   1661     # Convert to ValueError for backwards compatibility.
-> 1662     raise ValueError(str(e))
   1663 
   1664   return c_op

ValueError: Can not squeeze dim[1], expected a dimension of 1, got 10 for 'metrics/acc/remove_squeezable_dimensions/Squeeze' (op: 'Squeeze') with input shapes: [?,10].

0 个答案:

没有答案