无法为张量为'(?,)'的张量'input / Y:0'输入形状(100,1)的值

时间:2019-05-14 19:29:08

标签: python tensorflow shapes

我在Tensorflow中的黑兽之一是形状的问题。 我总是堆放它们。

那个时候我遇到了一个多重分类问题,我需要对one_hot使用nn.softmax_cross_entropy_with_logits编码。 我在网上尝试了许多解决方案,但仍然总是收到此错误:

Cannot feed value of shape (100, 1) for Tensor 'input/Y:0', which has shape '(?,)'

这是我的代码的必要部分:

在这里设置占位符并应用tf.one_hot

with tf.name_scope('input'):
    # [BATCH_SIZE, NUM_FEATURES]
    self.X=tf.placeholder(dtype=tf.float32, shape=[None,self.n_input_train], name="X")
    # [BATCH_SIZE]
    self.Y = tf.placeholder(dtype=tf.int32, shape=[None], name='Y')
    self.is_train = tf.placeholder(tf.bool, name="is_train")
    # [BATCH_SIZE, NUM_CLASSES]
    self.Y_onehot = tf.one_hot(indices=self.Y, depth=self.n_classes, on_value=1, off_value=0, name='Y_onehot')

sess.run中的代码堆栈显示了上面的错误:

for sample in mini_batches:
    batch_x = x_train.iloc[sample, :]
    batch_y =train_output.iloc[sample, :]
    #batch_y = np.reshape(batch_y, (-1))
    feed_dict={self.X: batch_x,self.Y:batch_y, self.is_train:True}
    self.train_summary, _, cost,acc=self.sess.run([self.merged, self.train_step, self.loss_, self.accuracy_],feed_dict=feed_dict)
    avg_cost += cost *len(sample)/n_samples 
    print('epoch[{}] step [{}] train -- loss : {}, accuracy : {}'.format(epoch,step, avg_cost, acc))
    step += 100

我的标签看起来像这样(它是一列的向量,只包含代表我的类的因子的值):

    0
0   108
1   30
2   30
3   16
4   62
5   126
6   22
7   30
8   48

在这里,我如何声明模型中的最后一个输出:

# Output fully connected layer with the output
        out_layer = tf.layers.dense(inputs=layer_3, units= self.n_classes, use_bias=True, kernel_initializer=self._init, name= 'out_layer')

那些是差异形状:

The shape of logits  (?, 64)
The shape of Y  (?, 64)
The shape of X  (?, 14)
The shape of tain_input  (847, 14)
The shape of tain_output  (847, 1)
The shape of y_batch  (100, 1) 

编辑:

这是模型:

 def multilayer_perceptron(self,X):


        # Hidden fully connected layer with n_hidden_1 neurons
        layer_1 = tf.layers.dense(inputs=X, units= self.n_hidden_1, use_bias=True, kernel_initializer=self._init, name= 'layer_1')
        layer_1 = tf.layers.batch_normalization(layer_1,training=self.is_train)
        layer_1 = self.activation(layer_1)     

        # Hidden fully connected layer with n_hidden_2 neurons
        layer_2 = tf.layers.dense(inputs=layer_1, units= self.n_hidden_2, use_bias=True, kernel_initializer=self._init, name= 'layer_2')
        layer_2 = tf.layers.batch_normalization(layer_2,training=self.is_train)
        layer_2 = self.activation(layer_2)   

        # Hidden fully connected layer with n_hidden_3 neurons
        layer_3 = tf.layers.dense(inputs=layer_2, units= self.n_hidden_3, use_bias=True, kernel_initializer=self._init, name= 'layer_3')
        layer_3 = tf.layers.batch_normalization(layer_3, training=self.is_train)
        layer_3 = self.activation(layer_3)  

        # Output fully connected layer with the output
        out_layer = tf.layers.dense(inputs=layer_3, units= self.n_classes, use_bias=True, kernel_initializer=self._init, name= 'out_layer')

        tf.summary.histogram('pre-activations', out_layer)

        return layer_1, layer_2, layer_3, out_layer 

这是我计算损失的方法:

def loss(self, X, Y):
    _, _, _, self.logits = self.multilayer_perceptron(X) 
    print("The shape of logits ", self.logits.get_shape())
    print("The shape of Y ", self.Y.get_shape())
    print("The shape of X ", X.get_shape())
    with tf.name_scope('loss'):    
        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.logits, labels=Y))
    tf.summary.scalar('loss', loss)         
    with tf.name_scope('accuracy'):
            with tf.name_scope('correct_prediction'):
                  correct_prediction = tf.equal(tf.argmax(self.logits, 1), tf.argmax(Y, 1))
            with tf.name_scope('accuracy'):
                 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', accuracy)
    self.merged =  tf.summary.merge_all()
    return loss, accuracy 

这是我称之为损失函数的位置:

def cross_validation(self,batch_size,n_hidden_​​1,n_hidden_​​2,n_hidden_​​3,learning_rate):
    损失= 0
    tf.reset_default_graph()

with tf.name_scope('input'):
    ...
    # [BATCH_SIZE]
    #self.Y=tf.placeholder(dtype=tf.int64, shape=[None,self.y_train.shape[1]], name="Y")
    self.Y = tf.placeholder(dtype=tf.int32, shape=[None], name='Y')          
    # [BATCH_SIZE, NUM_CLASSES]
    ...

self.loss_, self.accuracy_ = self.loss(self.X, self.Y_onehot)
self.train_step = self.optimizer(self.learning_rate).minimize(self.loss_)

# Initiate a tensor session
init = tf.global_variables_initializer()
self.sess = tf.Session()
self.sess.run(init)

#train the model 
loss = self.train()

self.sess.close()
del self.sess
return  loss  

我该如何解决?

他们有什么技巧可避免形状问题?

1 个答案:

答案 0 :(得分:1)

我确实使用flatten()解决了这个问题,将2D数组转换为1D数组:

 batch_y =train_output.iloc[sample, :]
 batch_y = np.array(batch_y).flatten()
相关问题