Tensorflow急切执行无法正常工作

时间:2018-06-28 22:05:38

标签: python tensorflow

我一直在尝试使用MNIST数据集复制Eager Execution tutorial,但是它没有用...我没有收到任何错误,但是由于代码只是在第一轮中停止,输出是
Epoch 000: Loss: nan, Accuracy: 9.898%
我已经测试了其他MNIST代码,它们的执行速度更快...(我等待了大约30分钟)

import numpy as np
import tensorflow as tf

tf.enable_eager_execution()


#Load dataset
mnist = tf.contrib.learn.datasets.load_dataset('mnist')
train_data = mnist.train.images 
train_labels = np.asarray(mnist.train.labels, dtype = np.int32)
test_data = mnist.test.images 
test_labels = np.asarray(mnist.test.labels, dtype = np.int32)


train_dataset = tf.data.Dataset.from_tensor_slices((train_data, train_labels))
train_dataset = train_dataset.shuffle(buffer_size=100000)
train_dataset = train_dataset.batch(10)

features, label = iter(train_dataset).next()
print("example features:", features[0])
print("example label:", label[0])

model = tf.keras.Sequential([
  tf.keras.layers.Dense(10, activation="relu", input_shape=(784,)),  # input shape required
  tf.keras.layers.Dense(10, activation="relu"),
  tf.keras.layers.Dense(3)
])


def loss(model, x, y):
    y_ = model(x)
    return tf.losses.sparse_softmax_cross_entropy(labels=y, logits=y_)

def grad(model, inputs, targets):
    with tf.GradientTape() as tape:
        loss_value = loss(model, inputs, targets)
    return tape.gradient(loss_value, model.variables)

optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.01)


train_loss_results = []
train_accuracy_results = []

num_epochs = 200

for epoch in range(num_epochs):
    epoch_loss_avg = tf.contrib.eager.metrics.Mean()
    epoch_accuracy = tf.contrib.eager.metrics.Accuracy()

    for x,y in train_dataset:
        grads = grad(model, x, y)
        optimizer.apply_gradients(zip(grads, model.variables),
                                  global_step=tf.train.get_or_create_global_step())

        epoch_loss_avg(loss(model, x, y))

        epoch_accuracy(tf.argmax(model(x), axis=1, output_type=tf.int32), y)

    train_loss_results.append(epoch_loss_avg.result())
    train_accuracy_results.append(epoch_accuracy.result())

    if epoch % 50 == 0:
        print("Epoch {:03d}: Loss: {:.3f}, Accuracy: {:.3%}".format(epoch,
                                                                epoch_loss_avg.result(),
                                                                epoch_accuracy.result()))

PS。我知道该模型可能不会高效,只是一个测试运行

0 个答案:

没有答案