All Questions
1,217
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 ...
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
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('...
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
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 ...
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
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 ...
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. ...
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
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
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 ...
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
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
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
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
11k
views
SHAP DeepExplainer with TensorFlow 2.4+ error
I'm trying to compute shap values using DeepExplainer, but I get the following error:
keras is no longer supported, please use tf.keras instead
Even though i'm using tf.keras?
KeyError ...
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
6k
views
Tensorflow Keras RMSE metric returns different results than my own built RMSE loss function
This is a regression problem
My custom RMSE loss:
def root_mean_squared_error_loss(y_true, y_pred):
return tf.keras.backend.sqrt(tf.keras.losses.MSE(y_true, y_pred))
Training code sample, ...
8
votes
2
answers
4k
views
What is the difference between conv1d with kernel_size=1 and dense layer?
I am building a CNN with Conv1D layers, and it trains pretty well. I'm now looking into how to reduce the number of features before feeding it into a Dense layer at the end of the model, so I've been ...
8
votes
3
answers
23k
views
Cannot use keras models on Mac M1 with BigSur
I am trying to use Sequential model from keras of tensorflow. When I am executing following statement:
model.fit(x_train, y_train, epochs=20, verbose=True, validation_data=(x_dev, y_dev), batch_size=...
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
3k
views
Should I use the standalone Keras library or tf.keras?
As Keras becomes an API for TensorFlow, there are lots of old versions of Keras code, such as https://github.com/keiserlab/keras-neural-graph-fingerprint/blob/master/examples.py
from keras import ...
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
1
answer
744
views
TF2.1: SegNet model architecture problem. Bug with metric calculation, keeps constant and converge to determined value
I'm building a custom model (SegNet) in Tensorflow 2.1.0.
The first problem I'm facing is the reutilization of the indices of the max pooling operation needed as described in the paper.
Basically, ...
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
4
answers
7k
views
The clear_session() method of keras.backend does not clean up the fitting data
I am working on a comparison of the fitting accuracy results for the different types of data quality. A "good data" is the data without any NA in the feature values. A "bad data" is the data with NA ...
7
votes
1
answer
8k
views
What is the difference between input_shape and input_dim in keras?
Currently i am learning deep learning and stumbled upon these confusion:
when to use input_shape and input_dim parameter.
shape of my data is (798,9) and these has 8 input variable and 1 output ...
7
votes
2
answers
7k
views
Keras callback AttributeError: 'ModelCheckpoint' object has no attribute '_implements_train_batch_hooks'
I'm using Keras (with TensorFlow back-end) to implement a neural network and want to only save the model that minimises loss on the validation set during training. To do this, I instantiated a ...
7
votes
6
answers
2k
views
pipenv - Pipfile.lock is not being generated due to the 'Could not find a version that matches keras-nightly~=2.5.0.dev' error
As the title clearly describes the issue I've been experiencing, no Pipfile.lock is being generated as I get the following error when I execute the recommended command pipenv lock --clear:
ERROR: ...
7
votes
2
answers
1k
views
In Keras, is there any function similar to the zero_grad() in Pytorch?
In Pytorch, we can call zero_grad() to clear the gradients. In Keras, do we have a similar function so that we can achieve the same thing? For example, I want to accumulate gradients among some ...