Anders Stendevad
Anders Stendevad

Reputation: 111

Using Tensorflow 2.0 and eager execution without Keras

So this question might stem from a lack of knowledge about tensorflow. But I am trying to build a multilayer perceptron with tensorflow 2.0, but without Keras.

The reason being that it is a requirement for my machine learning course that we do not use 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.

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.

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.

I can make tensor objects. I have the impression that I need to use eager execution and use gradient tape. 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.

Upvotes: 11

Views: 6305

Answers (1)

rvimieiro
rvimieiro

Reputation: 1145

As @Daniel Möller stated, there are these tutorials for custom training and custom layers on the official TensorFlow page. As stated on the custom training page:

This tutorial used tf.Variable to build and train a simple linear model.

There is also this blog that creates custom layers and training without Keras API. You can check this code on Google Colab, which uses Cifar-10 with custom layers and training in the same manner.

Upvotes: 0

Related Questions