question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiply tensor by value in keras

See original GitHub issue

I have a short code like this:

    input = layers.Input(shape=(None, 1))
    net = input

    x = net
    net = layers.add([
        x,
        # (opt1)  net * K.variable(value=1.0, dtype='float32'),
        # (opt2) layers.multiply([K.variable(value=1.0, dtype='float32'), net]),
    ])

    net = layers.Conv1D(5, 3, padding="same")(net)
    return models.Model(inputs=[input], outputs=[net])

however I cannot seem to multiply tensor by scalar value and make a model out of it. In opt1 I get:

AttributeError: 'Tensor' object has no attribute '_keras_history'

and in opt2 I get:

  File "/home/lpp/Desktop/minion-basecaller/.venv/lib/python3.6/site-packages/keras/layers/merge.py", line 588, in multiply
    return Multiply(**kwargs)(inputs)
  File "/home/lpp/Desktop/minion-basecaller/.venv/lib/python3.6/site-packages/keras/engine/topology.py", line 594, in __call__
    self.build(input_shapes)
  File "/home/lpp/Desktop/minion-basecaller/.venv/lib/python3.6/site-packages/keras/layers/merge.py", line 74, in build
    batch_sizes = [s[0] for s in input_shape if s is not None]
  File "/home/lpp/Desktop/minion-basecaller/.venv/lib/python3.6/site-packages/keras/layers/merge.py", line 74, in <listcomp>
    batch_sizes = [s[0] for s in input_shape if s is not None]
IndexError: tuple index out of range

This looks like pretty simple things I’m having issues doing.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

3reactions
nmiculiniccommented, May 16, 2018

Ok, I got it.

class ConstMultiplierLayer(Layer):
    def __init__(self, **kwargs):
        super(ConstMultiplierLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        self.k = self.add_weight(
            name='k',
            shape=(),
            initializer='ones',
            dtype='float32',
            trainable=True,
        )
        super(ConstMultiplierLayer, self).build(input_shape)

    def call(self, x):
        return K.tf.multiply(self.k, x)

    def compute_output_shape(self, input_shape):
        return input_shape

I’m not sure though, how can I avoid being tf specific in call, but since I doubt I’ll change to something else, that’s fine

0reactions
nmiculiniccommented, May 17, 2018
AttributeError: module 'keras.backend' has no attribute 'multiply'

yeah, I checked the keras.backend and couldn’t find anythings (dot seems related, but it looks not exactly what I’m looking for.

How do I combine other keras layer objects inside new layer? So for example, I’m trying something like this:

class GatedConvResidual1D(Layer):
    """https://arxiv.org/abs/1612.08083
   """

    def __init__(self, kernel_size=3, **kwargs):
        self.kernel_size = kernel_size
        super().__init__(**kwargs)

    def build(self, input_shape):
        assert len(input_shape) == 3
        channels_in = input_shape[2]
        self.k = self.add_weight(
            name='k',
            shape=(),
            initializer='ones',
            dtype='float32',
            trainable=True,
        )
        self.residual = models.Sequential([
            layers.Conv1D(
                channels_in, kernel_size=self.kernel_size,
                activation='linear', batch_input_shape=input_shape, padding='same'),
            layers.BatchNormalization(),
            layers.Activation('relu'),
        ])
        self.residual.build(input_shape)
        super().build(input_shape)

    def call(self, x):
        return layers.add([
            x,
            K.multiply(self.k, self.residual(x)),
        ])

    def compute_output_shape(self, input_shape):
        return input_shape

crashes with already known: AttributeError: 'Tensor' object has no attribute '_keras_shape'

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to multiply Keras tensor by scalar?
You can use a Lambda layer for any other scalar manipulations. Scalar Multiplication: res5 = Lambda(lambda x: x * 3)(res4). Scalar Addition:
Read more >
tf.keras.layers.Multiply | TensorFlow v2.11.0
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape)....
Read more >
Multiply layer
Multiply class​​ Layer that multiplies (element-wise) a list of inputs. It takes as input a list of tensors, all of the same shape,...
Read more >
Backend - Keras Documentation
KERAS_BACKEND =tensorflow python -c "from keras import backend" Using TensorFlow backend. ... Multiply the values in a tensor, alongside the specified axis.
Read more >
Element-wise Multiplication in TensorFlow Python - Value ML
math.multiply(). The arguments to the function should be of the same shape. The function implicitly converts the arguments into tensors so non-tensors can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found