All Questions
Tagged with tf.keras tensorflow
1,495
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 ...
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 ...
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
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
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. ...
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
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 ...
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
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
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 ...
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
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
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
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....