All Questions
Tagged with tf.keras tensorflow2.0
592
questions
65
votes
4
answers
21k
views
WARNING:tensorflow:sample_weight modes were coerced from ... to ['...']
Training an image classifier using .fit_generator() or .fit() and passing a dictionary to class_weight= as an argument.
I never got errors in TF1.x but in 2.1 I get the following output when starting ...
46
votes
4
answers
96k
views
Running the Tensorflow 2.0 code gives 'ValueError: tf.function-decorated function tried to create variables on non-first call'. What am I doing wrong?
error_giving_notebook
non_problematic_notebook
As it can be seen that I have used tf.function decorator in the 'error_giving_notebook' and it throws a ValueError while the same notebook without any ...
36
votes
2
answers
6k
views
Custom TensorFlow Keras optimizer
Suppose I want to write a custom optimizer class that conforms to the tf.keras API (using TensorFlow version>=2.0). I am confused about the documented way to do this versus what's done in ...
34
votes
3
answers
12k
views
Should I use @tf.function for all functions?
An official tutorial on @tf.function says:
To get peak performance and to make your model deployable anywhere,
use tf.function to make graphs out of your programs. Thanks to
AutoGraph, a ...
28
votes
4
answers
26k
views
Save model every 10 epochs tensorflow.keras v2
I'm using keras defined as submodule in tensorflow v2. I'm training my model using fit_generator() method. I want to save my model every 10 epochs. How can I achieve this?
In Keras (not as a ...
20
votes
5
answers
22k
views
How to graph tf.keras model in Tensorflow-2.0?
I upgraded to Tensorflow 2.0 and there is no tf.summary.FileWriter("tf_graphs", sess.graph). I was looking through some other StackOverflow questions on this and they said to use tf.compat.v1.summary ...
20
votes
3
answers
31k
views
Tensorflow 2: how to switch execution from GPU to CPU and back?
In tensorflow 1.X with standalone keras 2.X, I used to switch between training on GPU, and running inference on CPU (much faster for some reason for my RNN models) with the following snippet:
keras....
19
votes
2
answers
6k
views
Input pipeline w/ keras.utils.Sequence object or tf.data.Dataset?
I am currently using a tf.keras.utils.Sequence object to generate image batches for a CNN. I am using Tensorflow 2.2 and the Model.fit method for the model. When I fit the model, the following ...
16
votes
3
answers
8k
views
tf.data.Dataset iterator returning Tensor("IteratorGetNext:1", shape=(None, 16), dtype=int32) but cannot get the values of the Tensors
I am trying to write a Custom Model in which I am writing a custom train_step function
I am creating a 'tf.data.Dataset` from a Custom Datagenerator like
tds = tf.data.Dataset.from_generator(tdg....
14
votes
1
answer
1k
views
How to perform gradient accumulation WITH distributed training in TF 2.0 / 1.14.0-eager and custom training loop (gradient tape)?
Background:
I have a model and I'm trying to port it to TF 2.0 to get some sweet eager execution, but I just can't seem to figure out how to do distributed training (4 GPU's) AND perform gradient ...
13
votes
2
answers
49k
views
TypeError: __init__() got an unexpected keyword argument 'name' when loading a model with Custom Layer
I made a custom layer in keras for reshaping the outputs of a CNN before feeding to ConvLSTM2D layer
class TemporalReshape(Layer):
def __init__(self,batch_size,num_patches):
super(...
12
votes
1
answer
13k
views
"TypeError: can't pickle weakref objects" when pickling a deep learning model
When I run
pickle.dump(model,open('modelDL.pkl','wb'))
I get
TypeError: can't pickle weakref objects
I have a created a deep learning model which I am trying to save. The model:
model = Sequential()
...
11
votes
1
answer
4k
views
How to make custom loss with extra input in tensorflow 2.0
I'm having a lot of trouble getting a custom loss function with an extra argument to work in TF 2.0 using tf.keras and a dataset.
In the following case, the extra argument is the input data into the ...
11
votes
1
answer
644
views
Tensorflow 2.0: Accessing a batch's tensors from a callback
I'm using Tensorflow 2.0 and trying to write a tf.keras.callbacks.Callback that reads both the inputs and outputs of my model for the batch.
I expected to be able to override on_batch_end and access ...
10
votes
2
answers
7k
views
Passing `training=true` when using Tensorflow 2's Keras Functional API
When operating in graph mode in TF1, I believe I needed to wire up training=True and training=False via feeddicts when I was using the functional-style API. What is the proper way to do this in TF2?
...
10
votes
2
answers
9k
views
How do you apply layer normalization in an RNN using tf.keras?
I would like to apply layer normalization to a recurrent neural network using tf.keras. In TensorFlow 2.0, there is a LayerNormalization class in tf.layers.experimental, but it's unclear how to use it ...
10
votes
1
answer
5k
views
CUDNN_STATUS_BAD_PARAM when trying to perform inference on a LSTM Seq2Seq with masked inputs
I'm using keras layers on tensorflow 2.0 to build a simple LSTM-based Seq2Seq model for text generation.
versions I'm using: Python 3.6.9, Tensorflow 2.0.0, CUDA 10.0, CUDNN 7.6.1, Nvidia driver ...
9
votes
2
answers
4k
views
Is it still necessary to implement `compute_output_shape()` when defining a custom tf.keras Layer?
I have implemented a custom Layer in tf.keras, using TensorFlow 2.1.0.
In the past, when using the stand-alone Keras, it was important to define the compute_output_shape(input_shape) method in any ...
9
votes
4
answers
17k
views
In TensorFlow 2.0 with eager-execution, how to compute the gradients of a network output wrt a specific layer?
I have a network made with InceptionNet, and for an input sample bx, I want to compute the gradients of the model output w.r.t. the hidden layer. I have the following code:
bx = tf.reshape(x_batch[0,...
9
votes
1
answer
4k
views
Use TensorBoard with Keras Tuner
I ran into an apparent circular dependency trying to use log data for TensorBoard during a hyper-parameter search done with Keras Tuner, for a model built with TF2. The typical setup for the latter ...
9
votes
5
answers
7k
views
How to save/restore large model in tensorflow 2.0 w/ keras?
I have a large custom model made with the new tensorflow 2.0 and mixing keras and tensorflow. I want to save it (architecture and weights).
Exact command to reproduce:
import tensorflow as tf
...
8
votes
3
answers
7k
views
Keras Model AttributeError: 'str' object has no attribute 'call'
I'm trying to convert my Keras hdf5 file into a TensorFlow Lite file with the following code:
import tensorflow as tf
# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model("/...
8
votes
2
answers
9k
views
How to correctly use the Tensorflow MeanIOU metric?
I want to use the MeanIoU metric in keras (doc link). But I don't really understand how it could be integrated with the keras api. In the example, the prediction and the ground truth are given as ...
8
votes
2
answers
23k
views
TensorFlow 2.0 How to get trainable variables from tf.keras.layers layers, like Conv2D or Dense
I have been trying to get the trainable variables from my layers and can't figure out a way to make it work. So here is what I have tried:
I have tried accessing the kernel and bias attribute of the ...
8
votes
2
answers
10k
views
Unable to import Keras(from TensorFlow 2.0) in PyCharm 2019.2
I have just installed the stable version of TensorFlow 2.0 (released on October 1st 2019) in PyCharm.
The problem is that the keras package is unavailable.
The actual error is :
"cannot import ...
8
votes
1
answer
3k
views
Can SigmoidFocalCrossEntropy in Tensorflow (tf-addons) be used in Multiclass Classification? ( What is the right way)?
Focal Loss given in Tensorflow is used for class imbalance. For Binary class classification, there are a lots of codes available but for Multiclass classification, a very little help is there. I ran ...
8
votes
1
answer
16k
views
Tensorflow model.fit() using a Dataset generator
I am using the Dataset API to generate training data and sort it into batches for a NN.
Here is a minimum working example of my code:
import tensorflow as tf
import numpy as np
import random
def ...
8
votes
1
answer
9k
views
Dictionary of tensors input for Keras Functional API TensorFlow 2.0
I have a dataset of dictionary of tensors, and the following model defined using the subclassing API:
class Model(tf.keras.Model):
def __init__(self):
super().__init__()
self....
8
votes
1
answer
15k
views
AttributeError: The layer has never been called and thus has no defined input shape
I'm tring to build an autoencoder in TensorFlow 2.0 by creating three classes: Encoder, Decoder and AutoEncoder.
Since I don't want to manually set input shapes I'm trying to infer the output shape of ...
8
votes
2
answers
5k
views
Does tf.keras.metrics.AUC work on multi-class problems?
I have a multi-class classification problem and I want to measure AUC on training and test data.
tf.keras has implemented AUC metric (tf.keras.metrics.AUC), but I'm not be able to see whether this ...
8
votes
1
answer
2k
views
Saving and loading multiple models with the same graph in TensorFlow Functional API
In the TensorFlow Functional API guide, there's an example shown where multiple models are created using the same graph of layers. (https://www.tensorflow.org/beta/guide/keras/functional#...
8
votes
0
answers
788
views
Call Model.fit with tensors / Executing op on CPU not GPU / Tensorflow 2.1
I am experimenting with reinforcement learning in python. I am using Tensorflow 2.1 and my machine has muyliple GPUs (with CUDA 10.2 driver 440.59). I am allocating the operations on my GPUs using tf....
7
votes
1
answer
17k
views
ValueError: No gradients provided for any variable - Tensorflow 2.0/Keras
I am trying to implement a simple sequence-to-sequence model using Keras. However, I keep seeing the following ValueError:
ValueError: No gradients provided for any variable: ['simple_model/...
7
votes
4
answers
3k
views
Keras - no good way to stop and resume training?
After a lot of research, it seems like there is no good way to properly stop and resume training using a Tensorflow 2 / Keras model. This is true whether you are using model.fit() or using a custom ...
7
votes
1
answer
1k
views
Redundancies in tf.keras.backend and tensorflow libraries
I have been working in TensorFlow for about a year now, and I am transitioning from TF 1.x to TF 2.0, and I am looking for some guidance on how to use the tf.keras.backend library in TF 2.0. I ...
7
votes
1
answer
21k
views
Unable to train my keras model : (Data cardinality is ambiguous:)
I am using the bert-for-tf2 library to do a Multi-Class Classification problem. I created the model but training throws the following error:
-----------------------------------------------------------...
7
votes
1
answer
2k
views
How do sessions and parallelism work in TF2.0?
I am trying to run two tensorflow models in parallel in the same process.
In Tensorflow 1.x we could do e.g. Keras Tensorflow - Exception while predicting from multiple threads
graph = tf.Graph()
...
6
votes
2
answers
17k
views
tf.keras.preprocessing.image_dataset_from_directory Value Error: No images found
belos is my code to ensure that the folder has images, but tf.keras.preprocessing.image_dataset_from_directory returns no images found. What did I do wrong? Thanks.
DATASET_PATH = pathlib.Path('C:\\...
6
votes
6
answers
5k
views
Reload Keras-Tuner Trials from the directory
I'm trying to reload or access the Keras-Tuner Trials after the Tuner's search has completed for inspecting the results. I'm not able to find any documentation or answers related to this issue.
For ...
6
votes
2
answers
7k
views
CancelledError: [_Derived_]RecvAsync is cancelled
I am having an issue. I run the same code on my local machine with CPU and Tensorflow 1.14.0. It works fine. However, when I run it on GPU with Tensorflow 2.0, I get
CancelledError: [_Derived_]...
6
votes
1
answer
7k
views
Why does Keras gives me different results between model.evaluate, model.predicts & model.fit?
I'm working on a project with a resnet50 based dual output model. One output is for the regression task and the second output is for a classification task.
My main question is about the model ...
6
votes
1
answer
540
views
How to trigger a python function inside a tf.keras custom loss function?
Inside my custom loss function I need to call a pure python function passing in the computed TD errors and some indexes. The function doesn't need to return anything or be differentiated. Here's the ...
6
votes
1
answer
6k
views
Tensorflow 2.1.0 - An op outside of the function building code is being passed a "Graph" tensor
I am trying to implement a recent paper. Part of this implementation involves moving from tf 1.14 to tf 2.1.0. The code was working with tf 1.14 but is no longer working.
NOTE: If I disable eager ...
6
votes
1
answer
2k
views
Error with exporting TF2.2.0 model with tf.lookup.StaticHashTable for Serving
I'm using StaticHashTable as in one Lambda layer after the output layer of my tf.keras model. It's quite simple actually: I've a text classification models and I'm adding a simple lambda layer that ...
6
votes
1
answer
464
views
How to save a keras subclassed model with positional parameters in Call() method?
import tensorflow as tf
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
...
6
votes
1
answer
2k
views
Is it possible to integrate Levenberg-Marquardt optimizer from Tensorflow Graphics with a Tensorflow 2.0 model?
I have a Tensorflow 2.0 tf.keras.Sequential model. Now, my technical specification prescribes using the Levenberg-Marquardt optimizer to fit the model. Tensorflow 2.0 doesn't provide it as an ...
6
votes
1
answer
2k
views
When should we inherits keras.Model instead of keras.layers.Layer even if we don't use model.fit?
In some Tensorflow tutorials with tf2 (e.g. Neural Machine Translation with Attention and Eager essentials), they define custom tf.keras.Models instead of tf.keras.layers.Layers (e.g. ...
6
votes
2
answers
3k
views
Is there any implementation of mtcnn face detection in tensorflow 2.0?
Recently I've moved to tensorflow==2.0.0-rc0 and now mtcnn for face detection is not working on my computer. Can I find tensorflow==2.0.0-rc0 version of mtcnn? Pure Keras implementation of mtcnn would ...
5
votes
1
answer
6k
views
What is the numpy equivalent of TensorFlow Xavier initializer for CNN?
I would like to re-create the Xavier initialization in NumPy (using basic functions) in the same way that TensorFlow2 does for CNN.
Here is how I learned to do Xavier initialization in NumPy:
# ...
5
votes
1
answer
932
views
How to create two layers with shared weights, where one is the transpose of the other?
I am using the Keras API in Tensorflow 2.0.
As an example, say I want to have two dense layers in my model, called layer1 and layer2. But I want to tie their weights, such that the weight matrix in ...