3.26python作业

这篇博客展示了几个Python编程的练习,包括基于用户输入的人数判断和年龄判断的场景。通过`if`语句检查人数以决定是否接受,以及使用`while`循环不断询问用户年龄,并根据年龄给出不同票价信息。程序中包含了多种`while`循环的实现方式,以及如何退出循环的条件设定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 7-2


num = input("How many people do you have?")
num = int(num)
if (num >= 8):
    print("Sorry, there are no desk available.")
else:
    print("Welcome. This way please.")

# 7-5
while True:
    age = input("Please input your age:")
    age = int(age)
    if (age <= 3):
        print("Free ticket!")

    if (age <= 12):
        print("10 dollors")

    if (age > 12):
        print("15 dollars")


# 7-6-1
age = input("Please input your age:")
while age!= 'quit':
    age = int(age)
    if (age <= 3):
        print("Free ticket!")

    if (age <= 12):
        print("10 dollors")

    if (age > 12):
        print("15 dollars")
    age = input("Please input your age:")


# 7-6-2
active = True
while Active:
    age = input("Please input your age:")
    if(age == 'quit'):
        active = True
    else:
        age = int(age)
        if (age <= 3):
            print("Free ticket!")

        if (age <= 12):
            print("10 dollors")

        if (age > 12):
            print("15 dollars")

# 7-6-3
while True:
    age = input("Please input your age:")
    if(age == 'quit'):
        break;
    age = int(age)
    if (age <= 3):
        print("Free ticket!")

    if (age <= 12):
        print("10 dollors")

    if (age > 12):
        print("15 dollars")


# 7-7
while True:
    print(111)





### 大模型微调的设备配置要求 #### GPU 内存需求 对于大模型微调,GPU 显存的需求取决于模型参数的数量和使用的精度模式。假设模型参数为 float32 类型,则每 10 亿个参数大约需要 4 GB 的显存[^3]。例如,LLaMA 模型拥有约 70 亿参数,在全精度下加载其参数需要约 26.08 GB 的显存。然而,可以通过降低精度来减少显存消耗: - 使用 fp16 或 bf16 半精度时,显存需求减半至约 13.04 GB; - 进一步使用 int8 精度时,显存需求降至约 6.5 GB; - 如果仍不足,可以选择更低的 int4 精度,此时显存需求仅为约 3.26 GB。 尽管低精度可以有效缓解显存压力,但它可能会对模型性能造成一定影响,因此应根据具体应用场景权衡精度损失与资源节约之间的关系。 #### CPU 和 RAM 配置 除了 GPU,其他硬件组件同样重要。高性能多核 CPU 是必需的,因为它负责管理 I/O 操作和其他辅助任务。推荐选用具备高核心数和强大单线程能力的处理器型号。至于随机访问存储器 (RAM),建议至少配备 32 GB,但对于更大的模型或数据集来说,可能需要更多的内存容量以确保流畅运行[^1]。 #### 存储解决方案 快速读写速度的 NVMe SSD 应作为首选存储介质,以便于迅速加载权重文件及其他必要资料。这类硬盘相较于传统 HDD 提供更快的数据传输速率,有助于缩短整体作业周期。 #### 分布式策略应对更高挑战 当单一 GPU 已无法满足特定规模的大模型训练需求时,可考虑采用多种分布式方法进一步扩展计算能力: - **数据并行**:通过将批量样本拆分给不同节点上的相同副本独立完成前向传播与反向传播过程,从而实现时间成本削减的效果; - **ZeRO 第三阶段**:不仅实现了上述功能还额外引入了针对优化变量分布式的机制,进而达成更加高效的资源共享目的; - **张量并行**:把矩阵分解成若干部分分别交给各个单元单独处理后再汇总结果得到最终输出; - **管道并行**:按照顺序依次排列各层结构并将中间状态逐步传递向前推进直至结束整个流程为止[^2]。 综上所述,合理规划以上各项资源配置才能保障顺利完成复杂程度较高的深度学习项目开发工作。 ```python import torch from transformers import AutoModelForCausalLM, AutoTokenizer model_name = "your_model_path" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained( model_name, load_in_8bit=True, # Use INT8 quantization to save memory. device_map="auto", # Automatically map tensors across available devices. ) if not torch.cuda.is_available(): raise EnvironmentError("CUDA must be supported for this operation.") elif not torch.backends.cudnn.enabled: raise EnvironmentError("cuDNN acceleration is required but disabled.") print(f"CUDA Version: {torch.version.cuda}") ``` 问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值