Skip to main content
 首页 » 编程设计

python之ValueError : Shapes (16, ) 和 (1, 16) 在 tensorflow1.0 中不兼容

2025年02月15日2852php

当我在tensorflow中运行以下代码时

import tensorflow as tf 
import numpy as np 
 
x_input_data = np.random.random((1024,16)) 
q = tf.train.input_producer(x_input_data, element_shape=[1,16]) 
input = q.dequeue() # It replaces our input placeholder 
print (x_input_data.shape) 
print (input.get_shape()) 
 
sess = tf.Session() 
sess.run(tf.global_variables_initializer()) 
coord = tf.train.Coordinator() 
threads = tf.train.start_queue_runners(sess=sess, coord=coord) 
example_batch = tf.train.batch([input], batch_size=1024, num_threads=4, capacity=40, enqueue_many=True) 
for step in range(1000): 
    sess.run(example_batch) 
coord.request_stop() 
coord.join(threads) 
sess.close() 

出现以下错误

ValueError: Shapes (16,) and (1, 16) are not compatible 

为什么会出现此错误?

请您参考如下方法:

来自 https://www.tensorflow.org/api_docs/python/tf/train/input_producer , element_shape 是行的形状,它引用一维 numpy 数组 (16,) 而不是提供的二维 numpy 数组 (1, 16)。如果您删除 element_shape 或将其替换为 `(16,),它应该可以工作。