Questions tagged [tf.keras]
[tf.keras] is TensorFlow's implementation of the Keras API specification. Use the tag for questions specific to this TensorFlow module. You might also add the tag [keras] to your question since it has the same API.
tf.keras
2,189
questions
81
votes
9
answers
251k
views
How to import keras from tf.keras in Tensorflow?
import tensorflow as tf
import tensorflow
from tensorflow import keras
from keras.layers import Dense
I am getting the below error
from keras.layers import Input, Dense
Traceback (most recent call ...
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 ...
59
votes
20
answers
131k
views
How to fix "AttributeError: module 'tensorflow' has no attribute 'get_default_graph'"?
I am trying to run some code to create an LSTM model but i get an error:
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
My code is as follows:
from keras.models import ...
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 ...
29
votes
8
answers
23k
views
model.summary() can't print output shape while using subclass model
This is the two methods for creating a keras model, but the output shapes of the summary results of the two methods are different. Obviously, the former prints more information and makes it easier to ...
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 ...
26
votes
2
answers
17k
views
Keras - Validation Loss and Accuracy stuck at 0
I am trying to train a simple 2 layer Fully Connected neural net for Binary Classification in Tensorflow keras. I have split my data into Training and Validation sets with a 80-20 split using sklearn'...
24
votes
4
answers
47k
views
What is meant by sequential model in Keras
I have recently started working Tensorflow for deep learning. I found this statement model = tf.keras.models.Sequential() bit different. I couldn't understand what is actually meant and is there any ...
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....
20
votes
5
answers
28k
views
from_logits=True and from_logits=False get different training result for tf.losses.CategoricalCrossentropy for UNet
I am doing the image semantic segmentation job with unet, if I set the Softmax Activation for last layer like this:
...
conv9 = Conv2D(n_classes, (3,3), padding = 'same')(conv9)
conv10 = (Activation('...
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 ...
17
votes
4
answers
19k
views
What is the difference between keras and tf.keras?
I'm learning TensorFlow and Keras. I'd like to try https://www.amazon.com/Deep-Learning-Python-Francois-Chollet/dp/1617294438/, and it seems to be written in Keras.
Would it be fairly straightforward ...
17
votes
1
answer
4k
views
RNN in Tensorflow vs Keras, depreciation of tf.nn.dynamic_rnn()
My question is: Are the tf.nn.dynamic_rnn and keras.layers.RNN(cell) truly identical as stated in docs?
I am planning on building an RNN, however, it seems that tf.nn.dynamic_rnn is depricated in ...
16
votes
6
answers
49k
views
InvalidArgumentError: required broadcastable shapes at loc(unknown)
Background
I am totally new to Python and to machine learning. I just tried to set up a UNet from code I found on the internet and wanted to adapt it to the case I'm working on bit for bit. When ...
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....
15
votes
1
answer
4k
views
Is it meaningless to use ReduceLROnPlateau with Adam optimizer?
This question is basically for the working of Keras or tf.keras for people who have the verty deep knowledge of the framework
According to my knowledge, tf.keras.optimizers.Adam is an optimizer which ...
14
votes
2
answers
5k
views
Why in Keras subclassing API, the call method is never called and as an alternative the input is passed by calling the object of this class?
When creating a model using Keras subclassing API, we write a custom model class and define a function named call(self, x)(mostly to write the forward pass) which expects an input. However, this ...
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(...
13
votes
7
answers
25k
views
Keras that does not support TensorFlow 2.0. We recommend using `tf.keras`, or alternatively, downgrading to TensorFlow 1.14
I am having an error regarding (Keras that does not support TensorFlow 2.0. We recommend using tf.keras, or alternatively, downgrading to TensorFlow 1.14.) any recommendations.
thanks
import keras
...
13
votes
8
answers
14k
views
"UserWarning: An input could not be retrieved. It could be because a worker has died. We do not have any information on the lost sample."
While training model I got this warning "UserWarning: An input could not be retrieved. It could be because a worker has died.We do not have any information on the lost sample.)", after showing this ...
13
votes
1
answer
656
views
Most scalable way for using generators with tf.data ? tf.data guide says `from_generator` has limited scalability
tf.data has a from_generator initializer, it doesn't seem like it's scalable. From the official guide
Caution: While this is a convienient approach it has limited
portability and scalibility. It ...
13
votes
1
answer
2k
views
Why does tf.keras model.fit() initialize take so long? How can it be optimized?
Using tensorflow.keras (2.0-alpha0 with GPU support) I have extremely long initialize times with tf.keras.model.fit() on both newly compiled models and models previously saved and reloaded.
I ...
12
votes
1
answer
12k
views
How to use sample weights with tensorflow datasets?
I have been training a unet model for multiclass semantic segmentation in python using Tensorflow and Tensorflow Datasets.
I've noticed that one of my classes seems to be underrepresented in training. ...
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
5
answers
27k
views
TypeError: Tensor is unhashable. Instead, use tensor.ref() as the key. in Keras Surgeon
I'm using Kerassurgeon module for pruning.I encountered this error while i'm working with VGG-16 in google colab.It works fine for other models.Can someone help me fix this.
---> 17 model_new = ...
11
votes
1
answer
25k
views
How to save a list of numpy arrays into a single file and load file back to original form [duplicate]
I am currently trying to save a list of numpy arrays into a single file, an example of such a list can be of the form below
import numpy as np
np_list = []
for i in range(10):
if i % 2 == 0:
...
11
votes
3
answers
37k
views
Tensorflow==2.0.0a0 - AttributeError: module 'tensorflow' has no attribute 'global_variables_initializer'
I'm using Tensorflow==2.0.0a0 and want to run the following script:
import tensorflow as tf
import tensorboard
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import ...
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
1k
views
How do I save and load BatchNormalization Layer in this Tensorflow model?
I am trying to save a model and then load it later to make some predictions; what happens is that the accuracy of the model after training is 95%+, but when I save it and then load it, the accuracy ...
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
1
answer
30k
views
ImportError: cannot import name 'keras_tensor' from 'tensorflow.python.keras.engine'
I'm getting this error while loading the tensorflow addons library
import tensorflow_addons as tfa
ImportError: cannot import name 'keras_tensor' from 'tensorflow.python.keras.engine'
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
974
views
Convert a KerasTensor object to a numpy array to visualize predictions in Callback
I am writing a custom on_train_end callback function for model.fit() method of tensorflow keras sequential model. The callback function is about plotting the predictions that the model makes, so 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
16k
views
Decay parameter of Adam optimizer in Keras
I think that Adam optimizer is designed such that it automtically adjusts the learning rate.
But there is an option to explicitly mention the decay in the Adam parameter options in Keras.
I want to ...
9
votes
4
answers
5k
views
Saving meta data/information in Keras model
Is it possible to save meta data/meta information in Keras model? My goal is to save input pre-processing parameters, train/test set used, class label maps etc. which I can use while loading model ...
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
7k
views
Cannot clone object <tensorflow.python.keras.wrappers.scikit_learn.KerasClassifier object
This is with regards to TF 2.0.
Please find below my code that performs GridSearch along with Cross Validation using sklearn.model_selection.GridSearchCV for the mnist dataset that works perfectly ...
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
7k
views
How does Tensorflow calculate the accuracy of model?
I am following this tutorial for binary class classification. While defining the model it is defined as follows and quotes:
Apply a tf.keras.layers.Dense layer to convert these features into a ...
9
votes
1
answer
7k
views
Tensorflow: Modern way to load large data
I want to train a convolutional neural network (using tf.keras from Tensorflow version 1.13) using numpy arrays as input data. The training data (which I currently store in a single >30GB '.npz' ...
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
...
9
votes
1
answer
3k
views
keras fit_generator: 'zip' object has no attribute 'shape'
as in keras documentation in the (image, mask) data generator, I created data generator to have (input, output) tuple images.
When running locally on my laptop (with tensorflow 1.13.1), it runs with ...
8
votes
5
answers
9k
views
How do I calculate the matthews correlation coefficient in tensorflow
So I made a model with tensorflow keras and it seems to work ok. However, my supervisor said it would be useful to calculate the Matthews correlation coefficient, as well as the accuracy and loss it ...