python神经网络学习错误记载

文章提供了一个使用TensorFlow进行简单线性模型训练的代码示例,通过`tf.compat.v1`来兼容旧版本的API。然而,在运行过程中遇到了`torch.cuda.OutOfMemoryError`,表明GPU内存不足,尝试分配1.25GiB但只有0bytesfree,提示可能需要调整内存管理设置以避免碎片化。

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

4.12

#这里代码已经更新要加上compat.vl
这是因为版本更替的原因

optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

另外在import后边第一行要加上

tf.compat.v1.disable_eager_execution()

解释原因
整个代码

#from _future_ import print_function
import tensorflow as tf
import numpy as np


tf.compat.v1.disable_eager_execution()
# create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3

### create tensorflow structure start ###
Weights = tf.Variable(tf.random.uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))

y = Weights*x_data + biases

loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
### create tensorflow structure end ###

sess = tf.compat.v1.Session()
# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
#if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
#    init = tf.initialize_all_variables()
#else:
#    init = tf.global_variables_initializer()
init=tf.compat.v1.global_variables_initializer()
sess.run(init)

for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(Weights), sess.run(biases))

结果:
结果

torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 1.25 GiB (GPU 0; 6.00 GiB total capacity; 3.53 GiB already allocated; 0 bytes free; 4.55 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 1.25 GiB (GPU 0; 6.00 GiB total capacity; 3.53 GiB already allocated; 0 bytes free; 4.55 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

报错,GPU不够用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值