Tensorflow:在占位符中引入不同大小的矩阵 [英] Tensorflow: introducing a matrix of different size in a placeholder
问题描述
我正在尝试使用矩阵乘法的张量流做一个简单的操作,但我必须使用其列大小可变的矩阵(如下面的示例所示)
I'm trying to do a simple operation with tensorflow of matrix multiplication but I have to use a matrix of variable size of its columns (as it can be seen in the example below)
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.]}))
问题是,一旦执行此操作,就会收到一条错误消息,告诉我:
The thing is that once I do this, I receive an error message telling me:
ValueError:设置具有序列的数组元素.
ValueError: setting an array element with a sequence.
我知道在第一行中添加任何数字或无(可以生成mxn形状)很容易解决,但是我想为实验训练更大的数据,并且不确定0是否会影响数据或不是.
I know this can be easily solvable adding any number or None in the first row (to produce an m x n shape), however I want to train bigger data for an experiment and I'm not sure whether a 0 could affect data or not.
推荐答案
tf.placeholder()
op为密集张量定义了一个占位符,因此您必须在值中定义所有元素正在尝试喂食.
The tf.placeholder()
op defines a placeholder for a dense tensor, so you must define all of the elements in the value that you are trying to feed.
一种替代方法(在最新版本的TensorFlow中,如果您从源代码构建或下载夜间版本,则可用)是使用 tf.SparseTensor
和
An alternative (in the latest version of TensorFlow, available if you build from source or download a nightly release) is to use a tf.sparse_placeholder()
op, which allows you to feed a tf.SparseTensor
with a tf.SparseTensorValue
. This allows you to represent an object in which not all elements are defined, but the ones that are undefined are interpreted as zeros.
请注意,TensorFlow对稀疏数据和可变大小示例的支持仍是初步的,并且大多数操作—如输入管道,然后将其转换为恒定形状并使用
Note that TensorFlow's support for sparse data and variable-sized examples is still preliminary, and most of the operations—like 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 to make a single dense batch.
这篇关于Tensorflow:在占位符中引入不同大小的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!