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,185
questions
4
votes
3
answers
3k
views
Tensorflow 2 -Keras SAVE/LOAD model error (DenseFeatures and DistributionLambda layers)
I have a Tensorflow 2.x model which is using the TF preprocessing layer (tf.keras.layers.DenseFeatures) and the distributional layer from TF probability (DistributionLambda):
def ...
4
votes
2
answers
4k
views
"Could not compute output" error using tf.keras merge layers in Tensorflow 2
I'm trying to use a merge layer in tf.keras but getting AssertionError: Could not compute output Tensor("concatenate_3/Identity:0", shape=(None, 10, 8), dtype=float32). Minimal (not)working example:
...
4
votes
2
answers
7k
views
Using flow_from_dataframe what is the correct "value" for y_col
I am reading in a csv file with pandas, and give the column names stored in colname
colnames=['file', 'label']
# Read data from file
data = pd.read_csv('./Hand_Annotations_2.csv',names=colnames, ...
4
votes
2
answers
2k
views
How to stack Convolutional Layer and LSTM using Tensorflow2.0 alpha?
I am trying to implement a neural network for an NLP task with a convolutional layer followed up by an LSTM layer. I am currently experimenting with the new Tensorflow 2.0 to do this. However, when ...
4
votes
1
answer
2k
views
Tensorflow saving subclass model which has multiple arguments to call() method
I am following the tensorflow neural machine translation tutorial:
https://www.tensorflow.org/tutorials/text/nmt_with_attention
I am trying to save the Encoder and Decoder models which are subclasses ...
4
votes
1
answer
722
views
Set .trainable variable in GAN implementation in tf.keras properly
I am confused with the .trainable statement of tf.keras.model in the implementation of a GAN.
Given following code snipped (taken from this repo):
class GAN():
def __init__(self):
...
...
4
votes
2
answers
10k
views
MobileNetV2 in tf.keras. Many links but no useful information
I want to use mobileNetV2 with tf.keras.
If look on the tensorflow website for keras applications
I find
mobilenet = tf.keras.applications.MobileNetV2()
If I try to import MobileNetV2
from ...
4
votes
1
answer
6k
views
CustomMaskWarning when using Keras OxfordPets code
I'm on a project that is taking the Oxford Pets code https://keras.io/examples/vision/oxford_pets_image_segmentation/ and modifying it various ways. We're getting the following warning (when running ...
4
votes
1
answer
4k
views
Why is input_length needed in layers.Embedding in keras tensorflow?
layers.embedding has a parameter (input_length) that the documentation describes as:
input_length : Length of input sequences, when it is constant. This argument is required if you are going to ...
4
votes
1
answer
2k
views
Print all terms of loss function tensorflow 2.0
I'm defining a custom loss function. For eg, let's take the loss function = L1 loss + L2 loss.
When I do model.fit_generator(), the overall loss function is printed after every batch. But I want to ...
4
votes
1
answer
2k
views
Tensorflow2.0 training: model.compile vs GradientTape
I am starting to learn Tensorflow2.0 and one major source of my confusion is when to use the keras-like model.compile vs tf.GradientTape to train a model.
On the Tensorflow2.0 tutorial for MNIST ...
4
votes
1
answer
2k
views
Remove top layer from pre-trained model, transfer learning, tensorflow (load_model)
I have pre-trained a model (my own saved model) with two classes, which I want to use for transfer learning to train a model with six classes.
I have loaded the pre-trained model into the new training ...
4
votes
1
answer
1k
views
How to construct a sobel filter for kernel initialization in input layer for images of size 128x128x3?
This is my code for sobel filter:
def init_f(shape, dtype=None):
sobel_x = tf.constant([[-5, -4, 0, 4, 5], [-8, -10, 0, 10, 8], [-10, -20, 0, 20, 10], [-8, -10, 0, 10, 8], [-5, -4, 0, 4, 5]])
...
4
votes
2
answers
4k
views
TypeError: You are attempting to use Python control flow in a layer that was not declared to be dynamic. Pass `dynamic=True` to the class constructor
I am using TensorFlow 2.0.0 and tf.keras to create a model network that takes in n inputs, [x1,x2,x3,x4,x5,...xn], and computes f(x1,x2,x3,x4,x5,...xn).
I have defined my model below as:
def ...
4
votes
1
answer
3k
views
Difference between Tokenizer and TextVectorization layer in tensorflow
New to TensorFlow
I saw couple of small NLP projects where people use the 'tf.keras.preprocessing.Tokenizer' to pre-process their text (link: https://www.tensorflow.org/api_docs/python/tf/keras/...
4
votes
1
answer
7k
views
tf.keras.callbacks.ModelCheckpoint Type Error : Unable to serialize 1.0000000656873453e-05 to JSON
I am creating my custom layers tf.keras model using mobile net pretrained layer. Model training is running fine but when saving the best picked model it is giving an error. Below is the snippet of the ...
4
votes
2
answers
1k
views
How to convert saved model from NCHW to NHWC?
I trained my NCHW model on GPU and saved the best state.
I now want to make the inference on CPU, which apparently only support NHWC (I get an error mentionning that).
Do I have to retrain my model ...
4
votes
2
answers
2k
views
Saving a Tensorflow Keras model (Encoder - Decoder) to SavedModel format
I've written (with help from TF tutorials) an image captioning model which uses an encoder-decoder model with attention.
Now, I want to convert it to TfLite and eventually deploy it in Flutter.
I'm ...
4
votes
1
answer
1k
views
What are _get_hyper and _set_hyper in TensorFlow optimizers?
I see it in __init__ of e.g. Adam optimizer: self._set_hyper('beta_1', beta_1). There are also _get_hyper and _serialize_hyperparameter throughout the code. I don't see these in Keras optimizers - are ...
4
votes
1
answer
1k
views
Why is inference using tf.keras 75x slower than using TFLite?
I run a code making some predictions on audio data using a simple CNN.
When using tf.keras.Model.predict I get an an average execution time of 0.17s, and when I use TF.lite.Interpreter I get 0.002s, ...
4
votes
1
answer
5k
views
Adding custom metric Keras Subclassing API
I'm following the section "Losses and Metrics Based on Model Internals" on chapter 12 of "Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 2nd Edition - Aurélien Geron", in which he ...
4
votes
1
answer
2k
views
ValueError: Operands could not be broadcast together with shapes (54, 54, 128) (54, 54, 64)
I wrote a ResNet block with three convolutional layers:
def res_net_block(input_data, filters, kernel_size):
kernel_middle = kernel_size + 2
filters_last_layer = filters * 2
x = Conv2D(filters, ...
4
votes
1
answer
2k
views
Memory leak when running universal-sentence-encoder-large itterating on dataframe
I have 140K sentences I want to get embeddings for. I am using TF_HUB Universal Sentence Encoder and am iterating over the sentences(I know it's not the best way but when I try to feed over 500 ...
4
votes
2
answers
3k
views
saved_model.prune() in TF2.0
I am trying to prune nodes of a SavedModel that was generated with tf.keras. The pruning script is as follows:
svmod = tf.saved_model.load(fn) #version 1
#svmod = tfk.experimental....
4
votes
1
answer
985
views
Tensorflow model analysis, TFMA for keras model
I would like to use TFMA with keras model. The keras model was created with TF 2.0 alpha.
The model is a pretrained model with a classification layer:
...
4
votes
1
answer
3k
views
Tensorflow 2.0 Keras Model subclassing
I'm trying to implement a simple UNet-like model using the model subclassing method. Here's my code:
import tensorflow as tf
from tensorflow import keras as K
class Enc_block(K.layers.Layer):
...
4
votes
2
answers
7k
views
AttributeError: 'Sequential' object has no attribute 'total_loss'
I've been tearing my hair out trying to figure this out for days. I'm using tensorflow-gpu v1.13.1 and I could only find 2 other threads even mentioning a similar error.
Recreated error:
import ...
4
votes
1
answer
10k
views
Keras Graph disconnected cannot obtain value for tensor KerasTensor
Tensorflow: 2.4.0
This is the Full Error Message:
ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 64, 64, 3), dtype=tf.float32, name='...
4
votes
1
answer
803
views
ResNet model in keras and tf.keras give different output for the same image
For a given image, I'm extracting ResNet features i.e. after all conv layers and global max pooling, which gives a 2048 length vector per image.
Earlier, I was using keras==2.3.1 with backend ...
4
votes
1
answer
2k
views
How to use a saved tensorflow model by `saved_model.simple_save`?
The model was saved using this function:
import tensorflow as tf
from tensorflow.python.estimator.export import export as export_helpers
import keras.backend as K
def save_for_serving(self):
...
4
votes
2
answers
6k
views
Tensorflow performance (versions 1 vs 2 and CPU vs GPU)
I'm new to Machine Learning and found myself spending a disproportionate amount of time setting up Tensorflow. I use Anaconda to manage the different versions as environments. I managed to install
...
4
votes
1
answer
3k
views
Is automatic mixed precision supported by tf.keras in Tensorflow Beta 2.0?
I am trying to get Tensorflow's automatic mixed precision working (to use the tensor cores on an RTX 2080 Ti), using the tf.keras API, but I can't see any speed-up in training.
I have just added
os....
4
votes
1
answer
3k
views
add LSTM/GRU to BERT embeddings in keras tensorflow
I am experimenting with BERT embeddings following this code https://github.com/strongio/keras-bert/blob/master/keras-bert.py
These are the important bits of the code (lines 265-267):
bert_output = ...
4
votes
2
answers
1k
views
expected input_1 to have 3 dimensions, but got array with shape (3, 4)
This is a simplified version of my code which throws the error mentioned in the title:
import tensorflow as tf
BATCH_SIZE = 3
SEQ_LENGTH = 4
NUM_CLASSES = 2
LSTM_UNITS = 64
NUM_SHARDS = 4
...
4
votes
2
answers
5k
views
tf.keras loss becomes NaN
I'm programming a neural network in tf.keras, with 3 layers. My dataset is the MNIST dataset. I decreased the number of examples in the dataset, so the runtime is lower. This is my code:
import ...
4
votes
1
answer
3k
views
tf.keras.layers.MultiHeadAttention's argument key_dim sometimes not matches to paper's example
For example, I have input with shape (1, 1000, 10) (so, src.shape wil be (1, 1000, 10), which means the sequence length is 1000, and the dimension is 10. Then:
This works (random num_head and key_dim)...
4
votes
1
answer
515
views
Saving best metrics based on Custom metrics failing (WARNING:tensorflow:Can save best model only with CUSTOM METRICS available, skipping)
I have defined a callback that runs on the epoch end and calculate the metrics. It is working fine in terms of calculating the desired metrics. Below is the function for reference
callback to find ...
4
votes
2
answers
1k
views
Getting keras compiled model loss function
Is there any way to get the compiled model loss/metrics functions from the model object?
I need this to access the loss function from within a callback, where I have access to self.model, but I can't ...
4
votes
1
answer
1k
views
Keras: custom data validation callback on training data always returns validation data results
I am working on an autoencoder in Keras that contains some dropout layers. To evaluate bias and variance, I'd like to compare the losses of training and test data. However, since dropout is used ...
4
votes
1
answer
2k
views
Error when Loading a .pb Tensorflow Model
I created a tensorflow model on a Windows 10 machine and saved it using:
model.save('myfolder')
Inside the folder myfolder I get:
- saved_model.pb
- Variables folder
- Assets folder
Now I'm trying ...
4
votes
1
answer
3k
views
Tensorflow JS tfjs | Unable to load model using tf.loadLayersModel
While executing the following code embedded in html using WAMP stack
const model = tf.loadLayersModel('js/model.json');
I encounter the following errors in chrome
> Uncaught (in promise) ...
4
votes
1
answer
4k
views
Unable to understand the behavior of method `build` in tensorflow keras layers (tf.keras.layers.Layer)
Layers in tensorflow keras have a method build that is used to defer the weights creation to a time when you have seen what the input is going to be. a layer's build method
I have a few questions i ...
4
votes
1
answer
2k
views
Validation loss is zero on first epoch only
Problem
I am trying to build a regression model in tensorflow using the dataset and keras API's. The target contains quite a lot of zero's and the non-zero values are roughly distributed normally ...
4
votes
2
answers
1k
views
Training multiple models in series in Keras for hyperparameter optimization
The idea is to train multiple models using the same training dataset, changing some parameters each time in order to see which parameter works best. In order to do so I would need every model to be ...
4
votes
1
answer
1k
views
Compatibility between keras and tf.keras models
I am interested in training a model in tf.keras and then loading it with keras. I know this is not highly-advised, but I am interested in using tf.keras to train the model because
tf.keras is easier ...
4
votes
1
answer
2k
views
what is the corresponding function of K.gradients for tensorflow 2.0?
I want to visualize the classification result with tensorflow2.0. For keras, it need the following code for cam:
import tensorflow as tf
import keras.backend as K
from tensorflow.keras.applications....
4
votes
1
answer
195
views
Can I change the statefulness of RNN after training?
If I build and train my RNN based model with stateful=False, can I simply do (e.g.):
model.layers[0].stateful = True
And have it take effect as might be expected for use in prediction? I ask ...
4
votes
2
answers
1k
views
What does Keras do with the initial values of cell & hidden states (RNN, LSTM) for inference?
Assuming training is finished: what values does Keras use for the 0th cell state and hidden states at inference (in LSTM and RNN layers)? I could think of at least three scenarios, and could not find ...
4
votes
0
answers
2k
views
TypeError: Cannot interpret '<KerasTensor: shape=(None, 3) dtype=float32 (created by layer 'input_1')>' as a data type
Very simple question. I am using tensorflow probability package to use a bijector to form a trainable_distribution from a simple distribution (let's say gaussian)
Everything works properly in jupyter ...
4
votes
0
answers
649
views
Fine tuning a model - base_model Dropout in inference or training mode?
In the TensorFlow documentation it is highlighted that it is important during fine tuning to set the base_model to ’inference mode’ setting the parameter training = False when calling the base_model. ...