Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

python - Creating constant value in Keras

I am trying to create a constant variable inside a keras model. What I was doing till now is to pass it as Input. But it is always a constant so I want it as a constant.(The input is [1,2,3...50] for each example => so I use np.tile(np.array(range(50)),(len(X_input))) to reproduce it for each example)

So for now I had:

constant_input = Input(shape=(50,), dtype='int32', name="constant_input")

Which gives a tensor: Tensor("constant_input", shape(?,50), dtype=int32)

Now trying to do it as a constant:

np_constant = np.array(list(range(50))).reshape(1, 50)
tf_constant = K.constant(np_constant)
tensor_constant = Input(tensor=tf_constant, shape=(50,), dtype='int32', name="constant_input")

which gives a tensor: Tensor("constant_input", shape(50,1),dtype=float32)

But What I want is the constant to be scaled in each batch, meaning that the shape of the tensor should be (?, 50), the same as the way of using Input.

Is it possible to do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot have a constant with variable size. A constant always has the same value. What you can do is have the (1, 50) constant and then tile it within TensorFlow with K.tile. Also better use np.arange instead of np.array(list(range(50)). Something like:

from keras.layers.core import Lambda
import keras.backend as K

def operateWithConstant(input_batch):
    tf_constant = K.constant(np.arange(50).reshape((1, 50)))
    batch_size = K.shape(input_batch)[0]
    tiled_constant = K.tile(tf_constant, (batch_size, 1))
    # Do some operation with tiled_constant and input_batch
    result = ...
    return result

input_batch = Input(...)
input_operated = Lambda(operateWithConstant)(input_batch)
# continue...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...