Take a look at this question.
I am trying to convert this functional model into a sequential model.
Here is the full source code in Repl.it.
The following is the main-section of the source code:
# <editor-fold desc="def create_model()">
def create_model(n_hidden_1, n_hidden_2, num_classes, num_features):
model = Sequential()
model.add(tf.keras.layers.InputLayer(input_shape=(num_features,)))
model.add(tf.keras.layers.Dense(n_hidden_1, activation='sigmoid'))
model.add(tf.keras.layers.Dense(n_hidden_2, activation='sigmoid'))
model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))
return model
# </editor-fold>
if __name__ == "__main__":
len_int = len(sys.argv)
arg_str = None
if len_int > 1:
arg_str = sys.argv[1]
else:
arg_str = os.path.join(INPUT_PATH, INPUT_DATA_FILE)
# END of if len_int > 1:
# load training data from the disk
train_x, _, train_z, validate_x, _, validate_z = load_data_k(
os.path.join(INPUT_PATH, INPUT_DATA_FILE),
class_index=CLASS_INDEX,
feature_start_index=FEATURE_START_INDEX,
top_n_lines=NO_OF_INPUT_LINES,
validation_part=VALIDATION_PART
)
# create Stochastic Gradient Descent optimizer for the NN model
opt_function = keras.optimizers.SGD(
learning_rate=LEARNING_RATE
)
# create a sequential NN model
model = create_model(
LAYER_1_NEURON_COUNT,
LAYER_2_NEURON_COUNT,
CLASSES_COUNT,
FEATURES_COUNT
)
#
model.compile(loss=['categorical_crossentropy'] * 5,
optimizer=opt_function,
metrics=[['accuracy']] * 5)
# END of if model == None:
# run training and validation
history = model.fit(
train_x, tf.split(train_z, 5, axis=1),
epochs=EPOCHS,
batch_size=BATCH_SIZE,
shuffle=True,
verbose=2
)
print(history.history.keys())
# save the entire NN in HDF5 format
model.save(os.path.join(OUTPUT_PATH, MODEL_FILE))
However, this source code is generating the following error:
C:\ProgramData\Miniconda3\envs\by_nn\python.exe C:/Users/pc/source/repos/by_nn/SCRIPTS/model_k_sequential_model.py
GPU not found!
Epoch 1/1000
Traceback (most recent call last):
File "C:/Users/pc/source/repos/by_nn/SCRIPTS/model_k_sequential_model.py", line 180, in <module>
history = model.fit(
File "C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1098, in fit
tmp_logs = train_function(iterator)
File "C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\eager\def_function.py", line 823, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\eager\def_function.py", line 696, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\eager\function.py", line 2855, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\eager\function.py", line 3065, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\eager\def_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\framework\func_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function *
return step_function(self, iterator)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\keras\engine\training.py:789 run_step **
outputs = model.train_step(data)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\keras\engine\training.py:748 train_step
loss = self.compiled_loss(
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\keras\losses.py:149 __call__
losses = ag_call(y_true, y_pred)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\keras\losses.py:253 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\keras\losses.py:1535 categorical_crossentropy
return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
return target(*args, **kwargs)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\keras\backend.py:4687 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
C:\ProgramData\Miniconda3\envs\by_nn\lib\site-packages\tensorflow\python\framework\tensor_shape.py:1134 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (10, 3) and (10, 15) are incompatible
Process finished with exit code 1
How can I solve this issue?
Edit: I took Swaroop Bhandary's source code, and modified that as follows (check Repl.it code-base):
# custom loss to take into the dependency between the 3 bits
def loss(y_true, y_pred):
l1 = tf.nn.softmax_cross_entropy_with_logits(y_true[:, :3], y_pred[:, :3])
l2 = tf.nn.softmax_cross_entropy_with_logits(y_true[:, 3:6], y_pred[:, 3:6])
l3 = tf.nn.softmax_cross_entropy_with_logits(y_true[:, 6:9], y_pred[:, 6:9])
l4 = tf.nn.softmax_cross_entropy_with_logits(y_true[:, 9:12], y_pred[:, 9:12])
l5 = tf.nn.softmax_cross_entropy_with_logits(y_true[:, 12:], y_pred[:, 12:])
return l1 + l2 + l3 + l4 + l5
if __name__ == "__main__":
len_int = len(sys.argv)
arg_str = None
if len_int > 1:
arg_str = sys.argv[1]
else:
arg_str = os.path.join(INPUT_PATH, INPUT_DATA_FILE)
# END of if len_int > 1:
# load training data from the disk
train_x, train_y, train_z, validate_x,validate_y, validate_z = load_data_k(
os.path.join(INPUT_PATH, INPUT_DATA_FILE),
class_index=CLASS_INDEX,
feature_start_index=FEATURE_START_INDEX,
top_n_lines=NO_OF_INPUT_LINES,
validation_part=VALIDATION_PART
)
#print(train_y)
print("z = " + str(train_z))
# create Stochastic Gradient Descent optimizer for the NN model
opt_function = keras.optimizers.Adam(
learning_rate=LEARNING_RATE
)
# create a sequential NN model
model = create_model(
LAYER_1_NEURON_COUNT,
LAYER_2_NEURON_COUNT,
OUTPUTS_COUNT,
FEATURES_COUNT
)
#
model.compile(optimizer=opt_function, loss=loss, metrics=['accuracy'])
model.fit(train_x, train_z, epochs=EPOCHS,batch_size=BATCH_SIZE)
The network runs, but doesn't train.
Also, I see the following warning:
WARNING:tensorflow:AutoGraph could not transform <function loss at 0x000001F571B4F820> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: module 'gast' has no attribute 'Index'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
from How can convert a functional model into a sequential model?
No comments:
Post a Comment