我尝试在keras模型中创建一个常量变量。到目前为止,我一直在将其作为输入传递。但它始终是一个常量,所以我希望将其设置为常量。(每个例子的输入都是[1,2,3 ... 50]
,因此我使用np.tile(np.array(range(50)),(len(X_input)))
来为每个例子复制它)
所以现在我有:
constant_input = Input(shape=(50,), dtype='int32', name="constant_input")
这将提供一个张量:Tensor("constant_input", shape(?, 50), dtype=int32)
现在尝试将其作为常量处理:
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")
这将生成一个张量:Tensor("constant_input", shape(50,1),dtype=float32)
但我想要的是每个批次中常数进行缩放,意味着张量的形状应该是(?, 50)
,与使用Input
的方法相同。
这样做可能吗?
Model()
时,它会出现“图形断开连接:无法获取张量值”的错误。我不确定这是否与此相关,还是因为我正在尝试创建两个“模型”。在使用“Input”(问题中描述的第一种方法)时,不会发生此错误。 - Mpizos DimitrisLambda
或其他类似的东西,我已经更新了答案。 - jdehesatiled_constant
和input_batch
进行Dot()更新答案。如果我没记错的话,结果张量应该是(?, 1)
。但是我无法让它正常工作。 - Bryan Goggin