Home > Enterprise >  Dyadic product of two identity tensors of second order
Dyadic product of two identity tensors of second order

Time:10-07

I need to calculate the dyadic product of two identity tensors of order 2. This should result in a 4th order tensor. However, this is slightly different to the Identity of the 4th order.

I need to calculate this in Python. Can someone help, I have no idea how I can calculate it or what I should use. I do know how to do it on paper, however, I cannot translate it into a code.

For me

I= [[1,0],[0,1]]

CodePudding user response:

That is possible when you think to create identify from products.

Sample, create an identical identity.

import tensorflow as tf


class MyDenseLayer(tf.keras.layers.Layer):
    def __init__(self, num_outputs):
        super(MyDenseLayer, self).__init__()
        self.num_outputs = num_outputs
        
    def build(self, input_shape):
        min_size_init = tf.keras.initializers.RandomUniform(minval=10, maxval=10, seed=None)
        self.kernel = self.add_weight(shape=[int(input_shape[-1]),
                        self.num_outputs],
                        initializer = min_size_init,
                        trainable=True)

    def call(self, inputs):
        return tf.matmul(inputs, self.kernel)
        
start = 3
limit = 9
delta = 3
        
# Create DATA
sample_1 = tf.range(start, limit, delta)
sample_1 = tf.cast( sample_1, dtype=tf.float32 )


start = 4
limit = 12
delta = 4

# Create DATA
sample_2 = tf.range(start, limit, delta)
sample_2 = tf.cast( sample_2, dtype=tf.float32 )

# Multiply
sample_1 = tf.constant( sample_1, shape=( 1, 2 ) )
sample_2 = tf.constant( sample_2, shape=( 1, 2 ) )
layer = MyDenseLayer(4)
data = layer(sample_1)
data = layer(sample_2)

print( data )

Sample

CodePudding user response:

You can implement dyadic (outer) product of two second rank tensors a and b with tf.expand_dims like

product = tf.expand_dims(tf.expand_dims(a, 0), 1) * tf.expand_dims(tf.expand_dims(b, 2), 3)

If you need this for just two identities a tf.transpose of reshaped to 4 rank tf.eye should be simplier.

  • Related