我们可以在不使用keras的情况下在tensorflow2.0中训练模型吗?

时间:2019-09-09 21:42:30

标签: 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.


1 个答案:

答案 0 :(得分:0)

在TensorFlow 2.x中完全可以使用基于反向传播的模型训练,而无需使用keras API。用法将以tf.GradientTape名称空间下的tf.optimizers API和优化器对象为中心。

您的示例可以进行如下修改。请注意,这是一个简单的代码,旨在说明简短代码段中的基本用法。这并不是为了说明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]))