For some reason the start_from_epoch argument in the EarlyStopping callback is not recognised.
import tensorflow as tf
cbk = [tf.keras.callbacks.EarlyStopping(monitor='val_loss',
min_delta=0,
patience=3,
restore_best_weights = True,
start_from_epoch=10
)]
TypeError: __init__() got an unexpected keyword argument 'start_from_epoch'
even though it's clearly listed as an argument in the documentation
I have recent versions of TensorFlow installed, so anyone know why this is the case?
I think you are using older version of Tensorflow Try this
!pip install -q tensorflow==2.11.0
monitor='val_loss',
min_delta=0,
patience=0,
verbose=0,
mode='auto',
baseline=None,
restore_best_weights=False,
start_from_epoch=0
)
Related
My tensorflow recognizes my gpu
However, when I call model.fit() on my data it shows:
epoch(1/2) and then the kernel dies immediately
If I run this in a separate virtual environment with no GPU it works fine:
I have simplified the model architecture and number of training points to only ten as a quick test and it still fails
Simple example
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
model = keras.Sequential()
model.add(Dense(4,
activation='relu'))
model.add(Dense(1, activation='sigmoid'))
opt = keras.optimizers.Adam(learning_rate=.001)
model.compile(loss = 'binary_crossentropy' , optimizer = opt, metrics = ['accuracy'] )
info = model.fit(X_train, y_train, epochs=2, batch_size=2,shuffle=True, verbose=1)
versions:
Python 3.8.8
Num GPUs Available 1
2.5.0-dev20210227
2.4.3
cuda v11.2
I am going to answer my own question rather than deleting this because maybe someone else will be making the same simple mistake I was.
The main mistake I made was having the incorrect CUDA download. you can refer to the what versions are correct at this link:
https://www.tensorflow.org/install/source#gpu
TLDR: Just follow this video:
https://www.youtube.com/watch?v=hHWkvEcDBO0
This also highlighted the importance of a virtual environment where you control the package versions to prevent incompatibilities.
I had the same problem. I transferred the code into a python file and found the root cause. In my case it was copying cudnn dll files into C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin. Check the following link as well:
Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found
I fine.tuned an SSD model to recognize a custom object.
I followed the tutorials, ran the training process and exported the model, I tested it for inference and everything works great.
So, now I have a structure like:
exported models/
|
---- SSD_custom_model/
|
--------checkpoint/
--------saved_model/
--------pipeline.config
which I assume is what is referred to as "Saved model" in the TensorFlow documentation.
So, I wanted to convert this model to TensorFlow Lite to test in on an Android device, I checked the tutorials and I'm trying:
import tensorflow as tf
saved_model_dir = 'exported-models/SSD_custom_model/'
# # Convert the model
## I tried either just
# converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
## or, with more options
converter = tf.lite.TFLiteConverter.from_saved_model(
saved_model_dir, signature_keys=['serving_default'])
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.experimental_new_converter = True
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
# Save the model.
with open('tflite/custom_model.tflite', 'wb') as f:
f.write(tflite_model)
And I'm getting the error
File "/home/lews/anaconda3/envs/tf/lib/python3.8/site-packages/tensorflow/lite/python/convert.py", line 216, in toco_convert_protos
raise ConverterError(str(e))
tensorflow.lite.python.convert.ConverterError: <unknown>:0: error: loc(callsite(callsite("map/TensorArrayV2_1#__inference_call_func_11694" at "StatefulPartitionedCall#__inference_signature_wrapper_14068") at "StatefulPartitionedCall")): requires element_shape to be 1D tensor during TF Lite transformation pass
<unknown>:0: note: loc("StatefulPartitionedCall"): called from
<unknown>:0: error: loc(callsite(callsite("map/TensorArrayV2_1#__inference_call_func_11694" at "StatefulPartitionedCall#__inference_signature_wrapper_14068") at "StatefulPartitionedCall")): failed to legalize operation 'tf.TensorListReserve' that was explicitly marked illegal
<unknown>:0: note: loc("StatefulPartitionedCall"): called from
It seems to be complaining about the input shape ('requires element_shape to be 1D tensor during TF Lite transformation pass'). Maybe I should've modified something about the model before the fine-tuning process? Or after that?
Hi,I'm doing the same work and encountered the same error, but I sovled it.
The model I converted is SSD-Mobile-v2, and I'm using tensorflow 2_4, so I believe this will work for you.
All you need to do is to create a new conda environment (python 3.8 is ok), and then install tf-nightly:
pip install tf-nightly
It's important to note that the version of tf-nightly must be >= 2.5.
At first I used the tf-nightly 2.3, I encountered another error. Then I upgrade it to 2.5, the converter finally works.
In TF 1.14, below code will raise exception.
optimizer = tf.keras.optimizers.SGD(learning_rate=params['lr'])
train_op = optimizer.minimize(loss, global_step=tf.train.get_or_create_global_step())
The exception is
TypeError: minimize() got an unexpected keyword argument 'global_step'
It seems that tf.keras.optimizers in unavailable in TF 1.14. However, why the global_step is gone in tf.keras.optimizers.SGD? It is supposed to be there in tf.train.Optimizer of TF 1.14
tf.keras.optimizers.Optimizer and tf.train.Optimizer are two distinct classes. There was never global_step in tf.keras.optimizers.Optimizer
I am trying to use pre-trained elmo embeddings in jupyter notebook with python 3.7.
Tensorflow version - 1.14.0
This is my code
def ElmoEmbeddingLayer(x):
print(x.shape)
module = hub.Module("https://tfhub.dev/google/elmo/3", trainable=False)
embeddings = module(tf.squeeze(tf.cast(x, tf.string)), signature="default", as_dict=True)["elmo"]
return embeddings
elmo_dim=1024
elmo_input = Input(shape=(None,), dtype=tf.string)
elmo_embedding = Lambda(ElmoEmbeddingLayer, output_shape=(None,elmo_dim))(elmo_input)
x = Dense(1)(elmo_embedding)
x = Activation('relu')(x)
model = Model(inputs=[elmo_input], outputs=x)
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(x_train, y_train, epochs=1,validation_data=(x_test, y_test))
However I'm getting a runtime error that is
FailedPreconditionError: Error while reading resource variable module/bilm/CNN_proj/W_proj from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/module/bilm/CNN_proj/W_proj/N10tensorflow3VarE does not exist.
[[{{node lambda/module_apply_default/bilm/MatMul_9/ReadVariableOp}}]]
To use model pieces from TF Hub in building a Keras model, use the hub.KerasLayer class. It implements Keras's way of collecting variables for initialization. With tensorflow_hub 0.7.0 (and preferably tensorflow 1.15), you can also use it for older TF Hub modules (like the https://tfhub.dev/google/elmo/3 in your example), subject to some caveats, see tensorflow.org/hub/migration_tf2
For context: The older hub.Module class is for building models in the classic TF1 way (like tf.layers). It implements the old-style way of collecting variables for initialization via the GLOBAL_VARIABLES collection of the tf.Graph. Those are missed in your case. (You could try to initialize them manually in the Session returned bytf.compat.v1.keras.backend.get_session(), but that's getting weird.)
I am new to tensorflow and trying to set it up.
When I try to train a model using CuDNNGRU it seems to load correctly and then gives an error :
tensorflow.python.framework.errors_impl.NotFoundError: Op type not
registered 'CudnnRNN'
I do see a Cudnn_rnn directory in tensorflow/contrib for what that is worth.
I have python 3.6 and VS2013.
I have tried the following, but still getting an error:
Both Cuda 8/9
uninstalling/reinstalling tensorflow/Theano/Keras/TensorFlow
Honestly the setup seems so convoluted and touchy, I may have screwed something up.
Am I missing a to-do? Some way to manually fix? Thanks!
Sample code I am trying to replicate:
def get_model(embedding, sequence_length, dropout_rate, recurrent, dense_size):
input_layer = Input(shape=(sequence_length,))
embedding_layer = Embedding(embedding.shape[0], embedding.shape[1],
weights=[embedding], trainable=False)(input_layer)
x = Bidirectional(CuDNNGRU(recurrent, return_sequences=True))(embedding_layer)
x = Dropout(dropout_rate)(x)
x = Bidirectional(CuDNNGRU(recurrent, return_sequences=False))(x)
x = Dense(dense_size, activation="relu")(x)
I fixed this by doing :
pip install tensorflow --ignore-installed --upgrade
and then
from tensorflow.python.client import device_lib print(device_lib.list_local_devices())