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
Filter by
Sorted by
Tagged with
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 ...
Piotr Laczkowski's user avatar
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: ...
daknowles's user avatar
  • 1,419
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, ...
Florida Man's user avatar
  • 2,091
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 ...
Imperator123's user avatar
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 ...
Fady's user avatar
  • 87
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): ... ...
some_name.py's user avatar
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 ...
Florida Man's user avatar
  • 2,091
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 ...
Kaylee's user avatar
  • 41
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 ...
figs_and_nuts's user avatar
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 ...
Nagabhushan S N's user avatar
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 ...
piccolo's user avatar
  • 2,157
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 ...
maarnaa's user avatar
  • 53
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]]) ...
drum_stick's user avatar
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 ...
Manav Mishra's user avatar
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/...
Jay ra1's user avatar
  • 71
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 ...
learner's user avatar
  • 909
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 ...
jul's user avatar
  • 36.9k
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 ...
Rushil Desai's user avatar
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 ...
OverLordGoldDragon's user avatar
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, ...
jul's user avatar
  • 36.9k
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 ...
PHAN's user avatar
  • 303
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, ...
Yana's user avatar
  • 887
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 ...
froy001's user avatar
  • 624
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....
RobR's user avatar
  • 2,160
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: ...
eilalan's user avatar
  • 679
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): ...
HuckleberryFinn's user avatar
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 ...
user3827247's user avatar
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='...
Allstreamer_'s user avatar
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 ...
Nagabhushan S N's user avatar
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): ...
loadbox's user avatar
  • 646
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 ...
Adomas Baliuka's user avatar
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....
Bdawg N's user avatar
  • 41
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 = ...
daria's user avatar
  • 101
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 ...
Hamid Heydarian's user avatar
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 ...
Ronan Venkat's user avatar
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)...
EthanJiang's user avatar
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 ...
learner's user avatar
  • 909
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 ...
Mike's user avatar
  • 593
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 ...
wmVdNdsPZ's user avatar
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 ...
Stack's user avatar
  • 1,078
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) ...
Avi's user avatar
  • 41
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 ...
figs_and_nuts's user avatar
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 ...
M. Ungheretti's user avatar
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 ...
benvigano's user avatar
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 ...
mickey's user avatar
  • 485
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....
tidy's user avatar
  • 4,857
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 ...
Mastiff's user avatar
  • 2,170
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 ...
Jonn Dove's user avatar
  • 487
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 ...
Negar Erfanian's user avatar
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. ...
ai2ys's user avatar
  • 1,371

1 2 3
4
5
44