Error while creating a model for binary classification for text classification

335 views Asked by At

code:

model = create_model()
model.compile(optimize=tf.keras.optimizers.Adam(learning_rate=2e-5),
              loss=tf.keras.losses.BinaryCrossentropy(),
              metrics=[tf.keras.metrics.BinaryAccuracy()])
model.summary()

error:

TypeError                                 Traceback (most recent call last)
<ipython-input-58-cdba04f466b1> in <module>()
      2 model.compile(optimize=tf.keras.optimizers.Adam(learning_rate=2e-5),
      3               loss=tf.keras.losses.BinaryCrossentropy(),
----> 4               metrics=[tf.keras.metrics.BinaryAccuracy()])
      5 model.summary()

1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in _validate_compile(self, optimizer, metrics, **kwargs)
   2981     invalid_kwargs = set(kwargs) - {'sample_weight_mode'}
   2982     if invalid_kwargs:
-> 2983       raise TypeError('Invalid keyword argument(s) in `compile()`: '
   2984                       f'{(invalid_kwargs,)}. Valid keyword arguments include '
   2985                       '"cloning", "experimental_run_tf_function", "distribute",'

TypeError: Invalid keyword argument(s) in `compile()`: ({'optimize'},). Valid keyword arguments include "cloning", "experimental_run_tf_function", "distribute", "target_tensors", or "sample_weight_mode".

can someone have a look into this? here building a model for fine-tuning BERT for text classification

1

There are 1 answers

0
AudioBubble On

I was able to replicate above issue using sample code as shown below

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam


c = np.array([-40, -10, -0, 8, 15, 22, 38])
f = np.array([-40, 14, 32, 46, 59, 72, 100])

model = Sequential()
model.add(Dense(units=1,input_shape=(1,), activation='linear'))

model.compile(loss='mean_squared_error', optimize= Adam(0.1))

history = model.fit(c, f, epochs=5, verbose=0)

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-659b944d282f> in <module>()
     12 model.add(Dense(units=1,input_shape=(1,), activation='linear'))
     13 
---> 14 model.compile(loss='mean_squared_error', optimize= Adam(0.1))
     15 
     16 history = model.fit(c, f, epochs=5, verbose=0)

1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in _validate_compile(self, optimizer, metrics, **kwargs)
   2981     invalid_kwargs = set(kwargs) - {'sample_weight_mode'}
   2982     if invalid_kwargs:
-> 2983       raise TypeError('Invalid keyword argument(s) in `compile()`: '
   2984                       f'{(invalid_kwargs,)}. Valid keyword arguments include '
   2985                       '"cloning", "experimental_run_tf_function", "distribute",'

TypeError: Invalid keyword argument(s) in `compile()`: ({'optimize'},). Valid keyword arguments include "cloning", "experimental_run_tf_function", "distribute", "target_tensors", or "sample_weight_mode".

Fixed code:

Above TypeError clearly guide and it is due to typo, please can you change optimize to optimizer as shown below

model.compile(loss='mean_squared_error', optimizer= Adam(0.1))

For more details please find the gist for reference.