Vector Tensorflow-如何在计算批次时访问向量的元素

Vector Tensorflow-如何在计算批次时访问向量的元素,vector,tensorflow,element,Vector,Tensorflow,Element,现在在我的图中,我需要计算 z=a1*z1+a2*z2+a3*z3 在哪里 这里的?是要在图中计算的批次大小(构建图时未知) 问题是我还没有a1、a2、a3,但我需要从中提取它们 a: size=(?,3) 由,种类,a1=a[0],a2=a[1],a3=a[2]。但显然a[k]指的是批次中的第k个样本,因此此代码不正确 那么我怎样才能访问a的第k个元素呢? 如果我做a1=a[:,0],是否正确?您不做单独的乘法,而是以3D矩阵形式。以下是您可以尝试的代码: # Getting the i

现在在我的图中,我需要计算

z=a1*z1+a2*z2+a3*z3
在哪里

这里的
是要在图中计算的批次大小(构建图时未知)

问题是我还没有
a1、a2、a3
,但我需要从中提取它们

a: size=(?,3)
由,种类,
a1=a[0],a2=a[1],a3=a[2]
。但显然
a[k]
指的是批次中的第k个样本,因此此代码不正确

那么我怎样才能访问
a
的第k个元素呢?


如果我做
a1=a[:,0]
,是否正确?

您不做单独的乘法,而是以3D矩阵形式。以下是您可以尝试的代码:

 # Getting the input data in right size
 batch_size = 10
 z1 = np.random.randint(0,5, (batch_size,5))
 z2 = np.random.randint(0,5, (batch_size,5))
 z3 = np.random.randint(0,5, (batch_size,5))
 z = [z1, z2, z3]
 z = np.reshape(z, (batch_size, 5, 3))
 a = np.random.randint(0, 5, (batch_size, 3))
 a = np.reshape(a, (batch_size, 3, 1))

 #Tensorflow graph 
 Z = tf.placeholder(tf.int32, [None, 5, 3])
 A = tf.placeholder(tf.int32, [None, 3, 1])
 out = tf.squeeze(tf.matmul(Z, A))

 with tf.Session() as sess:
    result = sess.run(out, feed_dict={Z:z, A:a})

我希望这就是您打算做的。

为什么要进行单个元素的乘法。你应该做矩阵形式:z=A.T*z,其中A=[a1,a2,a3],z=[z1,z2,z3]@vijaym当然会更好。但我不知道怎么做。
 # Getting the input data in right size
 batch_size = 10
 z1 = np.random.randint(0,5, (batch_size,5))
 z2 = np.random.randint(0,5, (batch_size,5))
 z3 = np.random.randint(0,5, (batch_size,5))
 z = [z1, z2, z3]
 z = np.reshape(z, (batch_size, 5, 3))
 a = np.random.randint(0, 5, (batch_size, 3))
 a = np.reshape(a, (batch_size, 3, 1))

 #Tensorflow graph 
 Z = tf.placeholder(tf.int32, [None, 5, 3])
 A = tf.placeholder(tf.int32, [None, 3, 1])
 out = tf.squeeze(tf.matmul(Z, A))

 with tf.Session() as sess:
    result = sess.run(out, feed_dict={Z:z, A:a})