【问题标题】:For loop over 'NoneType' TensorFlow DimensionsFor 循环“NoneType”TensorFlow 维度
【发布时间】:2017-03-15 18:12:55
【问题描述】:

我正在 TensorFlow 中实现一种新型 NN。不同之处在于评估函数,所以我调用我自己的函数,而不是调用tf.matmul(),我们将调用它My_Function(A)

代码的 sn-p 如下所示,其中A 是左侧的张量乘以右侧的新 NN 实现。等效的 tensorflow 代码为 tf.matmul(A, this_new_NN)

def My_Function(self, A):
    dims = A.get_shape().as_list()
    shape = [dims[0], self.m] # Defining shape of resulting tensor
    X = tf.placeholder(tf.float32, shape=[shape[0], shape[1]])
    result = tf.zeros(tf.shape(X), dtype=tf.float32)
    for xyz in self.property:
        # Do some computation between A and xyz, xyz is a property of this_new_NN
        # resulting to temp_H with dimension [shape[0], xyz.m] of type tf.tensor
        dims_H = temp_H.get_shape().as_list()
        indices = [[i,j] for i in range(0, dims_H[0]) for j in range(xyz.k, xyz.k+dims_H[1])]
        # indices is a list of indices to update in "result"
        values = tf.reshape(temp_H, [-1]) # Values in temp_H as 1D list
        delta = tf.SparseTensor(indices, values, shape)
        result += tf.sparse_tensor_to_dense(delta)
    return result

现在我遇到的问题是在我计算 indices 的那一行,我得到了错误:

TypeError: 'NoneType' object cannot be interpreted as an integer

现在,我了解到此错误意味着您无法在 None 类型上迭代 for 循环,但我遇到的问题是测试集和训练集对于 batch_size 的值不同。这意味着当我去创建result 时,第一个维度是未知的,这就是为什么它是None 类型。

但是,为了获得我必须在 result 中更新的索引,我必须使用 for 循环将这些值生成为一个列表,以输入我正在创建为 tf.SparseTensordelta 所以可以添加到result

我的问题是,获取索引的最佳方法是什么?我尝试将 for 循环中的 dims_H[0] 替换为 tf.placeholder(tf.int32) 对象,然后在运行会话时传递大小,但出现错误:

TypeError: 'Tensor' object cannot be interpreted as an integer

编辑:

仅供参考,这段代码的调用方式如下,其中M是由tf.Variable值组成的预先构建的新NN。

Y1 = tf.nn.relu(M.My_Function(A) + B1)

其中B1 是该层的偏移量,A 是输入层。

编辑2:

result 应该是一个零张量每次 My_Function 被调用。但是,我怀疑它会在每个函数调用中保留 result 的值。如果这是正确的,请让我知道我需要做些什么来改变它。

编辑3:

定义A时,定义为

X1 = tf.placeholder(tf.float32, [None, 28, 28, 1])
A = tf.reshape(X1, [-1, 28*28])

随着A 的维度在训练数据和测试数据之间发生变化。

【问题讨论】:

    标签: python tensorflow neural-network deep-learning tensorflow-serving


    【解决方案1】:

    我刚试过这个

    >>> range(None, 0)
    Traceback (most recent call last):
      File "<pyshell#0>", line 1, in <module>
        range(None, 0)
    TypeError: 'NoneType' object cannot be interpreted as an integer
    

    检查xyz.k 是否不是None

    【讨论】:

    • 不是。当A 的尺寸固定后,我的代码功能完美。但是,当尺寸固定时,我无法在测试和训练数据上运行它。
    • 问题源于dim_H[0]None,此值来自shape[0],即A的第一个维度,其类型为None。我添加了另一个编辑来显示A 是如何创建的。
    猜你喜欢
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 2021-11-05
    • 1970-01-01
    相关资源
    最近更新 更多