I have the following code written using tflearn:

import tflearn
from tflearn.layers.core import fully_connected, input_data
from tflearn.layers.estimator import regression

def model():
    network = input_data(shape=[None, 5, 1], name='input')
    print('network:', network)
    # Outputs: network: Tensor("input/X:0", shape=(?, 5, 1), dtype=float32)
    network = fully_connected(network, 25, activation='relu')
    print('fully_network_1:', network)
    # Outputs: fully_network_1: Tensor("FullyConnected/Relu:0", shape=(?, 25), dtype=float32)
    network = fully_connected(network, 1, activation='linear')
    print('fully_network_2:', network)
    # Outputps: fully_network_2: Tensor("FullyConnected_1/BiasAdd:0", shape=(?, 1), dtype=float32)
    network = regression(network, optimizer='adam', learning_rate=self.lr,
                         loss='mean_square', name='target')
    print('reg:', network)
    # Outputs: reg: Tensor("FullyConnected_1/BiasAdd:0", shape=(?, 1), dtype=float32)
    model = tflearn.DNN(network, tensorboard_dir='log', tensorboard_verbose=3)
    return model

The created model is trained using the following code:

def train_model(train_x, train_y, model):
    train_x = train_x.reshape(-1, 5, 1)
    train_y = train_y.reshape(-1, 1)
    # Outputs: (599068, 5, 1)
    # Outputs: (599068, 1)
    model.fit(train_x, train_y, n_epoch=3, shuffle=True, run_id='model.tflearn')

When I display the graph using Tensorboard, I get the following graph:

enter image description here

I wanted to migrate to Tensorflow 2.2.0 (from Tensorflow 1.15.0), so I decided to rewrite the previous code using Tensorflow2, here is my try:

import tensorflow as tf

def build_model():
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Dense(units=25, activation='relu',
                                    input_shape=(5, 1)))
    model.add(tf.keras.layers.Dense(units=1, activation='linear'))
    opt = tf.keras.optimizers.Adam(learning_rate=0.01, beta_1=0.9,
                                   beta_2=0.999, epsilon=1e-07,
                                   amsgrad=False, name='Adam')
    model.compile(optimizer=opt, loss='mse')
    return model

The model summary prints:

Model: "sequential"
Layer (type)                 Output Shape              Param #   
dense (Dense)                (None, 5, 25)             50        
dense_1 (Dense)              (None, 5, 1)              26        
Total params: 76
Trainable params: 76
Non-trainable params: 0

The created model is trained using the following code:

import datetime
import tensorflow as tf

def train(model, train_x, train_y):
    train_x = train_x.reshape(-1, 5, 1)
    train_y = train_y.reshape(-1, 1)
    # train_x.shape = (599068, 5, 1)
    # train_y.shape = (599068, 1)
    log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
    tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

    model.fit(train_x, train_y, epochs=3, verbose=1, callbacks=[tensorboard_callback])

When I display the model's graph I got this:

enter image description here

This is totally different from the graph I got with tflearn.
Did I miss something when building the model with Tensorflow?

PS: I got different losses when I train both models, the first model's loss is 0.06, while the model built with Tensorflow2 has 0.25 loss, which is far from the first one!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.