Как решить проблему с размерностью в керасе и оценщиком - PullRequest
0 голосов
/ 25 мая 2019

Я пытаюсь преобразовать этот код с помощью оценщика , потому что я хочу узнать, как распределить обучение, и я понимаю, что использует оценщик с train_and_evaluate в Tensorflow 1.13

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