sandboxj
sandboxj

Reputation: 1254

How to make a reshaping layer in tensorflow?

When I have data of shape [2,2,2], e.g.:

a = np.array([[(1,2), (3,4)],
              [(5,6), (7,8)]
              ])

And I want the layer to output [2,2], e.g.:

b = np.array([[1,0],
              [0,1]])

How do I build the layer? My current setup returns a shape of [2,2,1] and I cannot seem to be able to specify the dimensions in the units variable of the layer:

tf_x = tf.placeholder(tf.float32, [None, 2, 2]) 

output = tf.layers.dense(tf_x, 1, tf.nn.relu) 

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())    
    pred = sess.run(output, {tf_x: a})

Upvotes: 1

Views: 1836

Answers (2)

Ishant Mrinal
Ishant Mrinal

Reputation: 4918

My current setup returns a shape of [2,2,1] and I cannot seem to be able to specify the dimensions in the units variable of the layer:

a = a.reshape(1, -1)

tf_x = tf.placeholder(tf.float32, [None, a.shape[-1]]) 

# now if you want the final array to have total 4 element, you can set it as number of output
output = tf.layers.dense(tf_x, 4, tf.nn.relu) 

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())    
    pred = sess.run(output, {tf_x: a})
pred = pred.reshape(2, 2)

Upvotes: 1

Andrey Lukyanenko
Andrey Lukyanenko

Reputation: 3851

You can do something like this:

b = tf.reshape(a, shape)

Upvotes: 1

Related Questions