训练时predict和loss都为nan
Created by: shaoshenchen
paddle版本:1.7.1 在训练时没报错但是输出不正常 我在想应该是数据的某一环节出了问题,看了好久都没思路......
import paddle.fluid as fluid
import numpy as np
def train_reader():
train_data = [i for i in range(100)]
test_data = [i for i in range(0, 200, 2)]
samples = [sample for sample in zip(train_data, test_data)]
for sample in samples:
yield sample
buf_size = 100
batch_size = 5
reader = fluid.io.batch(
fluid.io.shuffle(train_reader, buf_size=buf_size),
batch_size=batch_size
) # 分批送入网络
print("试一下效果:")
for i, sample in enumerate(reader()):
print("epoch:{0} batch_size:{1} sample:{2}" .format(i, len(sample), sample))
x = fluid.data(name='x', shape=[-1, 1])
y = fluid.data(name='y', shape=[-1, 1])
fc = fluid.layers.fc(x, 10) # 偷偷加一层全连接
fc = fluid.layers.fc(fc, 1)
loss = fluid.layers.square_error_cost(fc, y)
loss_mean = fluid.layers.mean(loss)
lm = fluid.layers.Print(loss_mean)
optimizer = fluid.optimizer.SGDOptimizer(learning_rate=1e-3)
optimizer.minimize(loss_mean)
use_gpu = False
place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
for epoch in range(1000):
for samples in reader():
y_predict, loss = exe.run(
feed=feeder.feed(samples),
fetch_list=[fc, loss_mean]
)
if (epoch+1) % 100 == 0:
print("当前训练第 %d 次" % (epoch+1))
print("Predict: {0}\tLoss: {1}".format(y_predict[0], loss))
# 当前训练第 100 次
# Predict: [nan] Loss: [nan]
# 当前训练第 200 次
# Predict: [nan] Loss: [nan]
# ...
# 保存模型
save_path = 'linear_regression'
fluid.io.save_inference_model(save_path, ['x'], [fc], exe)