首页 文章

Tensorflow难以喂料批量大小

提问于
浏览
2

作为前言,我了解如何制作无尺寸张量及其主要用途 .

我隐藏的图层取决于我传入的数据的batch_size . 因此我将批量大小传递给占位符 . 不幸的是,这给我带来了一堆压力和错误,因为许多功能对于非尺寸的形状不能很好地工作 . 我正在寻找一种方法来使用张量的动态形状来计算隐藏层中的节点数 .

目前,我收到错误

InvalidArgumentError(请参阅上面的回溯):您必须使用dtype int32 [[Node:Placeholder_2 = Placeholderdtype = DT_INT32,shape = [],_device =“/ job:localhost / replica:0 / task)为占位符张量'Placeholder_2'提供值:0 / CPU:0" ]]

它不喜欢用运行时未知的形状初始化填充常量 . 我非常感谢帮助

下面是一段错误被隔离的代码片段 .

import tensorflow as tf

x = tf.placeholder(tf.float32, shape=(None, 784))

nodes = tf.div(tf.shape(x)[0],2)

bias = tf.Variable(tf.constant(.1 ,shape = [nodes]), validate_shape =  False )

init = tf.initialize_all_variables()
sess = tf.InteractiveSession()
sess.run(init)

我也收到错误:

shape = [int(dim)for dim in shape] TypeError:int()参数必须是字符串,类字节对象或数字,而不是'Tensor'

更换填充线时

bias = tf.Variable(tf.constant(.1 ,shape = [nodes]), validate_shape = False )

先感谢您 .

1 回答

  • 0

    Tensor的形状不能依赖于另一个Tensor . 形状必须仅包含整数值,或 None ,表示形状未知 . 如果要对形状执行计算,则需要使用对整数进行操作的Python基元进行计算,而不是使用在Tensors上运行的Tensorflow基元进行计算 .

    (你也不应该关闭形状验证 - 这表明你做错了 . )

    也就是说,神经网络中的不同层使用不同的批量大小值是不寻常的 - 你确定这是你想要做的吗?

    如果您确定,请尝试以下方法:

    import tensorflow as tf
    
    batch_size = 32
    
    x = tf.placeholder(tf.float32, shape=(batch_size, 784))
    bias = tf.Variable(tf.constant(.1, shape = [batch_size / 2]))
    
    init = tf.initialize_all_variables()
    sess = tf.InteractiveSession()
    sess.run(init)
    

    我希望有所帮助!

相关问题