Question:
Hi all,
I have a pandas DataFrame with features as columns and rows as observations. One of the columns is a Series where each element is a 512-long tf.Tensor. I am trying to pass this Tensor vector, along with the other features, into a tf.estimator.BoostedTreesClassifier model. However, I am receiving the following error when passing the tf.Tensor column:
AttributeError: Tensor.name is meaningless when eager execution is enabled.

Your help is much appreciated! Below is a reproducible example. Many thanks in advance for your help!
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
df = pd.DataFrame({"Text": ['This is text one', 'This is text two', 'And well, this is just the third text']})
model_url = "https://tfhub.dev/google/universal-sentence-encoder/4"
encodings = tf.keras.Sequential(
    [
        tf.keras.layers.InputLayer(dtype=tf.string),
        hub.KerasLayer(model_url, input_shape=[], dtype=tf.string),
    ]
)
def encodes_text(txt):
    return encodings(tf.constant([txt]))
df['embeddings'] = df.map(lambda x: encodes_text(x))
tree_class = tf.estimator.BoostedTreesClassifier(
    df.embedding,
    max_depth=3,
    n_classes=2,
    n_trees,50,
    n_batches_per_layer=1
)

Context:
If you’re just getting started on this project my advice is don’t use anything in tf.estimator. Use TensorFlow Decision Forests 3 which takes advantage of modern APIs.
If you’re going to ignore that advice, and do it with tf.estimator anyway, then the fix is to note that first argument isn’t meant to be the data. It’s meant to be a list of tf.feature_column objects that describe how the model should process the data.
See:

tf.estimator.BoostedTreesClassifier  |  TensorFlow Core v2.5.0 2
Module: tf.feature_column  |  TensorFlow Core v2.5.0 3


Question:
Hello all: this is my first post and I am happy to share some cool stuff for you.
We have a YouTube channel built/maintained by Machine Learning GDEs.
Feel free to check it out and subscribe at

      YouTube

ML GDEs 3

  Machine Learning (ML) Google Developers Experts (GDEs) are a global network of experts who are passionate about helping developers about ML. This channel 1) includes video content uploaded by ML GDEs, and 2) features talks by GDEs from other...

Thank you!

Context:
@Soonson_Kwon massive share! Thank you.
Just a small nit. If you put the links in separate lines, the forum will generate nice previews. For this post I have taken care of it.


Question:
I have to define a custom F1 metric in keras for a multiclass classification problem. Since it is a streaming metric the idea is to keep track of the true positives, false negative and false positives so as to gradually update the f1 score batch after batch. Here’s the code:
data = load_iris()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
def compute_confusion_matrix(true, pred, K):
    result = tf.zeros((K, K), dtype=tf.int32)
    for i in range(len(true)):
        result = tf.tensor_scatter_nd_add(tensor = result, indices=tf.constant([[true[i], pred[i]]]),
                                 updates=tf.constant([1]))
    return result

def f1_function(y_true, y_pred):
    k = 3
    y_pred_lab = np.argmax(y_pred, axis=1)
    y_true = np.ravel(y_true)
    conf_mat= compute_confusion_matrix(y_true, y_pred_lab, K = k)
    tp = tf.linalg.tensor_diag_part(conf_mat)
    fp = tf.reduce_sum(conf_mat, axis = 0) - tp
    fn = tf.reduce_sum(conf_mat, axis = 1) - tp
    support = tf.reduce_sum(conf_mat, axis = 1)
    return tp, fp, fn, support

class F1Metric(keras.metrics.Metric):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.f1_fn = f1_function
        self.tp_count = self.add_weight("tp_count", initializer="zeros", shape = (3,), dtype=tf.float32)
        self.fp_count = self.add_weight("fp_count", initializer="zeros", shape = (3,), dtype=tf.float32)
        self.fn_count = self.add_weight("fn_count", initializer="zeros", shape = (3,), dtype=tf.float32)
        self.support_total = self.add_weight("support_total", initializer = "zeros", shape = (3,),
                                             dtype=tf.float32)

    def update_state(self, y_true, y_pred, sample_weight=None):
        tp, fp, fn, support = self.f1_fn(y_true, y_pred)
        print(tp)
        print(self.tp_count)
        self.tp_count.assign_add(tf.cast(tp, dtype=tf.float32))
        self.fp_count.assign_add(tf.cast(fp, dtype=tf.float32))
        self.fn_count.assign_add(tf.cast(fn, dtype=tf.float32))
        self.support_total.assign_add(tf.cast(support, dtype=tf.float32))

    def result(self):
        precisions = self.tp_count / (self.tp_count + self.fp_count)
        recalls = self.tp_count / (self.tp_count + self.fn_count)
        f1 = tf.constant(2, dtype=tf.float32) * (precisions*recalls) / (precisions + recalls)
        weighted_f1 = (f1 * self.support_total) / tf.reduce_sum(tf.cast(self.support_total, dtype=tf.float32))
        return  recalls

model = keras.models.Sequential([
    keras.layers.Dense(200, activation = "relu", input_shape = X_train.shape[1:]),
    keras.layers.Dense(4, activation = "softmax")
])

early_stopping_cb = keras.callbacks.EarlyStopping(patience=10,
                                              restore_best_weights=True)

#compile the model
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=[F1Metric()],
             run_eagerly=True)

#fit the model
history = model.fit(X_train, y_train, epochs = 100,
                   validation_split=0.1,
                    callbacks = [early_stopping_cb],
                   )

It gives the following error:
“Cannot assign to variable tp_count:0 due to variable shape (3,) and value shape () are incompatible”
Alternatively, I tried to use the tfa F1 metric but I can’t use it in a grid search (indeed I want to find the optimal model architecture and I want to use the f1 metric as the scorer) since it gives the following error:
“ValueError: The list/tuple elements must be unique strings of predefined scorers.  One or more of the elements were callables. Use a dict of score name mapped to the scorer callable. Got [<tensorflow_addons.metrics.f_scores.F1Score object at 0x7f8ac9516be0>]”
Any idea? Thank you

Context:
Did you try using the version from TensorFlow Addons?

      TensorFlow

tfa.metrics.F1Score  |  TensorFlow Addons 5

  Computes F-1 Score.


What does the run_eagerly parameter in model.compile do?

Question:
From the documentation 8:

Bool. Defaults to False . If True , this Model 's logic will not be wrapped in a tf.function 8. Recommended to leave this as None unless your Model cannot be run inside a tf.function 8. run_eagerly=True is not supported when using tf.distribute.experimental.ParameterServerStrategy 7.

What is the significance of being wrapped in tf.function and what are the practical advantage/disadvantages of setting run_eagerly = True?

Context:
When something is wrapped inside tf.function it has the advantage of being run in graph mode. All the backend compilation engineering is handled by TensorFlow itself in this case. The advantage is when all the operations are available as a graph we know much resources to allocate and how to best optimize the graph with the available resources. For more details refer to the following:

      TensorFlow

Better performance with tf.function  |  TensorFlow Core 17

run_eagerly=True lets figure out what exactly is going inside your model training loop. Let’s say you have implemented a custom loop and put that inside the train_step() method of a subclasses model. Setting run_eagerly to True will help you debug that loop if anything goes wrong. For practical applications of this, refer to the following guide:

      keras.io

Keras documentation: Keras debugging tips 19


Question:
I am unable to download ANY tfds dataset.
In windows 10 pro, VScode, python 3.8.8 (also 3.7), TF 2.4.1 (also 2.3 and 2.1)
I get this error:
Failed to rename: d:/data/tensorflow/mnist\1.0.0.incomplete7247AV to: d:/data/tensorflow/mnist\1.0.0 : Access is denied.
From this line of code:
(ds_train, ds_test), ds_info =  tfds.load(    “mnist”,            split=[“train”, “test”],    shuffle_files=True,    data_dir=‘d:/data/tensorflow/’,    as_supervised=True,
with_info=True,)
Full permissions are assigned to Everyone.  Notice how the slashes change from ‘/’ to ‘’.   Is there anyone using Windows that is successfully  downloading the tfds datasets?

Context: I think the issue may be because of something else and not of any OS. Can you please try to uninstall and reinstall tfds as mentioned here 5


Question:
Is there a way to run bazel test in a git checkout without compiling TensoFflow?
E.g. just using an installed TensorFlow wheel with pip install tensorflow.

Context: Copy test file out of tensorflow folder and run it.


Error running .pb model file converted into .tflite file

Question:
Hi Sayak,
I have running .pb model file converted into .tflite file but my model was showing error this
" KeyError: “The name ‘TfPoseEstimator/split:0’ refers to a Tensor which does not exist. The operation, ‘TfPoseEstimator/split’, does not exist in the graph.”
Can you please help me out how to solve it.
I have using tflite_convert to convert .pb to .tflite file.
Even input tensor name ‘split’ exist in the model graph.

Context:
Hello Sayak,
Please suggest me what to do now ?


How to convert a vector to a toeptliz matrix in tensorflow 2.5?

