Tensorflow:将占位符的值用于另一个占位符

时间:2017-02-02 20:13:38

标签: python tensorflow neural-network

我在同一神经网络中使用不同的数据集,每个数据集具有不同数量的输出(每个9,12,15,21,65)为了做到这一点,我尝试使用整数占位符并使用此参数作为另一个占位符的参数(为了保持输出的可修改大小),如下所示:

numb_labels = tf.placeholder(tf.int32)
y = tf.placeholder(tf.float32,[None,numb_labels])

一旦我这样做,我会收到一个很长的异常,告诉我我不能把张量值放在应该放一个数字或一个字符串的位置。好的,为了尝试解决这个问题,我根据我使用的占位符

制定了一个条件
numb_labels = tf.placeholder(tf.int32)
x = tf.placeholder(tf.float32,[None,shpeData]) 
if(numb_labels==9):
    y_ = tf.placeholder(tf.float32,[None,9]) #[batch_size]
elif(numb_labels==12):
    y_ = tf.placeholder(tf.float32,[None,12])
elif(numb_labels==15):
    y_ = tf.placeholder(tf.float32,[None,15])
elif(numb_labels==21):
    y_ = tf.placeholder(tf.float32,[None,21])
else:
    y_ = tf.placeholder(tf.float32,[None,65])

但是它创建了一个65占位符,当我尝试添加9个可能标签的数据集时,我收到一个异常,告诉我我试图将大小为9的数组插入到65的张量中

我一直在制作这个技巧然后我决定玩全部的东西所以我尝试使用条件依赖于numb_labels,因为我喜欢这个

    if(numb_labels==9):
        y_ = tf.placeholder(tf.float32,[None,9]) #[batch_size]
    if(numb_labels==12):
        y_ = tf.placeholder(tf.float32,[None,12])
    if(numb_labels==15):
        y_ = tf.placeholder(tf.float32,[None,15])
    if(numb_labels==21):
        y_ = tf.placeholder(tf.float32,[None,21])
    if(numb_labels==65):
        y_ = tf.placeholder(tf.float32,[None,65])

......以及(这是与此案件相同的5个案例的例子)

if(numb_labels==65):
    W_fc3 = weight_variable([1024,65], 'fc3', 'trunc') #Modified here
    b_fc3 = bias_variable([65], 'fc3')   
    k = tf.constant(0.00000001, shape=[1]) #Constant value added to prevent underflow (probability of having zero terms)
    h_fc3 = tf.matmul(h_fc2_drop, W_fc3) + b_fc3
    y_conv = tf.nn.softmax(h_fc3) + k
    print(y_conv)
    with tf.name_scope('cross_entropy'):
        cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
        cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_))
    with tf.name_scope('train'):
        train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)

    with tf.name_scope('accuracy'):
        correct_prediction = tf.equal(tf.arg_max(y_conv,1),tf.arg_max(y_,1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

    tf.scalar_summary('cost', cost)
    tf.scalar_summary('accuracy', accuracy)

然而,当我要去训练时,我收到一个例外,告诉我train_step没有定义......我的技巧已经完成了。有人可以告诉我如何在不造成太多麻烦的情况下将占位符值放在另一个占位符中?这是可能的,还是应该保持每个架构分开,直到现在。

我还想过使用

y_ = tf.placeholder(tf.float32,[None,None])

但是我在网络中的输出确实需要固定数量的标签(或至少TF所说的)

有没有办法使用占位符值作为另一个占位符的输入,还是应该为5个不同的数据集保留5个不同的脚本?

非常感谢。

0 个答案:

没有答案