TypeError:未调整大小的对象CNN的len()

2024-04-19 20:47:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着运行一个CNN的情绪分析模型,得到一个错误,显示:

File "twitter-sentiment-cnn.py", line 292, in test_batches = list(batch_iter(zip(x_test, y_test), FLAGS.batch_size, 1))

File "/home/sanaa/Sentimental/Cnn/twitter-sentiment-cnn/data_helpers.py", line 179, in batch_iter data_size = len(data1) TypeError: len() of unsized object

 # Pretty-printing variables
if FLAGS.train:
# Batches
  batches = batch_iter(zip(x_train, y_train), FLAGS.batch_size, FLAGS.epochs)
  test_batches = list(batch_iter(zip(x_test, y_test), FLAGS.batch_size, 1))
  my_batch = batches.next()  # To use with human_readable_output()

global_step = 0
batches_in_epoch = len(y_train) / FLAGS.batch_size
batches_in_epoch = batches_in_epoch if batches_in_epoch != 0 else 1
total_num_step = FLAGS.epochs * batches_in_epoch

batches_progressbar = tqdm(batches, total=total_num_step,
                           desc='Starting training...')

这是导致错误的函数:

^{pr2}$

Tags: intestsizelenstep错误batchbatches
1条回答
网友
1楼 · 发布于 2024-04-19 20:47:23

在python3中,len不能与迭代器一起使用,zip返回一个azip对象,它是一个迭代器。我猜是这样np.数组迭代或保留。如果需要长度,则可能需要应用list(),尽管这可能会带来可伸缩性的后果。在

相关问题 更多 >