AttributeError: 'Sequential' object has no attribute 'predict_proba' - tensorflow

predict_proba returns the error in the neural network
i saw the example on this link https://machinelearningmastery.com/how-to-make-classification-and-regression-predictions-for-deep-learning-models-in-keras/
https://faroit.com/keras-docs/1.0.0/models/sequential/#the-sequential-model-api
I am using Tensorflow Version: 2.6.0
Code:
#creating the object (Initializing the ANN)
import tensorflow as tf
from tensorflow import keras
LAYERS = [
tf.keras.layers.Dense(50, activation="relu", input_shape=X_train.shape[1:]),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Dense(25, activation="relu"),
tf.keras.layers.Dense(10, activation="relu"),
tf.keras.layers.Dense(5, activation="relu"),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1, activation='sigmoid')
]
LOSS = "binary_crossentropy"
OPTIMIZER = tf.keras.optimizers.Adam(learning_rate=1e-3)
model_cEXT = tf.keras.models.Sequential(LAYERS)
model_cEXT.compile(loss=LOSS , optimizer=OPTIMIZER, metrics=['accuracy'])
EPOCHS = 100
checkpoint_cb = tf.keras.callbacks.ModelCheckpoint("model_cEXT.h5", save_best_only=True)
early_stopping_cb = tf.keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True)
tensorboard_cb = tf.keras.callbacks.TensorBoard(log_dir="logs")
CALLBACKS = [checkpoint_cb, early_stopping_cb, tensorboard_cb]
model_cEXT.fit(X_train, y_train['cEXT'], epochs = EPOCHS, validation_data=(X_test, y_test['cEXT']), callbacks = CALLBACKS)
model_cEXT.predict_proba(X_test)
Error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-72-8f06353cf345> in <module>()
----> 1 model_cEXT.predict_proba(X_test)
AttributeError: 'Sequential' object has no attribute 'predict_proba'
Edit:
i need sklearn's like predict_proba output it is needed for visualization
skplt.metrics.plot_precision_recall_curve(y_test['cEXT'].values, y_prob)
plt.title('Precision-Recall Curve - cEXT')
plt.show()

Use this code instead
predict_prob=model.predict([testa,testb])
predict_classes=np.argmax(predict_prob,axis=1)

New Version might not have predict_proba method so i have creadted my own using .predict method
def predict_prob(number):
return [number[0],1-number[0]]
y_prob = np.array(list(map(predict_prob, model_cEXT.predict(X_test))))
y_prob

Related

Saving TensorFlow Neural Network KFold Cross Validation model

I am working on a sample Neural Network with KFold cross validation using TensorFlow 2.4.1. and sklearn.
Unfortunately, I am not able to save the model.
def my_model(self,):
inputs = keras.Input(shape=(48, 48, 3))
x = layers.Conv2D(filters=4, kernel_size=self.k_size, padding='same', activation="relu")(inputs)
x = layers.BatchNormalization()(x)
x = layers.MaxPool2D()(x)
x = layers.Flatten()(x)
output = layers.Dense(10, activation='softmax')(x)
model = keras.Model(inputs=inputs, outputs=output)
model.compile(optimizer='adam',
loss=[keras.losses.SparseCategoricalCrossentropy(from_logits=True)],
metrics=['accuracy'])
return model
def train_model(self):
try:
os.mkdir('model/saved_models')
except OSError:
pass
try:
os.mkdir('model/saved_graphs')
except OSError:
pass
kf = KFold(n_splits=3)
for train_index, test_index in kf.split(self.x_train):
x_train, x_test = self.x_train[train_index], self.x_train[test_index]
y_train, y_test = self.y_train[train_index], self.y_train[test_index]
model = self.my_model()
print(model.summary())
trained_model = model.fit(x_train, y_train, epochs=self.epochs, steps_per_epoch=10, verbose=2)
trained_model = trained_model.history
print('Model evaluation', model.evaluate(x_test, y_test, verbose = 2))
trained_model.save(f'model/saved_models/dummy_model_{date}')
return trained_model
I am getting a following error:
trained_model.save(f'model/saved_models/dummy_model_{date}')
AttributeError: 'dict' object has no attribute 'save'
I am not able to think of a way to take the trained model out of the for loop. And this might be the possible reason I can think of for this problem.
Can anybody suggest how we can solve this issue? Or is there any other way to build a ANN with KFold?
Thanks.
Yea your code has some typo:
trained_model = trained_model.history # This is your train stats, so your train stats is a dictionary
model.save(f'model/saved_models/dummy_model_{date}') # This is what your saving the actual model

How to efficiently use a tf.data.Dataset made of ordereddict?

