I'm trying to build a simple linear model using TensorFlow functional API.
def create_model():
input1 = tf.keras.Input(shape=(30,))
hidden1 = tf.keras.layers.Dense(units = 12, activation='relu')(input1)
hidden2 = tf.keras.layers.Dense(units = 6, activation='relu')(hidden1)
output1 = tf.keras.layers.Dense(units = 2, activation='softmax')(hidden2)
model = tf.keras.models.Model(inputs = input1, outputs = output1)
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
This is my code to create the model.
I'm using a data pipeline to create the input dataset like this.
def make_dataset(dataframe, shuffle=True, batch_size=32):
labels = dataframe.pop('target')
ds = tf.data.Dataset.from_tensor_slices((dict(dataframe), labels))
if shuffle:
ds = ds.shuffle(buffer_size=100000, seed = 121 ).repeat()
return ds
pos_ds = make_dataset(train_data_pos)
neg_ds = make_dataset(train_data_neg)
train_ds = tf.data.experimental.sample_from_datasets([pos_ds, neg_ds], weights=[0.5, 0.5], seed = 45)
train_ds = train_ds.batch(BATCH_SIZE)
steps_per_epoch = np.ceil(2.0*count_neg/BATCH_SIZE)
Here the train_data_pos and train_data_neg are data frame containing positive and negative classes
history = model.fit(train_ds,
validation_data=val_ds,
epochs=100,
verbose = 1,
steps_per_epoch=steps_per_epoch)
This is my model.fit() cmd.
My error log is as follows:
Traceback (most recent call last):
File "6.py", line 159, in <module>
steps_per_epoch=steps_per_epoch)
File "C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\keras\engine\training.py", line 66, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\keras\engine\training.py", line 848, in fit
tmp_logs = train_function(iterator)
File "C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\eager\def_function.py", line 580, in __call__
result = self._call(*args, **kwds)
File "C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\eager\def_function.py", line 627, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\eager\def_function.py", line 506, in _initialize
*args, **kwds))
File "C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\eager\function.py", line 2446, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\eager\function.py", line 2777, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\eager\function.py", line 2667, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\framework\func_graph.py", line 981, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\eager\def_function.py", line 441, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\framework\func_graph.py", line 968, in wrapper
raise e.ag_error_metadata.to_exception(e)
tensorflow.python.autograph.pyct.error_utils.KeyError: in user code:
C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\keras\engine\training.py:571 train_function *
outputs = self.distribute_strategy.run(
C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\keras\engine\training.py:531 train_step **
y_pred = self(x, training=True)
C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:927 __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\keras\engine\network.py:719 call
convert_kwargs_to_constants=base_layer_utils.call_context().saving)
C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\keras\engine\network.py:826 _run_internal_graph
inputs = self._flatten_to_reference_inputs(inputs)
C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\keras\engine\network.py:926 _flatten_to_reference_inputs
return [tensors[inp._keras_history.layer.name] for inp in ref_inputs]
C:\Users\Aniket\Documents\Aniket\learning-ML\ML_env\lib\site-packages\tensorflow\python\keras\engine\network.py:926 <listcomp>
return [tensors[inp._keras_history.layer.name] for inp in ref_inputs]
KeyError: 'input_1'
All of this works when I use sequential API to construct the model.
def create_model():
model = tf.keras.Sequential([
feature_layer,
tf.keras.layers.Dense(units = 12, activation='relu', use_bias = True, kernel_initializer= 'glorot_uniform', bias_initializer = 'glorot_uniform', name = 'd1'),
tf.keras.layers.Dense(units = 6, activation='relu', use_bias = True, kernel_initializer= 'glorot_uniform', bias_initializer = 'glorot_uniform', name = 'd2'),
tf.keras.layers.Dense(units = 2, activation='softmax', name = 'out')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
Here feature_layer is tf.keras.layers.DenseFeatures
Here is the link to the entire code - LINK
train_data_poslook like?