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