Vaaal88
Vaaal88

Reputation: 621

Tensorflow: create vector based on input

I am not really experienced in Tensorflow and I am doing one of those things that would apparently be very easy, but getting stuck at it.

I need to create a matrix given an input using a tensorflow layer. Here is what I've gotten:

def createTransformationMatrix(args):
    scale = args[0]
    M = tf.Variable([scale[0], 0, 0, 0, scale[1], 0, 0, 0], dtype=tf.float32)
    return  M

scaleValue = Input(shape=(2,));
createTransfMatrix = Lambda(createTransformationMatrix)(scaleValue)
transformImage = Model([scaleValue], createTransfMatrix, name='transformImage');
scaleValueInput = np.array([1.0,1.0])
output = transformImage.predict(scaleValueInput[None,:])

This gives the error:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'lambda_1/Placeholder' with dtype float and shape [?,2]
     [[Node: lambda_1/Placeholder = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Upvotes: 0

Views: 219

Answers (1)

mujjiga
mujjiga

Reputation: 16906

You can do it using tensorflow

scaleValue = tf.placeholder("float32", 2)
b = tf.expand_dims(scaleValue, axis=1)
c = tf.constant([[1,0,0,0]], 'float32')
d = tf.matmul(b,c)
res = tf.reshape(d, shape=[-1])

with tf.Session() as sess:
    print (sess.run([res], feed_dict={scaleValue: np.array([1,3])}))

Output

[array([1., 0., 0., 0., 3., 0., 0., 0.], dtype=float32)]


Solution using padding

scaleValue = tf.placeholder("float32", 2)
a = tf.expand_dims(scaleValue, axis=1)
paddings = tf.constant([[0, 0,], [0, 3]])
b = tf.pad(a, paddings, "CONSTANT")
res = tf.reshape(b, shape=[-1])


with tf.Session() as sess:
    print (sess.run([res], feed_dict={scaleValue: np.array([1,3])}))

Set the padding to constant to the shape you want

Where in paddings = tf.constant([[top, bottom,], [left, right]]), top, bottom, left, right represents No:of zeros in the corresponding position.

Upvotes: 1

Related Questions