我正在尝试运行CNN模型进行情绪分析,我收到的错误显示:
文件“twitter-sentiment-cnn.py”,第292行,in test_batches = list(batch_iter(zip(x_test,y_test),FLAGS.batch_size,1))
文件 “/home/sanaa/Sentimental/Cnn/twitter-sentiment-cnn/data_helpers.py” 第179行,在batch_iter中 data_size = len(data1)TypeError:未确定对象的len()
# 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...')
这是导致错误的函数:
def batch_iter(data, batch_size, num_epochs):
"""
Generates a batch iterator for a dataset.
"""
data = np.array(data)
data_size = len(data)
num_batches_per_epoch = int(len(data)/batch_size) + 1
for epoch in range(num_epochs):
# Shuffle the data at each epoch
shuffle_indices = np.random.permutation(np.arange(data_size))
shuffled_data = data[shuffle_indices]
for batch_num in range(num_batches_per_epoch):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
yield shuffled_data[start_index:end_index]
答案 0 :(得分:0)
在python3中,len
不能与迭代器一起使用,zip
返回一个zip对象,它是一个迭代器。我的猜测是np.array()保留了迭代器。如果您需要长度,则可能需要应用list()
,但这可能会产生可扩展性后果。