subclassing

Performance penalty when overriding Numpy's __array_function__() method

Performance penalty when overriding Numpy's __array_function__() method Question: I wrote an array-like class ‘Vector’ which behaves like an ‘np.ndarray’ but has a few extra attributes and methods to be used in a geometry engine (which are omitted here). The MVP below overrides ‘__ array_function __()’ to ensure that a Vector object is returned when using …

Total answers: 1

subclassing API: expected 'trainable' argument to be a boolean TF2

subclassing API: expected 'trainable' argument to be a boolean TF2 Question: I am following a tutorial for TensorFlow subclassing API: https://www.youtube.com/watch?v=WcZ_1IAH_nM&ab_channel=AladdinPersson When executing the code, I receive the following error: TypeError: Expected `trainable` argument to be a boolean, but got: [32, 32, 64] Which from what I gather comes from the ResBlock (as if trying …

Total answers: 1

TF2.6: ValueError: Model cannot be saved because the input shapes have not been set

TF2.6: ValueError: Model cannot be saved because the input shapes have not been set Question: I want to create a custom model using transfer learning in Google Colab. import tensorflow as tf from tensorflow.keras.layers import Conv2D from tensorflow.python.keras.applications.xception import Xception class MyModel(tf.keras.Model): def __init__(self, input_shape, num_classes=5, dropout_rate=0.5): super(MyModel, self).__init__() self.weight_dict = {} self.weight_dict[‘backbone’] = Xception(input_shape=input_shape, …

Total answers: 2

Calling __new__ when making a subclass of tuple

Calling __new__ when making a subclass of tuple Question: In Python, when subclassing tuple, the __new__ function is called with self as an argument. For example, here is a paraphrased version of PySpark’s Row class: class Row(tuple): def __new__(self, args): return tuple.__new__(self, args) But help(tuple) shows no self argument to __new__: __new__(*args, **kwargs) from builtins.type …

Total answers: 1

How can I subclass a Pandas DataFrame?

How can I subclass a Pandas DataFrame? Question: Subclassing Pandas classes seems a common need, but I could not find references on the subject. (It seems that Pandas developers are still working on it: Easier subclassing #60.) There are some SO questions on the subject, but I am hoping that someone here can provide a …

Total answers: 2

Emulating the list.insert() method as a subclass of Python's list

Emulating the list.insert() method as a subclass of Python's list Question: I’m trying to build a class that inherits methods from Python’s list, but also does some additional things on top… it’s probably easier just to show code at this point… class Host(object): """Emulate a virtual host attached to a physical interface""" def __init__(self): # …

Total answers: 4