简体   繁体   English

使用 Tensorflow 2.0 和没有 Keras 的急切执行

[英]Using Tensorflow 2.0 and eager execution without Keras

So this question might stem from a lack of knowledge about tensorflow.所以这个问题可能源于对 tensorflow 缺乏了解。 But I am trying to build a multilayer perceptron with tensorflow 2.0 , but without Keras .但我正在尝试使用tensorflow 2.0构建多层感知器,但没有Keras

The reason being that it is a requirement for my machine learning course that we do not use keras.原因是我的机器学习课程要求我们不使用 keras。 Why you might ask?为什么你可能会问? I am not sure.我不知道。

I already have implemented our model in tensorflow 2.0 with Keras ease, and now I want to do the exact same thing without keras .我已经在tensorflow 2.0中用 Keras 轻松实现了我们的 model,现在我想在没有keras的情况下做同样的事情。

model = Sequential()
model.add(Dense(64, activation='relu', input_dim=784))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(5, activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=Adam(),
              metrics=['accuracy'])

X_train = X[:7000]
y_train = tf.keras.utils.to_categorical(y[:7000], num_classes=5)
X_dev = X[7000:]
y_dev = tf.keras.utils.to_categorical(y[7000:], num_classes=5)

model.fit(X_train, y_train,
          epochs=100,
          batch_size=128)
score = model.evaluate(X_dev, y_dev, batch_size=128)
print(score)

Here is my problem.这是我的问题。 Whenever I look up the documentation on Tensorflow 2.0 , then even the guides on custom training are using Keras.每当我查看Tensorflow 2.0上的文档时,甚至自定义培训指南都在使用 Keras。

As placeholders and sessions are a thing of the past in tensorflow 2.0 , as I understand it, then I am a bit unsure of how to structure it.由于占位符和会话在tensorflow 2.0中已成为过去,据我了解,所以我有点不确定如何构建它。

I can make tensor objects.我可以制作张量对象。 I have the impression that I need to use eager execution and use gradient tape.我的印象是我需要使用 Eager Execution 并使用渐变胶带。 But I still am unsure of how to put these things together.但我仍然不确定如何将这些东西放在一起。

Now my question is.现在我的问题是。 Where should I look to get a better understanding?我应该从哪里获得更好的理解? Which direction has the greatest descent?哪个方向下降最大?

Please do tell me if I am doing this stack overflow post wrong.请告诉我我是否做错了这个堆栈溢出帖子。 It is my first time here.这是我第一次来这里。

As @Daniel Möller stated, there are these tutorials for custom training and custom layers on the official TensorFlow page.正如@Daniel Möller 所说,官方 TensorFlow 页面上有这些用于自定义训练自定义层的教程。 As stated on the custom training page:如自定义培训页面所述:

This tutorial used tf.Variable to build and train a simple linear model.本教程使用tf.Variable构建和训练一个简单的线性 model。

There is also this blog that creates custom layers and training without Keras API.还有这个博客在没有 Keras API 的情况下创建自定义层和训练。 You can check this code on Google Colab, which uses Cifar-10 with custom layers and training in the same manner.您可以在 Google Colab 上查看此代码,它使用带有自定义层的 Cifar-10,并以相同的方式进行训练。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM