首页 > 解决方案 > 我们可以在不使用 keras 的情况下在 tensorflow2.0 中训练模型吗?

问题描述

我正在尝试编写一个简单的 ML 代码来对 tensorflow2.0 中的 mnist 数据集进行分类。我暂时没有使用 Keras,因为我只想使用较低的 API 来帮助我了解 tensorflow 的工作原理。但是,在我定义了交叉熵之后,似乎无法继续了。所有的 tf2.0 优化器都移到了 keras,我不知道如何在 tf2.0 中训练没有 keras 的模型。有没有办法在 tf2.0 中绕过 keras?

from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf
from tensorflow.keras import datasets, layers, models

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt


(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()

print(train_images.shape)
print(len(train_labels))
print(train_images[1,:,:].shape)

# plt.figure()
# plt.imshow(train_images[0])
# plt.colorbar()
# plt.grid(False)
# plt.show()

# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

for i in range(1):
    x = tf.constant(train_images[1,:,:].reshape(784), dtype = tf.float32) 
    x = tf.reshape(x, [1, 784])
    print(tf.shape(x), tf.shape(W))
    # define the model
    y = tf.nn.softmax(tf.matmul(x, W) + b)
    print(y)
    # correct labels
    y_ = np.zeros(10)
    y_[train_labels[i]] = 1.0
    y_ = tf.constant(y_, dtype = tf.float32)
    y_ = tf.reshape(y_, [1, 10])
    cross_entropy = -tf.reduce_sum(y_* tf.math.log(y))
    print(cross_entropy)
        I don't know how to continue from here.


标签: kerastensorflow2.0

解决方案


在 TensorFlow 2.x 中完全可以在不使用 keras API 的情况下对模型进行基于反向传播的训练。使用将围绕命名空间tf.GradientTape下的 API 和优化器对象tf.optimizers

您的示例可以修改如下。请注意,这是一个简单的代码,旨在以简短的代码片段说明基本用法。这并不是为了说明 TF2 中的机器学习最佳实践。

(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()

train_images, test_images = train_images / 255.0, test_images / 255.0

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

@tf.function
def my_model(x):
  # This is a hand-rolled logistic regressor.
  y = tf.matmul(x, W) + b
  return tf.nn.softmax(y)

@tf.function
def loss(x, y):
  # This is a hand-rolled categorical cross-entropy loss.
  diff = -(labels * tf.math.log(logits))
  loss = tf.reduce_mean(diff)
  return loss

optimizer = tf.optimizers.Adam(learning_rate=1e-3)

for i in xrange(num_steps):

  # A single training step.
  with tf.GradientTape() as tape:
    # This is atypical, in that you would normally want to do this in
    # mini-batches, instead of using all examples in x_train and y_train
    # at once. But again, this is just a simple example.
    loss_value = loss(x_train, y_train)
  gradients = tape.gradient(loss_value, [W, b])
  optimizer.apply_gradients(zip(gradients, [w, b]))

推荐阅读