1

I am trying to replace the Keras Functional API with the Sequential API. I have added a minimalistic example that works without requiring any data imports.

Here is the code with the Functional API which works -

#Taken from - https://tomroth.com.au/keras/

import numpy as np

n_row = 1000
x1 = np.random.randn(n_row)
x2 = np.random.randn(n_row)
x3 = np.random.randn(n_row)
y_classifier = np.array([1 if (x1[i] + x2[i] + (x3[i])/3 + np.random.randn(1) > 1) else 0 for i in range(n_row)])
y_cts = x1 + x2 + x3/3 + np.random.randn(n_row)
dat = np.array([x1, x2, x3]).transpose()

# Generate indexes of test and train
idx_list = np.linspace(0,999,num=1000)
idx_test = np.random.choice(n_row, size = 200, replace=False)
idx_train = np.delete(idx_list, idx_test).astype('int')

# Split data into test and train
dat_train = dat[idx_train,:]
dat_test = dat[idx_test,:]
y_classifier_train = y_classifier[idx_train]
y_classifier_test = y_classifier[idx_test]
y_cts_train = y_cts[idx_train]
y_cts_test = y_cts[idx_test]

# setup
from keras.models import Input, Model
from keras.layers import Dense
from keras.models import Sequential


# # Build the model with Sequential API
# inputs = Input(shape=(3,))
# model = Sequential()
# model.add(inputs)
# model.add(Dense(2, activation="relu"))
# logistic_model = Model(inputs, model)

# Build the model with Functional API
inputs = Input(shape=(3,))
output = Dense(1, activation='sigmoid')(inputs)


logistic_model = Model(inputs, output)

# Compile the model
logistic_model.compile(optimizer='sgd',
                       loss = 'binary_crossentropy',
                       metrics=['accuracy'])

# Fit on training data
logistic_model.optimizer.lr = 0.001
logistic_model.fit(x=dat_train, y=y_classifier_train, epochs = 5,
                   validation_data = (dat_test, y_classifier_test))

logistic_model.fit(x=dat_train, y=y_classifier_train, epochs = 500, verbose=0,
                   validation_data = (dat_test, y_classifier_test))
logistic_model.fit(x=dat_train, y=y_classifier_train, epochs = 1, verbose=1,
                   validation_data = (dat_test, y_classifier_test))

However, when I try it with the Sequential API,

import numpy as np

n_row = 1000
x1 = np.random.randn(n_row)
x2 = np.random.randn(n_row)
x3 = np.random.randn(n_row)
y_classifier = np.array([1 if (x1[i] + x2[i] + (x3[i])/3 + np.random.randn(1) > 1) else 0 for i in range(n_row)])
y_cts = x1 + x2 + x3/3 + np.random.randn(n_row)
dat = np.array([x1, x2, x3]).transpose()

# Generate indexes of test and train
idx_list = np.linspace(0,999,num=1000)
idx_test = np.random.choice(n_row, size = 200, replace=False)
idx_train = np.delete(idx_list, idx_test).astype('int')

# Split data into test and train
dat_train = dat[idx_train,:]
dat_test = dat[idx_test,:]
y_classifier_train = y_classifier[idx_train]
y_classifier_test = y_classifier[idx_test]
y_cts_train = y_cts[idx_train]
y_cts_test = y_cts[idx_test]

# setup
from keras.models import Input, Model
from keras.layers import Dense
from keras.models import Sequential


# Build the model with Sequential API
inputs = Input(shape=(3,))
model = Sequential()
model.add(inputs)
model.add(Dense(2, activation="relu"))
logistic_model = Model(inputs, model)

# # Build the model with Functional API
# inputs = Input(shape=(3,))
# output = Dense(1, activation='sigmoid')(inputs)


logistic_model = Model(inputs, output)

# Compile the model
logistic_model.compile(optimizer='sgd',
                       loss = 'binary_crossentropy',
                       metrics=['accuracy'])

# Fit on training data
logistic_model.optimizer.lr = 0.001
logistic_model.fit(x=dat_train, y=y_classifier_train, epochs = 5,
                   validation_data = (dat_test, y_classifier_test))

logistic_model.fit(x=dat_train, y=y_classifier_train, epochs = 500, verbose=0,
                   validation_data = (dat_test, y_classifier_test))
logistic_model.fit(x=dat_train, y=y_classifier_train, epochs = 1, verbose=1,
                   validation_data = (dat_test, y_classifier_test))

I get the following error -

2022-02-03 14:42:56.883439: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
  File "C:/Users/thoma/AppData/Roaming/JetBrains/PyCharmCE2020.3/scratches/scratch_19.py", line 35, in <module>
    logistic_model = Model(inputs, model)
  File "C:\Users\thoma\anaconda3\envs\bug_fix\lib\site-packages\tensorflow\python\keras\engine\training.py", line 242, in __new__
    return functional.Functional(*args, **kwargs)
  File "C:\Users\thoma\anaconda3\envs\bug_fix\lib\site-packages\tensorflow\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\thoma\anaconda3\envs\bug_fix\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 115, in __init__
    self._init_graph_network(inputs, outputs)
  File "C:\Users\thoma\anaconda3\envs\bug_fix\lib\site-packages\tensorflow\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\thoma\anaconda3\envs\bug_fix\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 142, in _init_graph_network
    base_layer_utils.create_keras_history(self._nested_outputs)
  File "C:\Users\thoma\anaconda3\envs\bug_fix\lib\site-packages\tensorflow\python\keras\engine\base_layer_utils.py", line 191, in create_keras_history
    _, created_layers = _create_keras_history_helper(tensors, set(), [])
  File "C:\Users\thoma\anaconda3\envs\bug_fix\lib\site-packages\tensorflow\python\keras\engine\base_layer_utils.py", line 226, in _create_keras_history_helper
    op = tensor.op  # The Op that created this Tensor.
AttributeError: 'Sequential' object has no attribute 'op'

Process finished with exit code 1

1 Answer 1

1

The equivalent of this model using the Functional API:

import tensorflow as tf

inputs = tf.keras.layers.Input(shape=(3,))
output = tf.keras.layers.Dense(1, activation='sigmoid')(inputs)
model1 = tf.keras.Model(inputs, output)
print(model1.summary())
Model: "model_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_3 (InputLayer)        [(None, 3)]               0         
                                                                 
 dense_5 (Dense)             (None, 1)                 4         
                                                                 
=================================================================
Total params: 4
Trainable params: 4
Non-trainable params: 0
_________________________________________________________________
None

Is this using the Sequential API:

model2 = tf.keras.Sequential()
model2.add(tf.keras.layers.Dense(1, activation="sigmoid", input_dim=3))
print(model2.summary())
Model: "sequential_5"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_6 (Dense)             (None, 1)                 4         
                                                                 
=================================================================
Total params: 4
Trainable params: 4
Non-trainable params: 0
_________________________________________________________________
None
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.