Question:
Given a vector = [1, 2, 3, 4, 5,6]
Is there a function which can convert it in to a Toeptliz matrix  of 4 columns as given below ?
[ [1,2,4,5],[2,4,5,6], [3, 4,5,6]
I want to apply this transform to a batch of vectors.

Context: I think tf.linalg.LinearOperatorToeplitz 6 is what you’re looking for


Converting tensorflow model and checkpoint to onnx

Question:
I am trying to convert a pretrained model (Efficientnet) which I have trained on some custom images and new labels. But when using tf2onnx to convert it to onnx format it requires a checkpoint.meta file? But I can’t see this file anywhere? I only see a .index and .data file from the model when I have trained it.
How can I convert a custom model which is using transfer learning? I have downloaded the model from Tensorflow Model Zoo.
Thanks for any help!

Context: You can try to export checkpoint file to .pb with OD api exporters then use tf2onnx. But i dont know if it works with efficientnets. Script is in models\research\object_detection\exporter_main_v2.py


[HELP!] i can set seed on google colab + tf.Keras

Question:
I tried use the FAQ:

      keras.io

Keras documentation: Keras FAQ 3

doing this way:
import numpy                    as np
import tensorflow               as tf
import os
import random as rn
os.environ[‘PYTHONHASHSEED’] = ‘0’
np.random.seed(123)
rn.seed(123)
tf.random.set_seed(1234)
but sinply dont work
i using to make a RNN LSTM
pls Help!

Context:
This is likely because of the non-deterministic CUDA kernels being fired at the backend. You can use the tensorflow-determinism tool from NVIDIA to fix this.
Here’s a guide 29 that takes a deep dive into good reproducibility practices in TensorFlow.


How to do Minority class sampling using tensorflow?

Question:
Hi, I’m new to this field and I’m trying to do minority class sampling.
I have about 754975 cropped CT images, the size of each one is 19 * 19 * 19, saved as .npy on my local disk.
The truth table is saved as .csv, with the state of each image non-nodule or nodule  (0,1), the data is imbalanced with 1186 image = 1 and the total rest is  = 0.
I need to do minority class sampling as follow :
2000 images for validating set ( 700 nodule, 1300 non-nodule).
752975 images for training set ( 486 nodule, 752489 non-nodule).
I tried to do it using the following code, but the problem was the allocating memory exceeds my PC memory (32 gb)
nodules_path = "~/cropped_nodules/"
nodules_csv = pandas.read_csv("~/cropped_nodules_2.csv")

positive = 0
negative = 0
x_val = []
x_train = []
y_train = []
y_val = []

for nodule in nodules_csv.iterrows():
    if nodule.state == 1 and positive <= 700 and len(x_val) <= 2000 :
        positive += 1
        x_val_img = str(nodule.SN) + ".npy"
        x_val.append(np.load(os.path.join(nodules_path,x_val_img)))
        y_val.append(nodule.state)

    elif nodule.state == 0 and negative <= 1300 and len(x_val) <= 2000:
        x_val_img = str(nodule.SN) + ".npy"
        negative += 1
        x_val.append(np.load(os.path.join(nodules_path,x_val_img)))
        y_val.append(nodule.state)

    else:

        if len(x_train) % 10000 == 0:
            gc.collect()
            print("gc done")
        x_train_img = str(nodule.SN) + ".npy"
        x_train.append(np.load(os.path.join(nodules_path,x_train_img)))
        y_train.append(nodule.state)
        print("x_train len= ", len(x_train))
        print("Size of list1: " + str(sys.getsizeof(x_train)) + "bytes")

I tried to do many things to stop filling the momery, but I think the solution is not load the whole data to the memory at all, and I should find another method.
This post in stackoverflow summeraize my problem and my attempts to solve the memory problem.

      stackoverflow.com

  pd.iterrows() consume all the memory and gives an error (Process finished with exit code 137 (interrupted by signal 9: SIGKILL)) 1

  python, pandas, numpy

  asked by

    Mustafa Mahmood

  on 07:02PM - 25 Apr 21 UTC

I couldn’t figure out how to proparly load it using tensorflow dataset, or any other method.
I know the data is really imbalanced, I’ll try to do many things to overcome the imbalance
(Minority class sampling, data augmentation, minority oversampling, and weighted loss like binary cross entropy loss).
Any help will be appreciated, thanks in advance.

Context:
See if this works for you:

      TensorFlow

tf.data: Build TensorFlow input pipelines  |  TensorFlow Core 4

Earlier this summer I implemented a stratified sampler with tf.data that you could refer to as well:

      github.com

    sayakpaul/PAWS-TF/blob/main/utils/labeled_loader.py 2

    from . import multicrop_loader, config
import tensorflow as tf
import numpy as np
import os

GLOBAL_SCALE = [0.75, 1.0]
AUTO = tf.data.AUTOTUNE
(X_TRAIN, Y_TRAIN), (_, _) = tf.keras.datasets.cifar10.load_data()

def onehot_encode(labels, label_smoothing=0.1):
    """
    One-hot encode labels with label smoothing.

    :param labels: (batch_size, )
    return: one-hot encoded labels with optional label smoothing
    """
    labels = tf.one_hot(labels, depth=10)
    # Reference: https://t.ly/CSYO)
    labels *= 1.0 - label_smoothing

  This file has been truncated. show original

The script is a bit involved so please feel free to ask questions as needed.


Spurious metric tracking in ModelCheckpoint

Question:
Closely related to:
https://discuss.tensorflow.org/t/model-checkpointing-best-practices-when-using-train-step
When I am using this the callback is unable to keep track of the metric values correctly:

image2336×460 58.6 KB

I notice something similar in the SimSiam example I linked in my previous post as well. Am I missing out on?

Context:
Are these metrics outputs both before or after the weight update step?
Cause if not these printed values are not the same


Which version of tensorflow have the function to use class_weight

Question:
Dear everyone:
I want to know which version of tensorflow have the function to use class_weight? that recent versions of tensorflow have restricted the use of class_weight
Thanks
best wishes

Context: What do you mean with “restricted”?


Question:
Dear everyone:
I’m new to tensorflow.  The coding as follows:
def train_model(self):
checkpoint = ModelCheckpoint(self.PATH, monitor=‘val_loss’, verbose=1, save_best_only=True, mode=‘auto’)
if self.modality == “audio”:
model = self.get_audio_model()
model.compile(optimizer=‘adadelta’, loss=‘categorical_crossentropy’, sample_weight_mode=‘temporal’)
elif self.modality == “text”:
model = self.get_text_model()
model.compile(optimizer=‘adadelta’, loss=‘categorical_crossentropy’, sample_weight_mode=‘temporal’)
elif self.modality == “bimodal”:
model = self.get_bimodal_model()
model.compile(optimizer=‘adam’, loss=‘categorical_crossentropy’, sample_weight_mode=‘temporal’)
early_stopping = EarlyStopping(monitor=‘val_loss’, patience=10)
model.fit(self.train_x, self.train_y,
epochs=self.epochs,
batch_size=self.batch_size,
sample_weight=self.train_mask,
class_weight = {0:4.0, 1:15.0, 2:15.0, 3:3.0, 4:1.0, 5:6.0, 6:3.0},
shuffle=True,
callbacks=[early_stopping, checkpoint],
validation_data=(self.val_x, self.val_y, self.val_mask))
self.test_model()
To be honest, the class_weight = {0:4.0, 1:15.0, 2:15.0, 3:3.0, 4:1.0, 5:6.0, 6:3.0} was added by myself to adjust the  class weight. However, it reported the error : ValueError: class_weight not supported for 3+ dimensional targets.
The full of error as follows:
ValueError                                Traceback (most recent call last)
~\baseline.py in
288                 model.test_model()
289         else:
 → 290                 model.train_model()
~\baseline.py in train_model(self)
219
220                 early_stopping = EarlyStopping(monitor=‘val_loss’, patience=10)
 → 221 		model.fit(self.train_x, self.train_y,
222                                 epochs=self.epochs,
223                                 batch_size=self.batch_size,
F:\Anaconda\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1106          training_utils.RespectCompiledTrainableState(self):
1107       # Creates a tf.data.Dataset and handles batch and epoch iteration.
 → 1108       data_handler = data_adapter.get_data_handler(
1109           x=x,
1110           y=y,
F:\Anaconda\lib\site-packages\keras\engine\data_adapter.py in get_data_handler(*args, **kwargs)
1346   if getattr(kwargs[“model”], “_cluster_coordinator”, None):
1347     return _ClusterCoordinatorDataHandler(*args, **kwargs)
 → 1348   return DataHandler(*args, **kwargs)
1349
1350
F:\Anaconda\lib\site-packages\keras\engine\data_adapter.py in init(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution, distribute)
1156     self._insufficient_data = False
1157
 → 1158     self._configure_dataset_and_inferred_steps(strategy, x, steps_per_epoch,
1159                                                class_weight, distribute)
1160
F:\Anaconda\lib\site-packages\keras\engine\data_adapter.py in _configure_dataset_and_inferred_steps(failed resolving arguments)
1168     dataset = self._adapter.get_dataset()
1169     if class_weight:
 → 1170       dataset = dataset.map(_make_class_weight_map_fn(class_weight))
1171     self._inferred_steps = self._infer_steps(steps_per_epoch, dataset)
1172
F:\Anaconda\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in map(self, map_func, num_parallel_calls, deterministic)
1923         warnings.warn(“The deterministic argument has no effect unless the "
1924                       "num_parallel_calls argument is specified.”)
 → 1925       return MapDataset(self, map_func, preserve_cardinality=True)
1926     else:
1927       return ParallelMapDataset(
F:\Anaconda\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in init(self, input_dataset, map_func, use_inter_op_parallelism, preserve_cardinality, use_legacy_function)
4481     self._use_inter_op_parallelism = use_inter_op_parallelism
4482     self._preserve_cardinality = preserve_cardinality
 → 4483     self._map_func = StructuredFunctionWrapper(
4484         map_func,
4485         self._transformation_name(),
F:\Anaconda\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in init(self, func, transformation_name, dataset, input_classes, input_shapes, input_types, input_structure, add_to_graph, use_legacy_function, defun_kwargs)
3710     resource_tracker = tracking.ResourceTracker()
3711     with tracking.resource_tracker_scope(resource_tracker):
 → 3712       self._function = fn_factory()
3713       # There is no graph to add in eager mode.
3714       add_to_graph &= not context.executing_eagerly()
F:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py in get_concrete_function(self, *args, **kwargs)
3132          or tf.Tensor or tf.TensorSpec.
3133     “”"
 → 3134     graph_function = self._get_concrete_function_garbage_collected(
3135         *args, **kwargs)
3136     graph_function._garbage_collector.release()  # pylint: disable=protected-access
F:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py in _get_concrete_function_garbage_collected(self, *args, **kwargs)
3098       args, kwargs = None, None
3099     with self._lock:
 → 3100       graph_function, _ = self._maybe_define_function(args, kwargs)
3101       seen_names = set()
3102       captured = object_identity.ObjectIdentitySet(
F:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py in _maybe_define_function(self, args, kwargs)
3442
3443           self._function_cache.missed.add(call_context_key)
 → 3444           graph_function = self._create_graph_function(args, kwargs)
3445           self._function_cache.primary[cache_key] = graph_function
3446
F:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3277     arg_names = base_arg_names + missing_arg_names
3278     graph_function = ConcreteFunction(
 → 3279         func_graph_module.func_graph_from_py_func(
3280             self._name,
3281             self._python_function,
F:\Anaconda\lib\site-packages\tensorflow\python\framework\func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
997         _, original_func = tf_decorator.unwrap(python_func)
998
 → 999       func_outputs = python_func(*func_args, **func_kwargs)
1000
1001       # invariant: func_outputs contains only Tensors, CompositeTensors,
F:\Anaconda\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in wrapped_fn(*args)
3685           attributes=defun_kwargs)
3686       def wrapped_fn(*args):  # pylint: disable=missing-docstring
 → 3687         ret = wrapper_helper(*args)
3688         ret = structure.to_tensor_list(self._output_structure, ret)
3689         return [ops.convert_to_tensor(t) for t in ret]
F:\Anaconda\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in wrapper_helper(*args)
3615       if not _should_unpack(nested_args):
3616         nested_args = (nested_args,)
 → 3617       ret = autograph.tf_convert(self._func, ag_ctx)(*nested_args)
3618       if _should_pack(ret):
3619         ret = tuple(ret)
F:\Anaconda\lib\site-packages\tensorflow\python\autograph\impl\api.py in wrapper(*args, **kwargs)
693       except Exception as e:  # pylint:disable=broad-except
694         if hasattr(e, ‘ag_error_metadata’):
 → 695           raise e.ag_error_metadata.to_exception(e)
696         else:
697           raise
ValueError: in user code:
F:\Anaconda\lib\site-packages\keras\engine\data_adapter.py:1385 _class_weights_map_fn  *
    raise ValueError("`class_weight` not supported for "

ValueError: `class_weight` not supported for 3+ dimensional targets.

How to fix it? Thanks
Best wishes.
jiachen

Context:
The long story is

      github.com/tensorflow/tensorflow

      Adding a utility to penalize majority class pixels in the Segmentation tutorial 55

        opened

        Apr 23, 2021

          closed

        May 4, 2021

          sayakpaul

        stat:awaiting tensorflower
        type:docs-bug
        type:feature

    Thank you for submitting a TensorFlow documentation issue. Per our GitHub
polic…y, we only address code/doc bugs, performance issues, feature requests, and
build/installation issues on GitHub.

The TensorFlow docs are open source! To get involved, read the documentation
contributor guide: https://www.tensorflow.org/community/contribute/docs

## URL(s) with the issue:

Please provide a link to the documentation entry, for example:
https://www.tensorflow.org/tutorials/images/segmentation

## Description of issue (what needs changing):

It's not really an issue, a suggestion rather.

### Clear description

Semantic segmentation datasets can be highly imbalanced meaning that particular class pixels can be present more inside images than that of other classes. Since segmentation problems can be treated as per-pixel classification problems we can deal with the imbalance problem by weighing the loss function to account for this. It's a simple and elegant way to deal with this problem. Other solutions include always ensuring that a batch of samples (during training) always contain some proportion (which is prefixed) of positive classes.

However, TensorFlow does not yet support the `class_weight` argument in `model.fit()` for targets that are 3D (for segmentation problems, we are essentially predicting a map of shape `[batch_size, height, width, nb_channels]`). One way to get around this problem is to use `sample_weight` instead. But then again, it's not very clear as to how to do that properly particularly with `tf.data` pipelines.

Multiple folks have tried several hacks to get around this problem but it keeps coming back (see [here](https://github.com/keras-team/keras/issues/3653)). Therefore, I think the tutorial under question is a perfect opportunity to demonstrate the use case.

Cc: @MarkDaoust

      github.com/keras-team/keras

      Keras - how to use class_weight with 3D data 71

        opened

        Sep 1, 2016

          bsafacicek

    Hi,

I am using Keras to segment images to road and background pixels. As you c…an imagine percentage of road pixels are much lower than that of background pixels. Hence, I want to use class_weight= {0:0.05, 1:0.95} while fitting the model so that cnn won't predict every pixel as background. But, when I do this I got the following error:

  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 597, in fit
    sample_weight=sample_weight)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1035, in fit
    batch_size=batch_size)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 973, in _standardize_user_data
    in zip(y, sample_weights, class_weights, self.sample_weight_modes)]
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 387, in standardize_weights
    raise Exception('class_weight not supported for '
Exception: class_weight not supported for 3+ dimensional targets.

My  training labels are in this form: (number_of_training_samples=10000, number_of_pixels_in_patch=16384, number_of_classes=2). How can I weight the classes in Keras?

Thanks in advance.

/cc @Sayak_Paul @markdaoust


Extracting the current state of an RNN layer

Question: For tf.keras.layers.RNN, there’s a reset_states method that resets the recorded state to some pre-defined input. Is there some way to extract what the current recorded state is?

Context: The documentation mentions a states attribute which looks like it’s readable. There’s also a return_state argument in the declaration if you want to include the state in your graph along with your output.


Difference between Explainable AI , what-if tool and TensorBoard

Question:
-2
All these three seems to be very closely related and are used post model is created and trained.

Explainable AI :- Shows why model has given the prediction that was in the output. It shows you which part of image was more prominent to make a decision or which feature in text is more prominent.
TensorBoard :- provide a holistic view on metrics, loss, fairness, hyperparameter tuning, model performance on machine
What-If Tool:- This combines both explainable AI and TensorBoard techniques but only for text classifications and text regression models.

It is very difficult to judge when to use which tool especially for beginners , any help in this regard? Any easy cheat sheet which can clearly tell the difference and hence help me understand when to use what.

Context:
All these three seems to be very closely related and are used post model is created and trained.

Explainable AI :- Shows why model has given the prediction the way it is. It shows you which part of image was more prominent to make a decision or which feature in text is more prominent using feature attribution
TensorBoard :- provide a holistic view on metrics, loss, fairness, hyperparameter tuning, model performance on machine
What-If Tool:- This is part of TensorBoard, but you can also use it outside of TensorBoard, in a Jupyter notebook as well. Here you pass the final output model as well the data to it and using both of this this tools will give you much boarder visual picture on loss measure , model fairness …etc.

In Summary
Explainable AI is used get deeper understanding on model apart from your normal evaluation metrics like ROC/AUC , RMSE …etc. It will give you details about which feature in your trained model is impacting the result and to what extend . Note here you cannot modify the value dynamically add test the model again for new values
TensorBoard:- This is a visual tool, is used to get insight about your normal evaluation metrics like ROC/AUC, RMSE …etc. Apart from this you can train your model for several hyperparameter’s so using Tensorboard you can get a visual view on the performance of model for different hyperparameter’s and hence TensorBoard helps in hyperparameter tuning
What-if Tools:- As mentioned above it is part of TensorBoard. Now what this gives is that you can dynamically modify the input data and then see how the model performs so its a dynamic training that’s happening in What-if.
Conclusion: All three tools are very different from each other, they are not same though they might look same at the beginning.


Unclear error from tensorflowjs_converter of USE large from TFHub

Question:
When I run
tensorflowjs_converter    --input_format=tf_hub    ‘tfhub.dev/google/universal-sentence-encoder-large/5’    use-large
(the URL in the command starts with https:// but I can’t post URLs to this forum)
It responds
ValueError: Signature “default” does on exist in the saved model
I believe I correctly followed the directions in tensorflow/tfjs/tree/master/tfjs-converter on Github
How can I proceed?

Context:
Ken_Kahn:

tensorflowjs_converter --input_format=tf_hub ‘tfhub.dev/google/universal-sentence-encoder-large/5’

you should specify the --signature_name for the converter CLI, since tfhub module seems to have default signature as ‘serving_default’ now, instead of ‘default’.
tensorflowjs_converter --input_format=tf_hub --signature_name=serving_default ‘https://tfhub.dev/google/universal-sentence-encoder-large/5’ /tmp/web_model


Why can’t I build a VGG19 model in keras?

Question:
I have no idea how to fix it. Here is my code and error:
from keras import backend as K
    target_image = K.constant(preprocess_image(target_image_path))
    style_reference_image = K.constant(preprocess_image(style_reference_image_path))

    combination_image = K.placeholder((1, img_height, img_width, 3))
    input_tensor = K.concatenate([target_image, style_reference_image, combination_image], axis=0)
    model = vgg19.VGG19(input_tensor=input_tensor, weights='imagenet', include_top=False)
    print('model loaded')

AttributeError Traceback (most recent call last)
<ipython-input-17-709cfab4486a> in <module>
      6 combination_image = K.placeholder((1, img_height, img_width, 3))
      7 input_tensor = K.concatenate([target_image, style_reference_image, combination_image], axis=0)
----> 8 model = vgg19.VGG19(input_tensor=input_tensor, weights='imagenet', include_top=False)
      9 print('model loaded')

~/anaconda3/envs/tf_gpu/lib/python3.7/site-packages/keras/applications/__init__.py in wrapper(*args, **kwargs)
     18         kwargs['models'] = models
 19         kwargs['utils'] = utils
---> 20         return base_fun(*args, **kwargs)
 21
 22     return wrapper

~/anaconda3/envs/tf_gpu/lib/python3.7/site-packages/keras/applications/vgg19.py in VGG19(*args, **kwargs)
  9 @keras_modules_injection
 10 def VGG19(*args, **kwargs):
---> 11     return vgg19.VGG19(*args, **kwargs)
 12
 13

~/anaconda3/envs/tf_gpu/lib/python3.7/site-packages/keras_applications/vgg19.py in VGG19(include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
102         img_input = layers.Input(shape=input_shape)
103     else:
--> 104         if not backend.is_keras_tensor(input_tensor):
105             img_input = layers.Input(tensor=input_tensor, shape=input_shape)
106         else:

~/anaconda3/envs/tf_gpu/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in is_keras_tensor(x)
693     ```
694     """
--> 695     if not is_tensor(x):
696         raise ValueError('Unexpectedly found an instance of type `' +
697                          str(type(x)) + '`. '

~/anaconda3/envs/tf_gpu/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in is_tensor(x)
701
702 def is_tensor(x):
--> 703     return isinstance(x, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(x)
704
705

AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike'

Context:
Are you using Keras or tf.keras?
What Is your TersorFlow version?


Question: In many cases, it’s useful to train different layer groups with different learning rates. How do we achieve this with the stand keras.optimizers?

Context:
I don’t think that we have an off the shelf solution.
Probably you could use a custom function with  gradient_transformers 5


Problem with GRU Stacking in Text Generation Tutorial

Question:
I am doing TensorFlow’s text generation tutorial and it says that a way to improve the model is to add another RNN layer.
The model in the tutorial is this:
class MyModel(tf.keras.Model):
  def __init__(self, vocab_size, embedding_dim, rnn_units):
    super().__init__(self)
    self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
    self.gru = tf.keras.layers.GRU(rnn_units,
                                   return_sequences=True,
                                   return_state=True)
    self.dense = tf.keras.layers.Dense(vocab_size)

  def call(self, inputs, states=None, return_state=False, training=False):
    x = inputs
    x = self.embedding(x, training=training)
    if states is None:
      states = self.gru.get_initial_state(x)
    x, states = self.gru(x, initial_state=states, training=training)
    x = self.dense(x, training=training)

    if return_state:
      return x, states
    else:
      return x

And I tried adding a layer doing this:
class MyModel(tf.keras.Model):
  def init(self, vocab_size, embedding_dim, rnn_units):
    super().init(self)
    self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
    self.gru = tf.keras.layers.GRU(rnn_units, return_sequences=True, return_state=True)
    self.gru2 = tf.keras.layers.GRU(rnn_units, return_sequences=True, return_state=True)
    self.dense = tf.keras.layers.Dense(vocab_size)

  def call(self, inputs, states=None, return_state=False, training=False):
    x = inputs
    x = self.embedding(x, training=training)
    if states is None:
      states = self.gru.get_initial_state(x)
    x, states = self.gru(x, initial_state=states, training=training)
    x, states = self.gru2(x, initial_state=states, training=training)
    x = self.dense(x, training=training)

    if return_state:
      return x, states
    else:
      return x

The accuracy during training is above 90% but the text that it generates is nonsensical.
What am I doing wrong?
How should I add that new layer?
Edit:
This is an example of the text generated:
Y    gué  el chme th ¡G   : i uit: R dud d RR  dududut ded,d!D! ties, is: y ui: iu,: ¡RRAShad wy…Ze…Zlegh Fither k.#É…WIkk.DR… t: W: R: IXII?IllawfGh…ZEWThedWe td y: W,Y,!:Z
Edit 2:
this is the tutorial I am following:

      TensorFlow

Text generation with an RNN  |  TensorFlow 1

my code is essentially the same except for the new GRU layer

Context:
dsr:

The accuracy during training is above 90% but the text that it generates is nonsensical.

RNN/LSTMs are not the current state-of-the-art for natural language generation. For example, in the  Google AI Blog: Reformer: The Efficient Transformer 8 post it says that:

In the language domain, long short-term memory 1 (LSTM) neural networks cover enough context to translate sentence-by-sentence 7. In this case, the context window (i.e., the span of data taken into consideration in the translation) covers from dozens to about a hundred words. The more recent Transformer model 1 not only improved performance in sentence-by-sentence translation, but could be used to generate entire Wikipedia articles through multi-document summarization. This is possible because the context window used by Transformer extends to thousands of words.

Have you tried any of the pre-trained Transformer-based models or trained one of your own?
You may find the following posts and research papers useful:

A Transformer Chatbot Tutorial with TensorFlow 2.0 — The TensorFlow Blog 1

T5: Google AI Blog: Exploring Transfer Learning with T5: the Text-To-Text Transfer Transformer (Colab - “Fine-Tuning the Text-To-Text Transfer Transformer (T5) for Closed-Book Question Answering”: Google Colaboratory). arXiv - [1910.10683] Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer (2019)

Reformer: Google AI Blog: Reformer: The Efficient Transformer 8

ALBERT: Google AI Blog: ALBERT: A Lite BERT for Self-Supervised Learning of Language Representations

BERT: Google AI Blog: Open Sourcing BERT: State-of-the-Art Pre-training for Natural Language Processing

Fundamentals of Text Generation. This is an introductory tutorial on… | by Murat Karakaya | Deep Learning Tutorials with Keras | May, 2021 | Medium 1 (includes a lot of reference links).


Question:
There seems to no support available for LSTM layer quantization.
If one has to do Quantization of LSTM layers, what will be starting point ?

Context:
Hi, I think you can starts from Post training quantization : Post-training quantization  |  TensorFlow Lite 4
Unfortunately, Quantization aware training is not supports LSTM/RNN layers yet.
(It just in the plan.)
For your information, There’s known issue related to LSTM quantization: Can LSTM be fully quantized for inference? · Issue #25563 · tensorflow/tensorflow · GitHub 14


Unsupported characters in Function `_wrapped_model` contains input name(s)

Question:
I’ve been using Tensorflow 2.4 and recently upgraded to 2.5 and got this warning, I know it’s only a warning but it’s rather odd as the rename just put it all on lowercase? Hoping someone has insight on the warning.
WARNING - Function `_wrapped_model` contains input name(s) BP, BPA, CF, CP, CRS, Class, D, DIST, DLR, FU, JA, JT, LSC, LSD, LSL, LSP, LST, LSW, NR, P, Race, SP, SU, TA, TIM, TU, UP, WT with
unsupported characters which will be renamed to bp, bpa, cf, cp, crs, class, d, dist, dlr, fu, ja, jt, lsc, lsd, lsl, lsp, lst, lsw, nr, p, race, sp, su, ta, tim, tu, up, wt in the SavedModel.

Context:
It was introduced with

      github.com/tensorflow/tensorflow

      Log info message if input name in function signature changes in SavedModel, which get converted here:  https://github.com/tensorflow/tensorflow/blob/7e3a0d6be0e1c5f2c87d8552c092055d6340f596/tensorflow/core/framework/graph_to_functiondef.cc#L82-L93 33

        committed

        Nov 5, 2020

          +63
          -3

Also clean up signature_serialization.canonicalize_signatures

PiperOrigin-RevI…d: 340680532
Change-Id: Ia5794e5ead1171531ebd08a8234538add68d45d4

Just to inform the user when an input name Is normalized.


Question:
Hi folks. When using mixed precision to perform transfer learning with any hub model I run into the following error:
ValueError: Could not find matching function to call loaded from the SavedModel. Got:
      Positional arguments (2 total):
        * Tensor("x:0", shape=(None, 224, 224, 3), dtype=float16)
        * False
      Keyword arguments: {}

    Expected these arguments to match one of the following 4 option(s):

    Option 1:
      Positional arguments (2 total):
        * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='input_1')
        * True
      Keyword arguments: {}

    Option 2:
      Positional arguments (2 total):
        * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='x')
        * False
      Keyword arguments: {}

    Option 3:
      Positional arguments (2 total):
        * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='input_1')
        * False
      Keyword arguments: {}

    Option 4:
      Positional arguments (2 total):
        * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='x')
        * True
      Keyword arguments: {}

Is it a known issue? To reproduce this, just take this official example: Transfer learning with TensorFlow Hub 1 and add the following lines of code in the library imports:
from tensorflow.keras import mixed_precision
policy = mixed_precision.Policy('mixed_float16')
mixed_precision.set_global_policy(policy)

The data type of the last layer of the classifier should be float32. This is to prevent numeric instabilities, though. Also, to perform mixed-precision the compute capability of your GPU should be at least 7 or higher. V100 is a good example of this.
Cc: @lgusm @kempy @markdaoust

Context:
Hi Sayak,
This is expected behaviour as you cannot change number types that are saved on the SavedModel.
The model publisher could publish a version with mixed precision and allow you to do what you want but I don’t know if anyone did that yet.
From the docs 4: “Note: The data types used by a saved model have been fixed at saving time. Using tf.keras.mixed_precision etc. has no effect on the saved model that gets loaded by a hub.KerasLayer.”


Question:
While converting a model using tflite, I want to understand the internal block diagram of it. How the conversion is taking place.
Is there internal pruning happening in it ??
If yes, then does it make sense to prune a network beforehand ??

Context: Generally I suggest you to select tags and category from the menu on a new thread cause specialized technical team members could be subscribed only to a tag subset  (e.g. in this case I suggest tflite and docs)


Question:
We are a group of 4 people from Padova University, studying computer engineering.
The University assigned us a project that concern the analysis an open source code, and we choose your project. We have some quick questions about the project, and we wonder if some of the original developers were available to answer them. If this is the wrong place to ask, please give us the right contact

Context: Do you have any other details about your project? /cc @ewilderj @thea


Question:
#help_request #tfdf
I can’t seem to be able to make prediction using a tfdf model, I’m working in colab and I obtain an “assertion error” anytime I try using something like model(Xtest) or model.predict(Xtest) while everything seems to go smooth up to that point.
Any idea of what goes wrong ?
Thanks,
Cristelle.

Context:
Hi Cristelle,
can you post the code you used?
Using the beginner colab 5, after the cell with model_1.save(…) (in the section Prepare this model for TensorFlow Serving) I’ve tried this:
for r in test_ds.take(1):
  print(model_1(r[0]))

And I got the expected predictions


Question:
I encountered error while working on tensorflow
ValueError: Items of feature_columns must be a _FeatureColumn. Given (type <class ‘tensorflow_hub.feature_column_v2._TextEmbeddingColumnV2’>): _TextEmbeddingColumnV2(key=‘sentence’, module_path=‘TensorFlow Hub 1’, output_key=None, trainable=False), can anyone help me?

Context: Possible to provide the code? Otherwise, it’s difficult to get a sense of what’s going wrong.


Click an Image and Display Similar Results

Question:
Hello Everyone
Hi, so i have been building a Django project in python for the last two months and have got everything ready from user registration to file upload. Now my LAST part left is the following.
A User clicks an image on site which then drops down and shows similar results of that image.
How can i achieve that ? I want to fetch the results from the Database back-end of python (Will use the async Fetch function for that ) and the add Event listener for clicking an Image.
Will Really Need Your Help. Thanks  Will TensorFlow.js be suitable? I am a newbie at this.
So far I know the Following:
i have got the concept, that is extract the features and then compare histograms and display similar results.
is there an example in java script. i tried finding it. just could not get it any where
mostly i found is image resizing
Will it be better to do it in the back end or use JavaScript ? Sorry for Noob Question

Context:
You’re in the right direction regarding the features.
My first try would be to extract the image feature vector and use it (instead of the text embeddings) shown in this tutorial: Semantic Search with Approximate Nearest Neighbors and Text Embeddings 2


Can BERT be used as an alternative to the Universal Sentence Encoder?

Question: BERT is available in TFJS for question answering but can also it be used as a sentence encoder? USE is getting pretty old and my impression is that BERT is better for the same task. Is it?

Context:
I don’t know if you need exactly BERT for your project but if you want something ready we have many USE models available in TensorFlow Hub 24
Here you can find conversion commands for TFjs:

      TensorFlow

Importing a TensorFlow GraphDef based Models into TensorFlow.js 5


Help with cuDNN installation: GPU With Tensorflow on Jupyter Notebook

Question:
Hi guys, i’m trying to use my GPU(Nvidia GeForce 540M) with Tensorflow.
I’ve installed CUDA 6.5 but it seems not be supported by tensorflow.
I’ve seen that is mandatory to have cuDNN
Can anyone help me?

Context: Hello @Luca_Zanetti You may need to create an NVIDIA Developer Account, if you don’t have one already, before downloading cuDNN. (Requirements: GPU support  |  TensorFlow 7 This tutorial showing how to install CUDA, cuDNN, etc by a TensorFlow user seems to be getting some good feedback, you may find this useful: https://youtu.be/hHWkvEcDBO0?t=144 7 Keep us posted and good luck!


Adapt TensorFlow 1 Variational Autoencoder example from a notebook

Question:
This is based on: tensorflow-mnist-vae/mnist_vae.ipynb at master · CharlesD1ng/tensorflow-mnist-vae · GitHub
In my model.py, I have:
class model():
  def __init__(self):
    self.X = tf.placeholder(dtype=tf.float32, shape=[None, 28, 28], name='X')
    self.keep_prob = tf.placeholder(dtype=tf.float32, shape=(), name='keep_prob')

  def encoder(self): #I'm using self.X here to find z
    ...
    return self.X, self.keep_prob, z, mn, sd

  def decoder(self, z): #I'm using z (generated in encoder) to find image
    ...
    return image

  def loss(self, image, sd, mn):#I'm using image generated by decoder and the original image self.X
    ...
    return loss

  def optimizer(self, loss, lr): #Call optimizer by using loss and learning rate.
    ...
    return optimizer

In my training.py, I want to train the model that I defined in model.py. Therefore I have:
mnist = input_data.read_data_sets('MNIST_data')
lr = 0.005

model = model()
X, keep_prob, z, mn, sd = model.encoder()
decoder = model.decoder(z)
loss = model.loss(decoder, sd, mn)
optimizer = model.optimizer(loss, lr)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for epoch in range(epoch_number):
    batch = [np.reshape(b, [28, 28]) for b in mnist.train.next_batch(batch_size=batch_size)[0]]
    loss_, _ = sess.run([loss, train_optimizer], feed_dict={X: batch, keep_prob: 0.8})

    print("iteration: {}, overall_loss: {}".format(epoch, loss_))

Actually my question is related to that point:
model = model()
X, keep_prob, z, mn, sd = model.encoder()
decoder = model.decoder(z)
loss = model.loss(decoder, sd, mn)
optimizer = model.optimizer(loss, lr)

I’m new on tensorflow and not sure if it is the right way to train a model in tensorflow 1. I saw something scope, variable, get_collection things and a little bit confused. Is it the right way of training a model? Because, if it is the case. After saving my model, to generate new images using this decoder that I trained, I have some difficulties. In a similar way, to test my model that I saved, in test.py, I do:
model = model()
X, keep_prob, z, mn, sd = model.encoder()
decoder = model.decoder(z)

saver = tf.train.Saver()
sess = tf.Session()

saver.restore(sess, 'model.pk')

randoms = [np.random.normal(0, 1, intermediate_dim) for _ in range(100)]
images = sess.run(dec, feed_dict = {z: randoms, keep_prob: 1.0})
k = [np.reshape(imgs[i], [28, 28]) for i in range(len(images ))]

In this writing style, to use decoder I need to call encoder. If I do not call it, it will not know the variable z. See:
X, keep_prob, z, mn, sd = model.encoder()
decoder = model.decoder(z)

Do you have any idea about what is the right way to use different .py files while training a deep learning network using tensorflow 1.
Thank you in advance.

Context:
I suggest you to start with TF 2 directly. Take a look at

      TensorFlow

Convolutional Variational Autoencoder  |  TensorFlow Core 2


Question:
@Bhack could you provide a minimal example of tfa.image.compose_transforms()?

      TensorFlow

tfa.image.compose_transforms  |  TensorFlow Addons 14

  Composes the transforms tensors.

Context:
Here you have rotation + translation

      github.com

    tensorflow/addons/blob/master/tensorflow_addons/image/tests/transform_ops_test.py#L41-L55 11

          def test_compose(dtype):
              image = tf.constant(
                  [[1, 1, 1, 0], [1, 0, 0, 0], [1, 1, 1, 0], [0, 0, 0, 0]], dtype=dtype
              )
              # Rotate counter-clockwise by pi / 2.
              rotation = transform_ops.angles_to_projective_transforms(np.pi / 2, 4, 4)
              # Translate right by 1 (the transformation matrix is always inverted,
              # hence the -1).
              translation = tf.constant([1, 0, -1, 0, 1, 0, 0, 0], dtype=tf.dtypes.float32)
              composed = transform_ops.compose_transforms([rotation, translation])
              image_transformed = transform_ops.transform(image, composed)
              np.testing.assert_equal(
                  [[0, 0, 0, 0], [0, 1, 0, 1], [0, 1, 0, 1], [0, 1, 1, 1]],
                  image_transformed.numpy(),
              )


Question:
Hey! I have just recently started with TF and ML in general and wanted to use random forest on our dataset. I was pretty excited when I saw that there was finally something out for the newer version (compared to having to run TF 1.15) and a great guide to it, however I am already struggling at the first steps (I am rather new to docker and linux as well).
My issue is: when I use the tensorflow/tensorflow:latest-jupyter, I get an error when trying the “pip install -q tensorflow_decision_forests”
WARNING: The candidate selected for download or install is a yanked version: ‘tensorflow-decision-forests’ candidate (version 0.0.0 at https://files.pythonhosted.org/packages/9b/c2/5b5d8796ea5cb8c19457cd9b563c71b536ffc3d14936049fa9cf49b31dcf/tensorflow_decision_forests-0.0.0-py3-none-any.whl#sha256=18ed810f415437ef8e8a4848d3cbf2cff51e3ea2b66224b75f9d9c0f689629a7 2 (from Links for tensorflow-decision-forests))
Reason for being yanked:
and then when trying to import it, it obviously says cannot find decision-forests module. I looked at the newer versions of the decision forests module, but they only seem to be python 3.7 or higher, the container however runs 3.6? When I tried upgrading python it kinda broke everything else in it, so I am not sure what to do. It would be great if anyone has a solution to this!

Context:
I am trying to update also the jupyter (devel) image with this PR Update TF official devel images to Ubuntu 20.04 and precommit hooks by bhack · Pull Request #48371 · tensorflow/tensorflow · GitHub 1
Problably we could also upgrade the non devel Docker image if this will be merged.


Question:
Hi, i wanna know if you had use some programs to analyze CK,LOC,CC metrics of TensorFlow code.
Thanks to the availability, Andrea.

Context: Hi @Andrea , Can you please describe your question in brief please? I didn’t get the context of what you want to say. If you are searching for documentation on metrics that tensorflow supports and here 10 it is.


Understanding Implementations of layers related to RNN (e.g. LSTM, GRU etc)

Question:
I am not that good with RNNs and want to understand it in depth. Also, not just how to create RNN based models and which models are good, I also want to understand how tensorflow has implemented LSTM and GRU like layers.
Any materials (Books, Blogs, Videos etc.) would be appreciated for learning RNNs.
Please, also suggest the part of the code which can be starting point to understand implementation of RNN based layers in tf.

Context:
Personally, in TF/Keras I suggest you to make a first pass on

      keras.io

Keras documentation: Working with RNNs 8


“the kernel appears to have died”: Problems using Tensorflow2 on a Ubuntu 21.04 desktop

Question:
hello, I am new here in the forum. I am a retired analyst who tinkers with Python, Tensorflow, PyTorch etc. I have an older Dell Optiplex 760 desktop which I use for some of my ML exercises. Running TF and TF2 on the Ubuntu 20.04 desktop there was non problems, how when I upgraded from Ubuntu 20.04 to Ubuntu 21.04, none of my Python scripts with TF2 would work. as soon as the script tried to run “Import Tensorflow as tf” , I would get the message “the kernel appears to have died, it will restart automatically”.
While its an older computer with 4 megs of ram, the same scripts ran very well on a Ubuntu 20.04 machine with the same specs. I thought that I would ask the question here in case anyone had similar experiences.
I look forward to any responses
Clive DaSilva

Context: You could try to use one of the official docker tensorflow images to see if it solves your problem.


Question:
What is the best way to extract the y_test and X_test data once we have it converted into the tf_dataset?
I could get the y_test with y_tfdf = np.concatenate([y for x, y in test_ds], axis=0)
I was hoping to compare with other GBMs using plot_roc_curve() from sklearn

Context: @Erin , There is an entire guide here 5 in documentation on splitting slicing data when using tensorflow_datasets.


Get_initial_state method in custom RNN cell over-writing user input initial_state when stateful = True

Question:
I’m following the instructions for tf.keras.layers.RNN to define a custom RNN layer and When I call the layer the initial_state passed in as an argument, the initial state is over-written by the output of my get_initial_state() method (which randomly initializes the initial state) when stateful = True.  The custom RNN cell is defined below:
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras import backend
import numpy as np
np.random.seed(42)

class MinimalRNNCell(keras.layers.Layer):

def __init__(self, units, **kwargs):
    self.units = units
    self.state_size = units
    super(MinimalRNNCell, self).__init__(**kwargs)

def build(self, input_shape):
    #weights initialized as zero
    self.kernel = self.add_weight(shape=(input_shape[-1], self.units),
                                  initializer=tf.keras.initializers.Constant(value=0),
                                  name='kernel')
    #constant weights for testing
    self.recurrent_kernel = self.add_weight(
        shape=(self.units, self.units),
        initializer=tf.keras.initializers.Constant(value=1),
        name='recurrent_kernel')
    self.built = True

def call(self, inputs, states):
    prev_output = states[0]
    h = backend.dot(inputs, self.kernel)
    output = h + backend.dot(prev_output, self.recurrent_kernel)
    return output, [output]

#randomly initialized initial state
def get_initial_state(self, inputs=None, batch_size=None, dtype=None):
    initializer = tf.keras.initializers.RandomNormal(mean=0., stddev=1., seed=None)
    return initializer((batch_size, self.units))

batch_size = 8
input_dim = 4
state_size = 3
timestep = 2

input = tf.zeros((batch_size, timestep, input_dim)) #input initialized to zero
init = ( tf.random.stateless_normal([batch_size, state_size], seed = (1,2) ))

min_rnn = MinimalRNNCell(state_size)
layer = keras.layers.RNN(min_rnn, return_sequences=True, stateful=True)
out =layer(input , training = True , initial_state =  init)
out[:,0,:] == backend.dot(init, min_rnn.recurrent_kernel)

Regarding the last four lines, when layer is defined such that stateful = False, the last line generates a boolean array of all True indicating that the initial_state is being passed in properly. However, when stateful = True, that’s no longer the case. I’m wondering how to fix this such that the user input initial state always over-rides the random initialization from my get_initial_state() method.

Context:
Not sure if it will fit your solution but the following tutorial gives you an idea of setting the initial LSTM states of a decoder in an NMT setting:

      TensorFlow

Neural machine translation with attention  |  Text  | ... 5


Is there an Android equivalent to the Apple Word Tagger Model?

Question:
On the iOS version of our app we are using the Apple Word Tagger Model 20 so that it can assist us with parsing some complex sentences and that way we can extract the parts that we actually want. It is working fairly well.
I need something similar on Android.
Basically I would feed it a large sample of sentences and a classification for every word in the sentence and once it learns those I can pass it some new sentences and it’ll tell me the classification of each of the words.
A similar example to what we want would be an address parser. Imagine that the training data looks like:
123, house_number
E., direction_short
Main, street_name
Av., avenue
17123, house_number
W. direction_short
West, direction_long
Ct., court
10th, street_name
etc, etc
And then we pass it “87 W. Central St.”, then the word tagger would tell us 87 is house_number, W. is direction_short, etc.
I tried using the Bert Classifier and the NLClassifier with my own model but when I get the classifier results back it is analyzing the entire “87 W. Central St.” and telling me the score for each possible label, at least I think that is what is doing.
I need a model that will tell me the category (label) of each word in my sentence. Is there such a thing for Android?
Hope that makes sense.
Thanks.

Context:
If you are looking for something canned like Apple WTM you can take a look at MLKIT:

      Google Developers

Extract Entities with ML Kit on Android  |  Google Developers 26


Question:
Hi everybody,
despite searching on google and the forum, I wasn’t able to find a good explanation on how to initialize an agent with a custom policy. How do I do that? Does anybody have a hint?
Greetings,
Stefan

Context:
Have you already seen this tutorial?

      TensorFlow

Policies  |  TensorFlow Agents


Get the first element in the output layer

Question:
Dear All
I try to perform a simple multiplication using the first element of the output layer. My code looks like the following:
from tensorflow.keras.layers import *
import tensorflow as tf
scale_, mean_ = 2., 4.
a = Input(shape=(128,128), name=‘Input_vec’)
m_num = Input(shape=(4,), name=‘Input_num’)
output = Lambda(lambda x: tf.multiply(x[0], x[1]))((a, m_num[1]))
But I get the following error:
ValueError: Dimensions must be equal, but are 128 and 4 for '{{node lambda_18/Mul}} = Mul[T=DT_FLOAT](Placeholder, Placeholder_1)' with input shapes: [?,128,128], [4].

Context:
Check tf.multply doc

If x.shape is not the same as y.shape, they will be broadcast to a compatible shape.

      TensorFlow

tf.math.multiply  |  TensorFlow Core v2.5.0 2

  Returns an element-wise x * y.


How to put weights from a csv file into a model

Question:
I want to compare the recognition rate before and after quantization.
First, i want to save the weights of the trained model as a csv file,
quantize them through an external program, and then input the csv file
as the weights of the model again
but, i don’t know how…
thx for reading my Question.

Context:
tf.keras.Model has explicit methods get_weights and load_weights with which you can store weights in any type of file and load them from any type of file.
Please take a look at this stackoverflow answer 13


Question: I am running Google Colab locally on my Macbook Air running the M1 chip. I have installed the pre-released TensorFlow and am trying to run a ConvNet model, but get an error saying ‘ImportError: Image transformations require SciPy. Install SciPy’. I have read the readme on the GitHub for the pre-released TF and it says SciPy is currently not available.  I tried installing SciPy on the virtual environment that TF was installed in but still no luck, does not import when running an interactive python session on my terminal and also when running the model again. Has anyone gotten around this? I cannot stand waiting 15+ minutes per epoch on 30 epochs when training my model when using Colabs hosted server.

Context:
It Is a temp known limit but there are workarounds:
E.g. See Installing TensorFlow and JupyterLab on Mac with M1 27


Difference between training with original grayscale image and color image converted to grayscale

Question:
Hi
When I load grayscale, I use cv2.imread and give cv2.IMREAD_GRAYSCALE as the flag.
I just wondered what will happen if I train the model by color image from gray scale.
I Tried, and the training ended successfully.
Now, I’ve got something to ask.
What difference is between training with original grayscale image and color image converted to grayscale?
Is it better to train grayscale model with original grayscale images than to train color model with color images converted to grayscale?

Context:
If the conversion to grayscale produces the same output as the grayscale image then the output of the training should be identical (modulo randomness).
If the conversion is different, then it depends on how big the difference is. But this is unikely, as conversion to grayscale is usually done using the same formula.


Question:
Hello,
I have found the following code as alternative to the torch.select_index function, but the loop makes the process very slow. Do you have any suggestion ?
    def tf_index_select(self, input_, dim, indices):
    """
    input_(tensor): input tensor
    dim(int): dimension
    indices(list): selected indices list
    """
    start = time.time()
    shape = input_.get_shape().as_list()
    if dim == -1:
        dim = len(shape)-1
    shape[dim] = 1

    tmp = []
    for idx in indices:
        begin = [0]*len(shape)
        begin[dim] = tf.dtypes.cast(idx, tf.int64)
        tmp.append(tf.slice(input_, begin, shape))
    res = tf.concat(tmp, axis=dim)
    return res

Thank you

Context:
Is tf.gather enought for your use case?

      TensorFlow

tf.gather  |  TensorFlow Core v2.5.0 2

  Gather slices from params axis axis according to indices. (deprecated arguments)

Then we have also tf.gather_nd:

      TensorFlow

tf.gather_nd  |  TensorFlow Core v2.5.0 2

  Gather slices from params into a Tensor with shape specified by indices.


Question:
Hello,
I hope everyone is having a good day. I have been experimenting on TFX and running pipelines in Kubeflow trying to build an end-to-end solution.
After the model deployment phase, I have been wondering if there is a supported tool for model monitoring in the production environment to check whether a model is performing as expected/ what are the responses to the inference requests and if they are true in nature… I’m looking for a deployment solution… keeping eye on the running model especially in the kubeflow context.
I would love to hear from the team.
Thank You.

Context:
Hi Muhammad,
It depends on where you’re deploying to.  For TFlite and TFJS deployments model monitoring is very different.  For TensorFlow Serving it would mostly be a matter of logging and then analyzing the log data for drift and skew.  To detect concept drift you will need labeling of samples of prediction requests.  For Vertex AI there is a preview release of model monitoring support 5.
thanks,
Robert


Video classification TensorFlow Lite model(i3d) with 5D input on android

Question:
Need to run a sign language video classification model(i3d) on android, that takes 5 dimension ( 1 3 64 224 224),examples/lite/examples/image_classification/android at master · tensorflow/examples · GitHub 2 , but failes. But runs on PC with python3.7.

java1574×237 58 KB

This is the error. I think it is due to the input size. In opencv we can concat frames and convert it to a numpy array to get the size as (1 64 224 224 3) then transpose it to (1 3 64 224 224). No idea on how it can be done with Java.

input1400×925 35.1 KB

output747×923 31.9 KB

Context:
What is the exact type of the input tensor buffer that you’re using in the Java code?
Based on the error message, your Java buffer size is 576 bytes, which suggests that your buffer type / size doesn’t match what’s expected by your model.


Tensorflow.js – Complete sentence / Predict next word in a sentence

Question:
I’m developing a Machine Learning “fortune teller” that completes sentences from seeds, but my trained algorithm seems very poor or even not usable.
It’s my first time training with tokenized words, and I’ve tried several approaches but non results in a good trained model.
Example of my dataset
This July money will be by your side.
The next days love will come at your door.
Be careful with your friend and money.
Love is going to be hard this year.
A lot of friends will come at your door.
Be powerful at your job it will result good this month.
... etc, etc.

I have:

21,885 unique sentences
1,523 unique words

How have I prepared my data?
I’ve sorted the unique words in alphabetic order and saved them into a 1,523-length array.
For example:
[a, at, be, by, come, door, good, job, lot, ....]

With this built array, I assign to every word of my data a numbered value.
For example:
Hello, my name is Carlos will be equal to [54, 504, 492, 394, 100, 150]
Supposing that in my dictionary Hello=54 ,=504 my=492 name=394 is=100 Carlos=150
Why not using a pre-trained model that completes sentences? Because I have special words that don’t exist in the dictionary and weird names.
Creating my inputs and outputs
Having tokenized my words, I’ve come up with a way to make same-length inputs for my model by grouping array of words into N-length entries.
I’ve decided to split my sentences in arrays of 4-items with an array of 2-items as output, for example:
Hello, my name is {Name} and I like apples.

Will result into:
Input                                Output

["Hello", ",", "name", "is"]         ["{Name}", "and"]
[",", "name", "is", "{Name}"]        ["and", "I"]
["name", "is", "{Name}", "and"]      ["I", "like"]
["is", "{Name}", "and", "I"]         ["like", "apples"]
["{Name}", "and", "I", "like"]       ["apples", "."]
["and", "I", "like", "apples"]       [".", "."]

Note: I start appending “.” at the end of output when the sentence has finished so the algorithm has a good knowledge of when a sentence ends.
Then I repeat these steps for all the 21,885 sentences I have.
And finally I substitute the word with the index of where it’s my word list, so real input/output looks like this.
Input                                Output

[400,500,390,293]                    [303, 442]

Training
My data on this example is constructed by Inputs of 4-length arrays and outputs of 2-length arrays.
Idea is to train an algorithm that given 4 words it can predict the next 2 words. (Used 4-length array for input for this example but I’ve tried multiple array lengths for the input)
Layers and model used so far
const model = tf.sequential();
model.add(tf.layers.dense({units: 100, inputShape: [4]}));
model.add(tf.layers.activation({activation: 'softmax'}));
model.add(tf.layers.dense({units: 2}));
model.compile({loss: 'categoricalCrossentropy', optimizer: tf.train.sgd(0.001) ,metrics: ['accuracy']});

//70% of the data used for trainning
const xs = tf.tensor2d(inputs, [inputs.length, inputs[0].length]);
const ys = tf.tensor2d(outputs, [outputs.length, outputs[0].length]);

//20% of the data used for validation
const xsVal = tf.tensor2d(inputsVal, [inputsVal.length, inputsVal[0].length]);
const ysVal = tf.tensor2d(outputsVal, [outputsVal.length, outputsVal[0].length]);

model.fit(xs, ys,{epochs: 100,batchSize:64, validationData: [xsVal, ysVal]}).then(async () => {
  const saveResult = await model.save('file://modelo2');
});

But I cannot get it train correctly, it loops in a step giving all the time the same loss= value.
I’ve also tried by changing the training learning rate, but I keep getting the same loop. Which makes me thing I’m not preparing my data correctly or I’m using a bad technique.
I’ve also tried to change the model of training to meanSquaredError, but same problem.
Second approach
Instead of converting the output as a tokenized value of the index of my word list. E.g. Hello=54, I’ve made a another approach to only predict the next word by making a 1,523-length array of [0,0,0,...1,0] with a 1 in the index of my word list.
For example, if I want to tokenize the output of the word website of my list ["car", "apple", "website", "orange", "red"] the output will be a [0, 0, 1, 0, 0] array
So now my data looks like:
Input                                Output

[400,500,390,293]                    [0,0,0,0,0,0,0,0,0,0,1]

But this also leads to a failed training.
What am I missing or doing wrong?
I think I have to achieve something like LSTM, but I haven’t trained one as such and I’m lost about how to prepare the data for the problem I need to solve and how to prepare the layers and model.
Can anyone suggest a better approach for this?
Thanks in advance.
Note: All data and tokens for this example have been made up to explain the problem.

Context:
@cabada , I don’t know if your model architecture might get good results.
I’d suggest you look into this tutorial: Text generation with an RNN  |  TensorFlow 18
The tutorial uses charprediction but I think you can adapt it to word prediction.


Visualize Object detection model SSD mobilenet v2

Question: Can anyone please help me to visualize the inner features of the SSD  model at the time of inference, like the feature map of each layer?

Context: BTW I have no clue on how to get access to the last layer of the model. You know class prediction, bbox prediction, etc.


Model checkpointing best practices when using `train_step()`

Question:
Subclassing tf.keras.Model and overrding its train_step() function give us the kind of flexibility we need to control our training loops. It allows for easily plugging in our favorite callbacks, and almost do all kinds of stuff that are readily available during model.fit().
I am wondering how to use the ModelCheckpoint callback in this instance. Consider the following use-case (taken from here 2):
class SimSiam(tf.keras.Model):
    def __init__(self, encoder, predictor):
        super(SimSiam, self).__init__()
        self.encoder = encoder
        self.predictor = predictor
        self.loss_tracker = tf.keras.metrics.Mean(name="loss")

How do I set up a ModelCheckpoint callback for this one?

Context:
Can you use something like
Training checkpoints  |  TensorFlow Core 8 ?


Question:
I’m working on running ML models from TF on microcontrollers. For scaling the data: Is there a way getting the calculated values for ‘mean’ and ‘std’ from the StandardScaler? Or is there an easy way of scaling my sensor data using the same scaling in Arduino code on the device?
Thank you for your advice.

Context:
I’ve found a solution, if anyone wants to know:
In tensorflow scale the data normally:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train =  scaler.transform(X_train)
X_test =  scaler.transform(X_test)
To get the mean type:  scaler.mean_
To get the std type:  scaler.scale_
Copy and paste these figures into your Arduino code using the formula below:
The standard score of a sample x is calculated as:
z = (x - u) / s
where u is the mean of the training samples or zero if with_mean=False, and s is the standard deviation of the training samples or one if with_std=False.
Example code below:
float Sensor1 = (sensorReading1 - mean_) / scale_;


Is there any oppotunities that I can talk to Tensorflow engineers?

Question:
Hello everyone, I recently working on some TFLite and TFLite quantization work and encountered some questions. I did raised those questions on

      github.com

tensorflow/tensorflow 4

  s

  An Open Source Machine Learning Framework for Everyone

I am thinking to find some more efficient ways to communicate with Engineers like open discussion via ZOOM or some 1-on-1 session.
Is there anything like what I described?

Context: @Orange_J , I am not sure about 1-to-1 talks but this (tf forum) is I think the right place to ask your questions. You can tag the person who you think can answer your question


Question:
At what granularity should I apply tf.function to my models and layers? Should I decorate every layer’s call with @tf.function or just the model layer’s call method, or just the training step?
It seems to me that decorating keras layers’ call method is not really intended, as tf.function does not support keyword-arguments like training or mask.

Context:
Using tf.function gives you performance as well as reusability benefits by using same traces and gives you flexibility to use concrete functions.
Attaching doc reference 7 for how better performance can be achieved with tf.function.


What is considered samples in Tensorflow object detection API?

Question:
In image classification, it is obvious that images are considered as samples but what is considered as samples in Tensorflow object detection API? Are the samples images or the individual objects in the images?
if an image contains 10 objects, should I consider 10 objects as training samples or just a single image?
I need to know this to effectively set batch size and steps for training.

Context:
The question is very interesting.
IMO it should be images here as well since what we define in API config as batch_size is number of images while training object detection model.


Question: The default input size that resnet50 takes is 224*224,so now can i change the input size of the architecture to some higher resolution and still work on it in tensorfow?

Context: Looking at the Keras Documentation 23, it seems that it is possible but you will have to not include the fully-connected layer/dense layers at the top of the network.


LSTM - need your help with basic sequences

Question:
Hi guys!
I need your valuable help to understand better LSTM’s  for what I think is a relatively simple sequence , the code below runs however I am not getting the expected results,  I am suspecting the way to shape the data, or the  sequence definition, could you please shed light ?
import tensorflow as tf
from tensorflow.keras.layers import Embedding, LSTM, Dense, Bidirectional
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
import numpy as np
import matplotlib.pyplot as plt

#function to plot training
def plot_graphs(history, string):
  plt.plot(history.history[string])
  plt.xlabel("Epochs")
  plt.ylabel(string)
  plt.show()

#the data
# eleven samples, each sample has been divided in  5 sub-sequences of 3 elements each
# e.g.    1,2,3  is one part of the first line / sequence,   10,11,12 is the next

# Expected behaviour:
#  input  50,51   output 52
#  input  61,62   output 70
#  input  2002,2003   output 2010

data = np.array([
1,2,3,10,11,12,20,21,22,30,31,32,40,41,42
,101,102,103,110,111,112,120,121,122,130,131,132,140,141,142
,201,202,203,210,211,212,220,221,222,230,231,232,240,241,242
,301,302,303,310,311,312,320,321,322,330,331,332,340,341,342
,401,402,403,410,411,412,420,421,422,430,431,432,440,441,442
,501,502,503,510,511,512,520,521,522,530,531,532,540,541,542
,601,602,603,610,611,612,620,621,622,630,631,632,640,641,642
,701,702,703,710,711,712,720,721,722,730,731,732,740,741,742
,801,802,803,810,811,812,820,821,822,830,831,832,840,841,842
,901,902,903,910,911,912,920,921,922,930,931,932,940,941,942
,1001,1002,1003,1010,1011,1012,1020,1021,1022,1030,1031,1032,1040,1041,1042
])

#I am not sure if this is the right way to shape the data
data = data.reshape(11,5,3)

#print(data)

#slice the data , so the 3rd element of each subsequence of 3 elements is the label  and the first 2 are the input
#e.g.  1,2,3       1,2  is the input,  3 is the label

xs = data[:,:,:-1]
ys = data[:,:,-1:]

#print ('xs')
#print (xs)

#print ('ys')
#print (ys)

#define the model
lossf =  tf.keras.losses.MeanAbsoluteError()

model = Sequential()

#tried this but didn't make a difference for good
  #model.add(tf.keras.layers.BatchNormalization(input_shape=( 5,  2) ))
  #model.add(Bidirectional(LSTM(150, activation='relu')))

model.add(Bidirectional(LSTM(50, activation='relu' ), input_shape=( 5,  2)))
model.add(Dense(1))
adam = Adam(learning_rate=0.0001,  )
model.compile(loss=lossf, optimizer=adam, metrics=['accuracy'])

#fit
history = model.fit(xs, ys, epochs=120,
                  verbose=1 ,
                  validation_split=0.1 ,
                  batch_size=5
                  #,  shuffle=True
                  )

#plot
plot_graphs(history, 'accuracy')
plot_graphs(history, 'loss')
plot_graphs(history, 'val_accuracy')
plot_graphs(history, 'val_loss')

#try it
predicted =   model.predict( [[[50, 51]]], verbose=0)   # expected  52
print ('Predicted value ' ,  predicted )

predicted =   model.predict( [[[61, 62]]], verbose=0)   # expected  70
print ('Predicted value ' ,  predicted )

predicted =   model.predict( [[[2002, 2003]]], verbose=0)   # expected  2010
print ('Predicted value ' ,  predicted )

Context:
For series data like that, you could try to convert the series into windows. Feeding the whole series to the neural net is possible but won’t give accurate results.
Take a look at this 16.


[Question] implementation of “custom” convolution

Question:
Hello, I am trying to implement a “custom” convolution routine to perform spherical convolutions.
Basically, I store my spherical image as a 1D array following the healpix pixelisation scheme. I am trying to optimize an already existing routine and I would like to re-implement the convolution step to possibly make it more efficient.
A bit of context, you can skip to the last paragraph for the actual question.
For each element of the input array, I have a function which lets me compute the index of the neighboring pixels. The idea is to perform a convolution on the neighboring pixels of each element of the array. This idea works pretty well, but I fear that the implementation is not optimal.
At the moment, for each pixel I am gathering the neighboring pixels and parsing them in a new 1D array (using the tf.gather) , which results to be a 1D array 9 times bigger on which we perform the convolutions (since each pixel has ~8 neighboring pixels). Once I have such an “ordered array” I can perform simple 1D convolutions with stride=9 to achieve a spherical convolution. For more information about this idea, please take a look at the original paper: http://arxiv.org/abs/1902.04083
The actual question:
What I would like to do now is to tell tensorflow to perform a “custom” convolution, given the index of the elements that I want to convolve. Basically I would like to give tensorflow a series of indexes (one set of indexes for each element of the 1D array) and tell it to convolve them directly. This would involve accessing elements from the 1D array not contiguously, I think.
For example, for element 0 of the array, the convolution would involve the elements [ 0, 4, 11, 3, 2, 1, 6, 5, 13], for pixel 1 [1, 6, 5, 0, 3, 2, 8, 7, 16], for pixel 2 [ 2, 8, 7, 1, 0, 3, 10, 9, 19] and so on.
Is there a way to achieve it? Thank you very much for your help!

Context: Do you think there is interest in developing such things in tensorflow? Apparently, most of the research on spherical convolutions is done using PyTorch, it just seems crazy to me that the same things can’t be achieved in Tensorflow.


Retrain TF-Agents model with changed parameters (Checkpointer or PolicySaver)

Question:
Hello everyone!
I am trying to load a deep q model and retrain it with different parameters (e.g. changed epsilon value). I am using a config file for setting various parameters of the environment and the agent.
What is the best way to go about this? Should i use Checkpointer, PolicySaver or something else entirely?
I would be very grateful for your help!
Thank you!

Context:
Sarah_Riedmann:

I am trying to load a deep q model and retrain it with different parameters (e.g. changed epsilon value).

I’d have to experiment with this too. Since Checkpointer allows you to save and load not just the policy network state, but also the training state, I’d give that a go. In addition, it seems like it also caches into a replay buffer, which would be useful for sampling in DQN’s case. (tf_agents.utils.common.Checkpointer  |  TensorFlow Agents 4)
Hope this helps a little. cc @yablak @markdaoust


Custom transfer learning tensorflowlite model gives error in android code for image classification

Question:
Hello, I am trying to create chess pieces image classification program running on my phone. I trained my model using transfer learning technique. Made 6 classes for the six figures and converted my tf model to tflite model. When I use the model with a labelmap in the android project I get an error stating: Caused by: java.lang.IllegalArgumentException: Label number 6 mismatch the shape on axis 1. The whole error log is here: https://pastebin.com/bxdq9x1r.
This is my tflite model: https://www.pastefile.com/vpg57x
This is my label map: https://www.pastefile.com/9rg9v7

Context: The label map URL seems unreachable. Can you check it?


Doubts in loss scaling during distributed training

Question:
I had posted something similar before 3 but couldn’t get around the problem.
I want to how to properly scale the final loss when it’s composed of two or several other individual loss terms as is common in many use-cases like Image Generation. For example, for image generation, a typical loss function is the following:
total_loss = (alpha * perceptual_loss) + (beta * l1_loss)

Note that the non-reduced versions of perceptual_loss and l1_loss do have the same shapes. So, I guess it becomes necessary to reduce them before summing them up. The official guide 2 does provide some guidance but I could not find something concrete around this particular use-case.
Any pointers regarding this would be very helpful.

Context:
I faced the same problem some year ago, and we came up with this solution:
From ashpy/executor.py:
    @staticmethod
    def reduce_loss(call_fn: Callable) -> Callable:
        """
        Create a Decorator to reduce Losses. Used to simplify things.
        Apply a ``reduce sum`` operation to the loss and divide the result
        by the batch size.
        Args:
            call_fn (:py:obj:`typing.Callable`): The executor call method.
        Return:
            :py:obj:`typing.Callable`: The decorated function.
        """
        # decorator definition
        def _reduce(self, *args, **kwargs):
            return tf.nn.compute_average_loss(
                call_fn(self, *args, **kwargs),
                global_batch_size=self._global_batch_size,  # pylint: disable=protected-access
            )

        return _reduce

and we apply this decorator to the losses - in particular, for the L1 loss used during the adversarial training we use it in this way
class GeneratorL1(GANExecutor):
    r"""
    L1 loss between the generator output and the target.
    .. math::
        L_G = E ||x - G(z)||_1
    Where x is the target and G(z) is generated image.
    """

    def __init__(self) -> None:
        """Initialize the Executor."""
        super().__init__(L1())

    @Executor.reduce_loss
    def call(self, context: GANContext, *, fake: tf.Tensor, real: tf.Tensor, **kwargs):
        """
        Call the carried loss on `fake` and `real`.
        Args:
            context (:py:class:`ashpy.contexts.GANContext`): GAN Context.
            fake (:py:class:`tf.Tensor`): Fake data (generated).
            real (:py:class:`tf.Tensor`): Real data.
        Returns:
            :py:class:`tf.Tensor`: Output Tensor.
        """
        mae = self._fn(fake, real)
        return mae

Using this approach we were able to scale the training on multiple GPUs obtaining the same numerical result of doing the same training on a single GPU (with the same batch size = sum of the batch size used in the single GPU).
Hope it helps
PS: you can find some good insight about adversarial training, gans, distributed training and distribution strategy in the ashpy project 1 - feel free to refer to it or use part of the code


Computer-vision project for summer intern

Question: I am a prefinal year undergrad with decent computer vision knowledge. This summer I am planning to apply as a summer intern to various professors. Any suggestions on what I shall present as my research idea?

Context:
As you said thay you have descent knowledge on CV, IMO you may want to focus on GAN based models. Although, GAN itself is very vast field but i can suggest you one topic that i myself thought in my final year.
You may have heard of StyleGAN which produces very high res images of persons that doesn’t exist.
You can focus on creating faces between interpolation space of two images. There can be infinite numbers or faces generated by using linear interpolation of two face vectors. You could start with two and then expand it to work with even multiple face vectors in which the interaction of vectors may become little bit complex.


Question:
How can I compare size of tensor flow model and tensor flow lite model ?
Are there any functions for this or a python module available ?
Like tensor flow model is 10MB and after conversion tflite model is 1MB ?

Context: I have not really understood the question. Do you mean if there is a way to calculate the generated size of tflite file from the original TensorFlow model?


Question:
Hello!
I am trying to convert a TF2 saved model into a frozen graph so that I can load it into TensorBoard in order to figure out what the input/output node names are. Everything that has to do with conversions to frozen graphs seems to be deprecated. And when I try to load the SavedModel file into tensorboard it is overly complicated and doesn’t show input/output node names. Would anyone be able to help with this please?
Thank,
Ahmad

Context:
I guess this is not possible anymore. The SavedModel serialization format is the only format nowadays supported.
You can use the saved_model_cli tool to inspect the content of the saved model and understand what graphs are stored inside and what are the inputs and outputs.
The typical usage of saved_model_cli is as follows
 saved_model_cli show --all --dir saved_model_folder

This should show you the content of the saved model.


Unhandled Rejection (TypeError): t is not a function

Question:
Hi there,
I’m currently experimenting with TF.js.
Using the pre-packaged models is a real walk in the park. But when trying to load models (TF.js, TF Lite or from TF-hub) I seem to be getting an error while predicting:

The code I’m running is the following:
      .then(() => {
        return tf.loadGraphModel(
          "https://storage.googleapis.com/tfjs-models/savedmodel/mobilenet_v2_1.0_224/model.json"
        )
      })
      .then((model) => {
        let img = tf.browser.fromPixels(document.getElementById("image"))
        img = img.cast("float32").reshape([1, 224, 224, 3])
        return model.predict(img)
      })
      .then((predictions) => {
        console.log(predictions)
      })```

I've tried using both `"@tensorflow/tfjs": "^3.5.0",` as well as `"@tensorflow/tfjs": "^3.6.0",`

I'm probably making a rookie mistake here. If anybody would be able to point me in the right direction it would be greatly appreciated!

Context: Generally I suggest you to use tags on new thread cause specialized technical team members could be subscribed only to a tag subset (e.g. in this case tfjs and help_request tags).


Calculate Flops in Tensorflow and Pytorch are not equal?

Question:
Given the same model, I found that the calculated flops in pytorch and tensorflow are different. I used the keras_flops (keras-flops · PyPI 60) in tensorflow, and ptflops (ptflops · PyPI 31) in pytorch to calculate flops. It seems that flops in pytorch are closed to my own calculation by hands. Is that tensorflow has some tricks to speed up the computation so that few flops are measured? My model in tensorflow
d=56
s=12

inp = Input((750 ,750, 1))
x = Conv2D(d, (5,5), padding='same')(inp)
x = PReLU()(x)

x = Conv2D(s, (1,1), padding='valid')(x)
x = PReLU()(x)

x = Conv2D(s, (3,3), padding='same')(x)
x = PReLU()(x)
x = Conv2D(s, (3,3), padding='same')(x)
x = PReLU()(x)
x = Conv2D(s, (3,3), padding='same')(x)
x = PReLU()(x)
x = Conv2D(s, (3,3), padding='same')(x)
x = PReLU()(x)

x = Conv2D(d, (1,1), padding='same')(x)
x = PReLU()(x)
out = Conv2DTranspose(1 ,(9,9), strides=(4, 4),padding='same',output_padding = 3)(x)

My model in tensorflow
node name | # float_ops
Conv2D                   8.92b float_ops (100.00%, 61.95%)
Conv2DBackpropInput      5.10b float_ops (38.05%, 35.44%)
Neg                      180.00m float_ops (2.61%, 1.25%)
BiasAdd                  105.75m float_ops (1.36%, 0.73%)
Mul                      90.00m float_ops (0.63%, 0.63%)

======================End of Report==========================
The FLOPs is:14.3 GFlops

However, the FLops in pytorch is
Model_1(
  0.013 M, 100.000% Params, 45.486 GMac, 100.000% MACs,
  (begin): Sequential(
    0.002 M, 11.804% Params, 0.851 GMac, 1.870% MACs,
    (0): Conv2d(0.001 M, 11.367% Params, 0.819 GMac, 1.801% MACs, 1, 56, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
    (1): PReLU(0.0 M, 0.437% Params, 0.032 GMac, 0.069% MACs, num_parameters=56)
  )
  (middle): Sequential(
    0.007 M, 52.775% Params, 3.803 GMac, 8.360% MACs,
    (0): Conv2d(0.001 M, 5.340% Params, 0.385 GMac, 0.846% MACs, 56, 12, kernel_size=(1, 1), stride=(1, 1))
    (1): PReLU(0.0 M, 0.094% Params, 0.007 GMac, 0.015% MACs, num_parameters=12)
    (2): Conv2d(0.001 M, 10.212% Params, 0.736 GMac, 1.618% MACs, 12, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): PReLU(0.0 M, 0.094% Params, 0.007 GMac, 0.015% MACs, num_parameters=12)
    (4): Conv2d(0.001 M, 10.212% Params, 0.736 GMac, 1.618% MACs, 12, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (5): PReLU(0.0 M, 0.094% Params, 0.007 GMac, 0.015% MACs, num_parameters=12)
    (6): Conv2d(0.001 M, 10.212% Params, 0.736 GMac, 1.618% MACs, 12, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (7): PReLU(0.0 M, 0.094% Params, 0.007 GMac, 0.015% MACs, num_parameters=12)
    (8): Conv2d(0.001 M, 10.212% Params, 0.736 GMac, 1.618% MACs, 12, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (9): PReLU(0.0 M, 0.094% Params, 0.007 GMac, 0.015% MACs, num_parameters=12)
    (10): Conv2d(0.001 M, 5.684% Params, 0.409 GMac, 0.900% MACs, 12, 56, kernel_size=(1, 1), stride=(1, 1))
    (11): PReLU(0.0 M, 0.437% Params, 0.032 GMac, 0.069% MACs, num_parameters=56)
  )
  (final): ConvTranspose2d(0.005 M, 35.420% Params, 40.833 GMac, 89.770% MACs, 56, 1, kernel_size=(9, 9), stride=(4, 4), padding=(4, 4), output_padding=(3, 3))
)
Computational complexity:       45.49 GMac

Context: Can anyone help plz?


Add TFDF outputs in a concatenate API functional layer

Question:
Hi, I’m using Keras API for tabular data with embedding, conv1D and sequential with onehot encoding to get a merged model more efficient (after a concatenation layer). I’m very interesting in adding a Keras gradient boosting model but I guess it won’t be possible to get the output layer (and input) to concatenate multiple outputs and create a merged model ? It seems we can only fit the gradient model (without compile step) as any other gradient boosting models (Xgb, Lgb, Cat…). Do I miss something ? Thank you very much for your information

I read the intermediate colab page, but my question was about the gradient boosting model which is much more efficient than a random forest for tabular data.
In the page, It seems that only random forest can be concatenated with functional API . In addition, my understanding (but I need to go deeper in the example provided in the page) is that the training is performed in 2 steps (not in //). My objective is to use multiple heads  made of functional API (head for embedding  layer,  head for Conv1D…) to compute a tabular dataset in // and concatenate the // heads into a final stream (still API) for a prediction ( with some dense layers and finally a softmax activation for example). Of course, a head with gradient boosting would improve the result of the concatenation.
I did it with a head made of Decision Forest model (from Keras deep_neural_decision_forests) but it was not very powerfull .
By the way, it seems (not important) that you should change vocabulary_size in the data pipeline by vocab_size because it leads to an error in google colab notebook.

Context:
Bonjour/Hello Laurent :),
To make it more visual, here is the model you describe as I understand it:

graphviz491×641 20.5 KB

(Assuming this is the model you describe), this configuration is possible, but there are some caveats:
The TF-DF models don’t backpropagate gradients (yet 4). It means that the parameters of the “Common learnable pre-processing” part cannot be trained through the GBT. Instead, the pre-processing needs be trained with the gradients back-propagated through the “model #1” and “model #2”.
This implies a 2 steps training:

Training of “Common learnable pre-processing” + “model #1” + “model #2”.
Training of “GBT model”.

If there are no “Common learnable pre-processing” stages, all the models (including the GBT) can be trained in parallel (i.e. in the same session call). However, this might not be optimal as GBT trains in exactly on epoch, while NNs generally train over multiple ones. Instead, it will be more interesting to train the GBT and the NN independently (but possibly at the same time).
This colab 18 demonstrates how to train the ensemble shown in the figure. I hope this helps. The API is a bit verbosy, but hopefully, this will improve :).
Cheers,


Question: Tools like Keras Tuner or Hyperas require to modify the code. For large applications, this can be quite cumbersome, especially w.r.t. parameters that are not part of the model itself (e.g. settings for data preprocessing). Moreover, it complicates maintenance of the code base (development vs. production). For this reason, I’m looking for a minimally invasive hyperparameter tuner that works at the level of executables / scripts and passes parameters via command line arguments. Any hints or ideas? Thanks a lot!

Context:
Have you already evalauted  TFX timer?

      TensorFlow

The Tuner TFX Pipeline Component  |  TensorFlow 7


Question: I wish we would have working grouped transposed convolutions. We waited for a long time to have (standard) grouped convolution in TF when it had been available in other frameworks for quite a while. We don’t need to set some kind of tradition over this.

Context:
We have a ticket at

      github.com/tensorflow/tensorflow

      Groups parameter of Conv2d and Conv2dTranpose ('deconvolution') not Working ? 12

        opened

        Nov 27, 2020

          NguyenThaiHoc1

        TF 2.3
        comp:keras
        stat:awaiting tensorflower
        type:bug

    Hello Authors

I'm a user of tensorflow. Someday i have use Deconvolution and …i know it called "Conv2dTranpose" in tensorflow. But I want to reduce parameters of model by groups. I found groups property at Pytorch and I see it in Conv2D class (Conv2dTranspose inheritance Conv2d). But when i use i get result which i don't want
This is my code

`
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Conv2DTranspose, BatchNormalization, ReLU, MaxPool2D
model = tf.keras.Sequential([
    Conv2D(512,
           kernel_size=(3, 3),
           strides=(1, 1),
           padding='same',
           use_bias=False,
           name='conv'),
    BatchNormalization(),
    ReLU(),
    Conv2DTranspose(256, kernel_size=(4, 4), strides=(2, 2),
                    padding='same', use_bias=False,
                    groups=256,
                    kernel_initializer='he_normal'),
    BatchNormalization(),
    ReLU()
])

model.build((32, 256, 192, 3))
model.summary()
`

This is summary when i received
>
Layer (type)                 Output Shape              Param #
conv (Conv2D)                (32, 256, 192, 512)       13824
batch_normalization (BatchNo (32, 256, 192, 512)       2048
re_lu (ReLU)                 (32, 256, 192, 512)       0
conv2d_transpose (Conv2DTran (32, 512, 384, 256)       2097152
batch_normalization_1 (Batch (32, 512, 384, 256)       1024
re_lu_1 (ReLU)               (32, 512, 384, 256)       0
Total params: 2,114,048
Trainable params: 2,112,512
Non-trainable params: 1,536

I think conv2d_transpose will: 2097152 / 256(groups) = 8192 params ?

Sorry for my writer is not good
Thanks you for reading
Thai Hoc


Question: How can I calculate number of MACs and flops for a particular NN architecture ?

Context:
We already have a thread at:

    Calculate Flops in Tensorflow and Pytorch are not equal? General Discussion

    Given the same model, I found that the calculated flops in pytorch and tensorflow are different. I used the keras_flops (keras-flops · PyPI) in tensorflow, and ptflops (ptflops · PyPI) in pytorch to calculate flops. It seems that flops in pytorch are closed to my own calculation by hands. Is that tensorflow has some tricks to speed up the computation so that few flops are measured? My model in tensorflow
d=56
s=12

inp = Input((750 ,750, 1))
x = Conv2D(d, (5,5), padding='same')(inp)
x = PReLU()…

/cc @markdaoust @thea