Tensorflow:在占位符中引入不同大小的矩阵
Tensorflow: introducing a matrix of different size in a placeholder
我正在尝试使用矩阵乘法的张量流进行简单操作,但我必须使用其列大小可变的矩阵(如下例所示)
import tensorflow as tf
input1 = tf.placeholder("float", [None,None])
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)
with tf.Session() as sess:
print(sess.run([output], feed_dict={input1:[[1,2],[3,4,5]], input2:[2.]}))
问题是,一旦我这样做,我就会收到一条错误消息,告诉我:
ValueError: setting an array element with a sequence.
我知道在第一行添加任何数字或 None(以生成 m x n 形状)可以很容易地解决这个问题,但是我想为实验训练更大的数据,但我不确定是否0 是否会影响数据。
tf.placeholder()
操作为 密集张量 定义了一个占位符,因此您必须定义您尝试提供的值中的所有元素。
另一种方法(在最新版本的 TensorFlow 中,如果您从源代码构建或下载夜间版本则可用)是使用 tf.sparse_placeholder()
op, which allows you to feed a tf.SparseTensor
with a tf.SparseTensorValue
。这允许您表示一个对象,其中并非所有元素都已定义,但未定义的元素被解释为零。
请注意,TensorFlow 对稀疏数据和可变大小示例的支持仍处于初步阶段,并且大多数操作如 tf.mul()
—are currently only defined for dense tensors. An alternative approach, which we use for variable-sized image data, is to process one (variable-sized) record at a time in an input pipeline, before converting it to a constant shape, and using the batching functions 用于制作单个密集批处理。
我正在尝试使用矩阵乘法的张量流进行简单操作,但我必须使用其列大小可变的矩阵(如下例所示)
import tensorflow as tf
input1 = tf.placeholder("float", [None,None])
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)
with tf.Session() as sess:
print(sess.run([output], feed_dict={input1:[[1,2],[3,4,5]], input2:[2.]}))
问题是,一旦我这样做,我就会收到一条错误消息,告诉我:
ValueError: setting an array element with a sequence.
我知道在第一行添加任何数字或 None(以生成 m x n 形状)可以很容易地解决这个问题,但是我想为实验训练更大的数据,但我不确定是否0 是否会影响数据。
tf.placeholder()
操作为 密集张量 定义了一个占位符,因此您必须定义您尝试提供的值中的所有元素。
另一种方法(在最新版本的 TensorFlow 中,如果您从源代码构建或下载夜间版本则可用)是使用 tf.sparse_placeholder()
op, which allows you to feed a tf.SparseTensor
with a tf.SparseTensorValue
。这允许您表示一个对象,其中并非所有元素都已定义,但未定义的元素被解释为零。
请注意,TensorFlow 对稀疏数据和可变大小示例的支持仍处于初步阶段,并且大多数操作如 tf.mul()
—are currently only defined for dense tensors. An alternative approach, which we use for variable-sized image data, is to process one (variable-sized) record at a time in an input pipeline, before converting it to a constant shape, and using the batching functions 用于制作单个密集批处理。