You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Colab报索引越界,本地VS Code运行正常的TensorFlow项目问题

索引越界问题排查:Colab全量样本报错,本地运行正常

我用TensorFlow开发项目,在本地VS Code中使用1000条样本编译完全正常,但在Google Colab运行全量42000条样本时,出现索引越界错误;本地运行全量样本虽不报错,但耗时过长。

原字符串转浮点数函数:

def str_column_to_float(dataset, column):
    for row in dataset:
        value = row[column]
        if isinstance(value, str):  # 检查值是否为字符串
            value = value.strip()
            if not value.replace('.', '').isdigit():  # 检查值是否非数字
                continue
        try:
            row[column] = float(value)
        except (ValueError, AttributeError):
            pass  # 跳过非数字值的转换

原函数调用代码:

filename = 'Train_data.csv'
dataset = load_csv(filename)
dataset = dataset[1:]
for i in range(0, len(dataset[0]) - 1):
        str_column_to_float(dataset, i)

我未找到问题根源,也无法理解为何仅Colab报错、VS Code运行正常。随后尝试指定跳过含文字的列,问题仍未解决:

修改后的函数:

def str_column_to_float(dataset, column):
    for row in dataset:
        row[column] = float(row[column].strip())

修改后的调用代码:

for i in range(0, len(dataset[0])-1):
    if i not in [1, 2, 3, 42]:  # 跳过列1、2、3和42的转换
        str_column_to_float(dataset, i)

内容的提问来源于stack exchange,提问作者Obaid Sohail

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 00:42:13