admin管理员组文章数量:1125715
I am trying to clone a batch intermediately. For example my input has shape (2,3) where 2 is batch-size. But I wish to create clones of this batch between Keras layers such that the new batch shape is (6,3) with the batch of size 2 repeated 3 times. Finally I wish to also reshape the new expanded batch to (2,3,3). How can I use Lambda to do this?
I am trying to clone a batch intermediately. For example my input has shape (2,3) where 2 is batch-size. But I wish to create clones of this batch between Keras layers such that the new batch shape is (6,3) with the batch of size 2 repeated 3 times. Finally I wish to also reshape the new expanded batch to (2,3,3). How can I use Lambda to do this?
Share Improve this question asked Jan 9 at 3:28 p__10p__10 436 bronze badges1 Answer
Reset to default 0One solution with a Lambda layer:
from tensorflow import keras
from keras import layers, models
import tensorflow as tf
def myfunc(x):
res = tf.concat([x, x, x], axis=0)
res = tf.reshape(res, (-1, x.shape[-1], x.shape[-1]))
return res
inp = layers.Input(shape=(3,))
out_concat = layers.Lambda(lambda x: myfunc(x))(inp)
model = models.Model(inputs=inp, outputs=out_concat)
model.summary()
x = tf.constant([[1, 2, 3],
[4, 5, 6]])
res = model.predict([x])
print(res)
[[[1. 2. 3.]
[4. 5. 6.]
[1. 2. 3.]]
[[4. 5. 6.]
[1. 2. 3.]
[4. 5. 6.]]]
本文标签: tensorflowHow do I clone batches in KerasStack Overflow
版权声明:本文标题:tensorflow - How do I clone batches in Keras? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736674284a1947095.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论