Using TensorFlow 2.3.1, the code snippet below fails.
import tensorflow as tf
url = "https://storage.googleapis.com/download.tensorflow.org/data/creditcard.zip"
tf.keras.utils.get_file(
origin=url,
fname='creditcard.zip',
cache_dir="/tmp/datasets/",
extract=True)
ds = tf.data.experimental.make_csv_dataset(
"/tmp/datasets/*.csv",
batch_size=2048,
label_name="Class",
select_columns=["V1","V2","Class"],
num_rows_for_inference=None,
shuffle_buffer_size=600,
ignore_errors=True)
model = tf.keras.Sequential(
[
tf.keras.layers.Dense(256, activation="relu"),
tf.keras.layers.Dense(1, activation="sigmoid", name="labeling"),
],
)
model.compile(
optimizer=tf.keras.optimizers.Adam(1e-2),
loss="binary_crossentropy",
)
model.fit(
ds,
steps_per_epoch=5,
epochs=3,
)
The error stack is
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-c79f80f9d0fd> in <module>
----> 1 model.fit(
2 ds,
3 steps_per_epoch=5,
4 epochs=3,
5 )
[...]
ValueError: Layer sequential expects 1 inputs, but it received 2 input tensors. Inputs received: [<tf.Tensor 'ExpandDims:0' shape=(2048, 1) dtype=float32>, <tf.Tensor 'ExpandDims_1:0' shape=(2048, 1) dtype=float32>]
The solution I use so far is
def workaround(features, labels):
return (tf.stack(list(features.values()), axis=1), labels)
model.fit(
ds.map(workaround),
steps_per_epoch=5,
epochs=3,
)
My questions to you TF gurus:
Am I doing the right thing or is there a better solution?
Performance wise, is that solution viable for a dataset that would not fit in memory?
I'am not sure your code can fit the data in memoey or not .
If not , you can change your code like this :
import tensorflow as tf
url = "https://storage.googleapis.com/download.tensorflow.org/data/creditcard.zip"
ds = tf.data.experimental.make_csv_dataset(
"/tmp/datasets/*.csv",
batch_size=2048,
label_name="Class",
select_columns=["V1","V2","Class"],
num_rows_for_inference=None,
ignore_errors=True,
num_epochs = 1,
shuffle_buffer_size=2048*1000,
prefetch_buffer_size=tf.data.experimental.AUTOTUNE
)
input_list = []
for column in ["V1", "V2"]:
_input = tf.keras.Input(shape=(1,))
input_list.append(_input)
concat = tf.keras.layers.Concatenate(name="concat")(input_list)
dense = tf.keras.layers.Dense(256, activation="relu", name="dense", dtype='float64' )(concat)
output_dense = tf.keras.layers.Dense(1, activation="sigmoid", name="labeling", dtype='float64')(dense)
model = tf.keras.Model(inputs=input_list, outputs=output_dense)
model.compile(
optimizer=tf.keras.optimizers.Adam(1e-2),
loss="binary_crossentropy",
)
model.fit(
ds,
steps_per_epoch=5,
epochs=10,
)

How to execute TensorFlow 2 Keras Sequential model in eager mode when "compile"?

I want to build a loss in a "pythonic" way using the eager execution of TF2, but even in eager mode, Keras is passing non-eager tensors.
Code:
def conditional_loss(self, y_true, y_pred):
print(y_true)
return 0
def define_model(self):
self.model = keras.Sequential([
keras.layers.Dense(units=768),
keras.layers.BatchNormalization(),
keras.layers.ReLU(),
keras.layers.Dropout(0.2),
keras.layers.Dense(units=128),
keras.layers.BatchNormalization(),
keras.layers.ReLU(),
keras.layers.Dropout(0.2),
keras.layers.Dense(units=5, activation='softmax')
])
self.model.compile(optimizer='adam',
loss=self.conditional_loss,
metrics=[self.conditional_loss,
keras.metrics.sparse_categorical_accuracy]
)
self.model.fit(
self.train_dataset,
epochs=10,
validation_data=self.test_dataset,
callbacks=[tensorboard_callback, model_callback],
)
If I print y_true in conditional_loss TF prints a non-eager tensor.
Tensor("metrics/conditional_loss/Cast:0", shape=(None, 1), dtype=float32)
If I build my own keras.Model() I can call it with the argumentdynamic=True to enable eager execution. (Reference). Exists a way to do it in keras.Sequential() ?
To do that you have to call model.compile() with the argument run_eagerly=True. Following the question example:
self.model.compile(optimizer='adam',
loss=self.conditional_loss,
metrics=[self.conditional_loss,
keras.metrics.sparse_categorical_accuracy],
run_eagerly=True
)

Expected a callable, found non-callable tensorflow_federated.python.learning.model_utils.EnhancedTrainableModel

Unable to use TFF's build_federated_averaging_process(). Followed the tutorial from the TFF federated documentation.
Here's my model code:
X_train = <valuex>
Y_train = <valuey>
def model_fn():
model = tf.keras.models.Sequential([
tf.keras.layers.Conv1D(32,dtype="float64",kernel_size=3,padding='same',activation=tf.nn.relu,input_shape=(X_train.shape[1], X_train.shape[2])),
tf.keras.layers.MaxPooling1D(pool_size=3),
tf.keras.layers.Conv1D(64,kernel_size=3,padding='same',activation=tf.nn.relu),
tf.keras.layers.MaxPooling1D(pool_size=3),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation=tf.nn.relu),
tf.keras.layers.Dropout(0.45),
tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
])
model.compile(
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
optimizer=tf.keras.optimizers.SGD(learning_rate=0.05),
metrics=[tf.keras.metrics.Accuracy()])
model.summary()
return tff.learning.from_compiled_keras_model(model, sample_batch)
iterative_process = tff.learning.build_federated_averaging_process(model_fn())
I get the error:
TypeError: Expected a callable, found non-callable tensorflow_federated.python.learning.model_utils.EnhancedTrainableModel.
The argument to build_federated_averaging_process should be the model_fn function, not the return value from invoking it.
Try changing this line:
iterative_process = tff.learning.build_federated_averaging_process(model_fn())
to:
iterative_process = tff.learning.build_federated_averaging_process(model_fn)

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.