Keras: Bug that depends on how many layers in network - tensorflow

I am having trouble with a seemingly arbitrary bug when using keras. I run into the error "NotFoundError: FeedInputs: unable to find feed output dense_3_target:0" when trying to build a model in keras. The error seems to depend on the number of layers I put in the network (bug when number of layers not equal to 4). Does anyone know what is going on here?
The code and error message:
import tensorflow as tf
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense
tf.reset_default_graph()
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("mnist", one_hot=True)
X_train = mnist.train.images
y_train = mnist.train.labels
X_test = mnist.test.images
y_test = mnist.test.labels
# Hyper Parameters
n_features = 784
n_classes = 10
learning_rate = 0.5
training_epochs = 2
model = Sequential()
model.add(Dense(units = 100, activation = 'relu', input_dim = n_features))
model.add(Dense(units = 50,activation = 'relu'))
model.add(Dense(50,activation = 'relu'))
model.add(Dense(units = n_classes, activation = 'softmax'))
# Step 3: Compile the Model
model.compile(optimizer='adam',loss='categorical_crossentropy')
## Step 4: Train the Model
history = model.fit(X_train,y_train,epochs=10,batch_size = 100,validation_data=(X_test,y_test))
===================================================================
File "<ipython-input-14-1076cda88cc6>", line 43, in <module>
history = model.fit(X_train,y_train,epochs=10,batch_size = 100,validation_data=(X_test,y_test))
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/keras/engine/training.py", line 1037, in fit
validation_steps=validation_steps)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/keras/engine/training_arrays.py", line 199, in fit_loop
outs = f(ins_batch)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2666, in __call__
return self._call(inputs)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2635, in _call
session)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2587, in _make_callable
callable_fn = session._make_callable_from_options(callable_opts)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1480, in _make_callable_from_options
return BaseSession._Callable(self, callable_options)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1441, in __init__
session._session, options_ptr, status)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 519, in __exit__
c_api.TF_GetCode(self.status.status))
NotFoundError: FeedInputs: unable to find feed output dense_3_target:0

Related

ML error says: "ValueError: Input 0 of layer "sequential_6" is incompatible with the layer: expected shape=(None, 42), found shape=(None, 41)"

I needed to increase the accuracy of a model. So I tried using TabNet.
I'm attaching the train & test data in a google drive link
Link: https://drive.google.com/drive/folders/1ronho26m9uX9_ooBTh0M81ox1a43ika8?usp=sharing
Here is my code.
import tensorflow as tf
import pandas as pd
# Load the train and test data into pandas dataframes
train_df = pd.read_csv("train.csv")
#train_df1 = pd.read_csv("train.csv")
test_df = pd.read_csv("test.csv")
# Split the target variable and the features
train_labels = train_df[[f'F_{i}' for i in range(40)]]
#train_labels=trai
test_labels = train_df.target
# Convert the dataframes to tensors
train_dataset = tf.data.Dataset.from_tensor_slices((train_df.values, train_labels.values))
test_dataset = tf.data.Dataset.from_tensor_slices((test_df.values, test_labels.values))
# Define the model using the TabNet architecture
model = tf.keras.models.Sequential([
tf.keras.layers.Input(shape=(train_df.shape[1],)),
tf.keras.layers.Dense(32, activation="relu"),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dense(1)
])
# Compile the model with a mean squared error loss function and the Adam optimizer
model.compile(loss="mean_squared_error", optimizer="adam")
# Train the model on the training data
model.fit(train_dataset.batch(32), epochs=5)
# Make predictions on the test data
predictions = model.predict(test_dataset.batch(32))
#predictions = model.predict(test_dataset)
# Evaluate the model on the test data
mse = tf.keras.losses.mean_squared_error(test_labels, predictions)
print("Mean Squared Error:", mse.numpy().mean())
I don't know what's wrong with it as I'm just a beginner.
Here is the error code:
ValueError Traceback (most recent call last)
<ipython-input-40-87712e1604a9> in <module>
24
25 # Make predictions on the test data
---> 26 predictions = model.predict(test_dataset.batch(32))
27 #predictions = model.predict(test_dataset)
28
1 frames
/usr/local/lib/python3.8/dist-packages/keras/engine/training.py in tf__predict_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1845, in predict_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1834, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1823, in run_step **
outputs = model.predict_step(data)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1791, in predict_step
return self(x, training=False)
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential_6" is incompatible with the layer: expected shape=(None, 42), found shape=(None, 41)
I didn't really know what to do. So I'm expecting help from you guys. Would be grateful to whatever tips you guys give.

