在keras中将输入与常数向量连接起来.如何定义batch_size [英] Concatenate input with constant vector in keras. how one define the batch_size

查看:982
本文介绍了在keras中将输入与常数向量连接起来.如何定义batch_size的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为对该问题的后续:

在keras中将输入与常量向量连接起来

我正在尝试使用建议的解决方案:

I am trying to use the suggested solution:

constant=K.variable(np.ones((1,10, 5)))
constant = K.repeat_elements(constant,rep=batch_size,axis=0)

并出现以下错误:

NameError: name 'batch_size' is not defined

我看不到如何在keras模型中定义batch_size [未显式],以便可以将符号层和常量层连接起来,以将它们用作输入层.

I do not see how one define within the keras model the batch_size [not explicitly] so that one can concatenate a symbolic layer and a constant layer in order to use them as an input layer.

推荐答案

获取动态批处理大小:

batch_size = K.shape(your_tensor)[0]

但是K.repeat_elements()不接受repTensor值.但是,您可以使用 K.tile() :

But K.repeat_elements() doesn't accept Tensor values for rep. You can however produce the same result using K.tile():

from keras.models import *
from keras import backend as K
import numpy as np

a = Input(shape=(10, 5))
batch_size = K.shape(a)[0]
constant = K.variable(np.ones((1,10, 5)))
constant = K.tile(constant, (batch_size, 1, 1))
print(constant)
# Tensor("Tile:0", shape=(?, 10, 5), dtype=float32)

这篇关于在keras中将输入与常数向量连接起来.如何定义batch_size的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