Multiply tensor by value in keras
See original GitHub issueI 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:
- Created 5 years ago
- Reactions:1
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok, I got it.
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 fineyeah, 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:
crashes with already known:
AttributeError: 'Tensor' object has no attribute '_keras_shape'