The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape received: (None, None)

I am trying to build a binary Prediction model through Keras TensorFlow.
And I am having trouble when I add data augmentation inside.
This is my code.
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_dir = 'C:/train'
test_dir = 'C:/test'
train_data = train_datagen.flow_from_directory(train_dir,target_size=(224,224),class_mode='binary',seed=42)
test_data = test_datagen.flow_from_directory(test_dir,target_size=(224,224),class_mode='binary',seed=42)
tf.random.set_seed(42)
from tensorflow.keras.layers.experimental import preprocessing
data_augmentation = keras.Sequential([
preprocessing.RandomFlip("horizontal"),
preprocessing.RandomZoom(0.2),
preprocessing.RandomRotation(0.2),
preprocessing.RandomHeight(0.2),
preprocessing.RandomWidth(0.2),
], name='data_augmentation')
model_1 = tf.keras.Sequential([
tf.keras.layers.Input(shape=(224,224,3),name='input_layer'),
data_augmentation,
tf.keras.layers.Conv2D(20,3,activation='relu'),
tf.keras.layers.MaxPool2D(pool_size=2),
tf.keras.layers.Conv2D(20,3,activation='relu'),
tf.keras.layers.MaxPool2D(pool_size=2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1,activation='sigmoid')
])
model_1.compile(loss=tf.keras.losses.binary_crossentropy,
optimizer='Adam',
metrics=['accuracy'])
model_1.fit(train_data,epochs=10,validation_data=test_data)
I ready tried this way but error again
inputs = tf.keras.layers.Input(shape=(224,224,3),name='input_layer')
x = data_augmentation(inputs)
x = tf.keras.layers.Conv2D(20,3,activation='relu')(x)
x = tf.keras.layers.Conv2D(20,3,activation='relu')(x)
x = tf.keras.layers.MaxPool2D(pool_size=2)(x)
x = tf.keras.layers.Flatten()(x)
outputs = tf.keras.layers.Dense(1,activation='sigmoid')(x)
model_1 = tf.keras.Model(inputs,outputs)
and this is error message:
Traceback (most recent call last):
File "C:/Users/pondy/PycharmProjects/pythonProject2/main.py", line 60, in <module>
model_1 = tf.keras.Sequential([
File "C:\Users\pondy\PycharmProjects\pythonProject2\venv\lib\site-packages\tensorflow\python\training\tracking\base.py", line 530, in _method_wrapper
result = method(self, *args, **kwargs)
File "C:\Users\pondy\PycharmProjects\pythonProject2\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\pondy\PycharmProjects\pythonProject2\venv\lib\site-packages\keras\layers\core\dense.py", line 139, in build
raise ValueError('The last dimension of the inputs to a Dense layer '
ValueError: The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape received: (None, None)
Process finished with exit code 1
if didn't add data_augmentation inside it's not error
thank you for help<3
your code will work if you remove
preprocessing.RandomHeight(0.2),
preprocessing.RandomWidth(0.2),

using SMOTE with tensorflow's ImageDataGenerator Flow From Directory

Using Python3.6, TF 1.15, imblearn 0.0
I have an imbalanced data set, 3 classes, two are even, one is low. I am trying to apply SMOTE to the dataset, however, I am using flow from directory and I found out I can supposedly obtain X_train and y_train from the data generator using next(train_generator).
The problem is my generator appears to be outputting only one class to the y_train. If I use ravel it gives me the following error:
Found 22089 images belonging to 3 classes.
Found 2136 images belonging to 3 classes.
Found 792 images belonging to 3 classes.
Traceback (most recent call last):
File ".py", line 93, in <module>
X_train_smote, y_train_smote = smote.fit_sample(X_train.reshape(X_train.shape[0], -1), y_train.ravel())
File ".virtualenvs\TF15_Environment-fh5Z3l1i\lib\site-packages\imblearn\base.py", line 77, in fit_resample
X, y, binarize_y = self._check_X_y(X, y)
File ".virtualenvs\TF15_Environment-fh5Z3l1i\lib\site-packages\imblearn\base.py", line 135, in _check_X_y
X, y, reset=True, accept_sparse=accept_sparse
File ".virtualenvs\TF15_Environment-fh5Z3l1i\lib\site-packages\sklearn\base.py", line 432, in _validate_data
X, y = check_X_y(X, y, **check_params)
File ".virtualenvs\TF15_Environment-fh5Z3l1i\lib\site-packages\sklearn\utils\validation.py", line 72, in inner_f
return f(**kwargs)
File ".virtualenvs\TF15_Environment-fh5Z3l1i\lib\site-packages\sklearn\utils\validation.py", line 812, in check_X_y
check_consistent_length(X, y)
File ".virtualenvs\TF15_Environment-fh5Z3l1i\lib\site-packages\sklearn\utils\validation.py", line 256, in check_consistent_length
" samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [2, 6]
(2, 224, 224, 3)
(2, 3)
Process finished with exit code 1
and if I just chuck in y_train without .ravel() I get this:
Found 22089 images belonging to 3 classes.
Found 2136 images belonging to 3 classes.
Found 792 images belonging to 3 classes.
(2, 224, 224, 3)
(2, 3)
Traceback (most recent call last):
File ".py", line 93, in <module>
X_train_smote, y_train_smote = smote.fit_sample(X_train.reshape(X_train.shape[0], -1), y_train)
File ".virtualenvs\TF15_Environment-fh5Z3l1i\lib\site-packages\imblearn\base.py", line 80, in fit_resample
self.sampling_strategy, y, self._sampling_type
File ".virtualenvs\TF15_Environment-fh5Z3l1i\lib\site-packages\imblearn\utils\_validation.py", line 533, in check_sampling_strategy
" Got {} class instead".format(np.unique(y).size)
ValueError: The target 'y' needs to have more than 1 class. Got 1 class instead
Here is me code, appreciate any advice! Thanks :)
import datetime
import numpy as np
import cv2
import tensorflow as tf
from tensorflow.keras import backend as k
from tensorflow.keras.applications.mobilenet import MobileNet
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.mobilenet import preprocess_input
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dropout, Dense
from tensorflow.keras.callbacks import TensorBoard, EarlyStopping
from imblearn.over_sampling import SMOTE
smote = SMOTE()
k.clear_session()
tf.set_random_seed(42)
np.random.seed(42)
currentDay = datetime.date.today()
now = datetime.datetime.now()
t = now.strftime("%H-%M-%S")
NAME = f'{currentDay}_{t}new_model_001.h5'
tboard = TensorBoard(log_dir=f'logs\\{NAME}',
update_freq="epoch",
histogram_freq=1,
write_grads=True,
write_graph=True,
)
# config
img_width = 224
img_height = 224
INPUT_DEPTH = 3
input_shape = (img_height, img_width, INPUT_DEPTH)
TRAIN_DATA_DIR = 'dataset/train/'
VALIDATION_DATA_DIR = 'dataset/validation/'
TESTING_DATA_DIR = 'dataset/test/'
MODEL_DIR = 'h5_Models/'
EPOCHS = 500
PATIENCE = 25
BATCH_SIZE = 2
MODEL_NAME = 'new_model_001.h5'
train_datagen = ImageDataGenerator(
rescale=1/255,
# zca_whitening=True,
# zca_epsilon=0.1,
# rotation_range=5,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
zoom_range=(0.95, 0.95),
# data_format='channels_last',
horizontal_flip=True,
# vertical_flip=True,
fill_mode='nearest'
)
validation_datagen = ImageDataGenerator(rescale=1/255)
test_datagen = ImageDataGenerator(rescale=1/255)
train_generator = train_datagen.flow_from_directory(
TRAIN_DATA_DIR,
# color_mode='grayscale',
target_size=(img_height, img_width),
batch_size=BATCH_SIZE,
class_mode='categorical',
shuffle=True)
validation_generator = validation_datagen.flow_from_directory(
VALIDATION_DATA_DIR,
# color_mode='grayscale',
target_size=(img_height, img_width),
batch_size=BATCH_SIZE,
class_mode='categorical')
testing_generator = test_datagen.flow_from_directory(
TESTING_DATA_DIR,
# color_mode='grayscale',
target_size=(img_height, img_width),
batch_size=BATCH_SIZE,
class_mode='categorical',
)
X_train, y_train = next(train_generator)
print(X_train.shape)
print(y_train.shape)
X_train_smote, y_train_smote = smote.fit_sample(X_train.reshape(X_train.shape[0], -1), y_train.ravel())
print(X_train_smote.count)
X_train_smote = X_train_smote.reshape(X_train_smote.shape[0], 224, 224, 3)
When you use next(train_generator), you are simply considering a single batch of the train dataset which may just have a single class of images for some batches. SMOTE, however, if to be correctly applied, is applied with the entire dataset in consideration or a sample of it that is representative of all the classes and matching in distribution.

Unable to import a pretrained model after calling Keras.backend.clear_session()

I am trying to train a model with new data samples in each iteration in a loop in keras (using tensorflow backend). Due to GPU memory error after some iterations, I appended K.clear_session(). However, after one iteration, the code throws the error:
'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(7, 7, 3, 64), dtype=float32) is not an element of this graph.
If I remove K.clear_session() at end, there is no error. Is there anyone who can explain why this error comes in second iteration?
I tried other methods (for gpu release) but none of them worked and this is my last option. But it throws error. I have pasted an example code which can produce the error. Please NOTE that this is not the actual code, I just made an example to reproduce the error which I am facing in actual code.
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
import tensorflow as tf
import random
seed_value= 0
import os
import keras
os.environ['PYTHONHASHSEED']=str(seed_value)
random.seed(0)
np.random.seed(0)
from keras import backend as K
from keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
for i in range(3):
base_model = tf.keras.applications.resnet50.ResNet50(weights='imagenet', input_shape=(32, 32, 3),
include_top=False)
x = base_model.output
x = tf.keras.layers.GlobalAveragePooling2D()(x)
output = tf.keras.layers.Dense(10, activation='softmax',
kernel_initializer=tf.keras.initializers.RandomNormal(seed=4))(x)
model = tf.keras.Model(inputs=base_model.input, outputs=output)
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
for layer in base_model.layers:
layer.trainable = False
optimizer = tf.train.AdamOptimizer(learning_rate=0.0001)
model.compile(optimizer=optimizer, loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=1024,epochs=1,verbose=1)
K.clear_session()
Traceback (most recent call last):
File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1092, in _run
subfeed, allow_tensor=True, allow_operation=False)
File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3490, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3569, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("Placeholder:0", shape=(7, 7, 3, 64), dtype=float32) is not an element of this graph.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/codes/experiments-AL/breakhis/40X-M-B/codes-AL/error_debug.py", line 22, in <module>
include_top=False)
File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\applications\__init__.py", line 70, in wrapper
return base_fun(*args, **kwargs)
File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\applications\resnet50.py", line 32, in ResNet50
return resnet50.ResNet50(*args, **kwargs)
File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\keras_applications\resnet50.py", line 291, in ResNet50
model.load_weights(weights_path)
File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1544, in load_weights
saving.load_weights_from_hdf5_group(f, self.layers)
File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\saving.py", line 806, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\backend.py", line 2784, in batch_set_value
get_session().run(assign_ops, feed_dict=feed_dict)
File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
run_metadata_ptr)
File "C:\Users\sirshad\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1095, in _run
'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(7, 7, 3, 64), dtype=float32) is not an element of this graph.
Process finished with exit code 1
I was able to overcome this issue by saving the imagenet pre-trained model to disk and then loading everytime in loop after I call tf.keras.backend.clear_session(). So saving the base model to file and then loading works. But I am still confused why it did not work before with
base_model = tf.keras.applications.resnet50.ResNet50

