Python Tensorflow:基于输入创建向量

Python Tensorflow:基于输入创建向量,python,tensorflow,keras,Python,Tensorflow,Keras,我在Tensorflow方面并没有真正的经验,我正在做一件显然很容易的事情,但却被卡住了 我需要使用tensorflow层创建一个给定输入的矩阵。 以下是我得到的: def createTransformationMatrix(args): 比例=args[0] M=tf.Variable([scale[0],0,0,0,scale[1],0,0,0],dtype=tf.float32) 返回M scaleValue=输入(形状=(2,); createTransformMatrix=Lambd

我在Tensorflow方面并没有真正的经验,我正在做一件显然很容易的事情,但却被卡住了

我需要使用tensorflow层创建一个给定输入的矩阵。 以下是我得到的:

def createTransformationMatrix(args):
比例=args[0]
M=tf.Variable([scale[0],0,0,0,scale[1],0,0,0],dtype=tf.float32)
返回M
scaleValue=输入(形状=(2,);
createTransformMatrix=Lambda(createTransformationMatrix)(scaleValue)
transformImage=Model([scaleValue],createTransfMatrix,name='transformImage');
scaleValueInput=np.array([1.0,1.0])
输出=transformImage.predict(scaleValueInput[None,:])
这会产生以下错误:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'lambda_1/Placeholder' with dtype float and shape [?,2]
     [[Node: lambda_1/Placeholder = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

你可以用tensorflow来做

scaleValue = tf.placeholder("float32", 2)
b = tf.expand_dims(scaleValue, axis=1)
c = tf.constant([[1,0,0,0]], 'float32')
d = tf.matmul(b,c)
res = tf.reshape(d, shape=[-1])

with tf.Session() as sess:
    print (sess.run([res], feed_dict={scaleValue: np.array([1,3])}))
输出

[数组([1,0,0,0,3,0,0,0,0.],dtype=float32)]


使用填充的解决方案

scaleValue = tf.placeholder("float32", 2)
a = tf.expand_dims(scaleValue, axis=1)
paddings = tf.constant([[0, 0,], [0, 3]])
b = tf.pad(a, paddings, "CONSTANT")
res = tf.reshape(b, shape=[-1])


with tf.Session() as sess:
    print (sess.run([res], feed_dict={scaleValue: np.array([1,3])}))
将“填充”设置为“恒定”,使其成为所需的形状


其中,在
paddings=tf.常量([[top,bottom,],[left,right]])
top,bottom,left,right
表示相应位置的零数量。

谢谢,您正在利用我的M向量的属性将缩放值放置在一些方便的位置,因此,您可以执行矩阵乘法以获得结果数组。你有一个更一般的解决方案,我可以把值放在向量的任意点上吗?谢谢