Save the Keras model error: AttributeError: 'numpy.dtype' object has no attribute 'item'

I have tried to save my Keras model in pycharm where I got the error, this is how I created the model:
main_input = Input(shape=(X_train.shape[1],), dtype=X_train.dtype,
name='main_input')
xx = Embedding(output_dim=512, input_dim=3000, input_length=len(X))
(main_input)
xx= SpatialDropout1D(0.4)(xx)
lstm_out = LSTM(64)(xx)
#lstm_out = Dense(3,activation='softmax')(lstm_out)
from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model
auxiliary_input = Input(shape=(Z_train.shape[1],), name='aux_input')
auxB= Input(shape=(hasB_train.shape[1],), name='aux_B')
auxM = Input(shape=(hasM_train.shape[1],), name='aux_M')
auxBM_input = keras.layers.concatenate([ auxB, auxM])
auxiliary_output = Dense(3, activation='softmax', name='aux_output') (lstm_out)
auxBM_output = Dense(3, activation='softmax', name='auxBM_output') (auxBM_input)
x = keras.layers.concatenate([lstm_out, auxiliary_input, auxBM_input])
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
main_output = Dense(3, activation='sigmoid', name='main_output')(x)
model = Model(inputs=[main_input, auxiliary_input, auxB, auxM], outputs= [main_output, auxiliary_output, auxBM_output])
model.compile(optimizer='rmsprop', loss='categorical_crossentropy' ,metrics = ['accuracy'], loss_weights=[4, 1, 10])
model.summary()
when I run the this code model.save('model.h5'), I receive the below error:
Traceback (most recent call last): File
"C:/.../ENV/newDataset/combined3.py", line 209, in
model.save('blah.h5') File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\site-packages\keras\engine\network.py",
line 1085, in save
save_model(self, filepath, overwrite, include_optimizer) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\site-packages\keras\engine\saving.py",
line 117, in save_model
}, default=get_json_type).encode('utf8') File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\json__init__.py",
line 237, in dumps
**kw).encode(obj) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\json\encoder.py",
line 198, in encode
chunks = self.iterencode(o, _one_shot=True) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\json\encoder.py",
line 256, in iterencode
return _iterencode(o, 0) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\site-packages\keras\engine\saving.py",
line 84, in get_json_type
return obj.item() AttributeError: 'numpy.dtype' object has no attribute 'item'
I have no problem, if I run the below code:
model = Sequential()
model.add(Embedding(max_fatures, embed_dim,input_length = X.shape[1]))
model.add(SpatialDropout1D(0.4))
model.add(LSTM(lstm_out, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(3,activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy'])
X_train, X_test, Y_train, Y_test = train_test_split(X,Y,train_size=0.8, random_state = 42)
model.fit(X_train, Y_train, epochs = 1, batch_size=32,shuffle=True)
model.save('test.h5')
I believe you are having this issue because of how Keras handles the dtype argument when you are creating a Functional Model. Keras expects the dtype to be just a simple string and not a numpy.dtype object, and therefore, it will have difficulty saving the model when you pass a numpy object into this argument.
To adjust, I would use one of the strings to describe the data input type, as suggested at https://keras.io/backend/.
I had a similar issue, and when I changed the dtype argument to what Keras was expecting (a string), I was able to save the model without any additional problem.
To fix your issue, I would suggest, changing the dtype=X_train.dtype argument to dtype=X_train.dtype.name, as this would produce the string form of the dtype, which can be handled by Keras.