首页 Paddle框架 帖子详情
小白飞桨搭建简单的神经网络无法运行
收藏
快速回复
Paddle框架 问答深度学习 1479 0
小白飞桨搭建简单的神经网络无法运行
收藏
快速回复
Paddle框架 问答深度学习 1479 0
import paddle.fluid as fluid
import paddle
import numpy
import math
import sys
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd

BATCH_SIZE=100

###
reader=pd.read_excel('2ppm甲醇.xlsx')
a=len(reader.index.values)-100
data1=reader.iloc[:a,4:8].values
test1=reader.iloc[a:,4:8].values
data1=np.array(data1)
test1=np.array(test1)
###
reader=pd.read_excel('4ppm甲醇.xlsx')
a=len(reader.index.values)-100
data2=reader.iloc[:a,4:8].values
test2=reader.iloc[a:,4:8].values
data2=np.array(data2)
test2=np.array(test2)
###
reader=pd.read_excel('6ppm甲醇.xlsx')
a=len(reader.index.values)-100
data3=reader.iloc[:a,4:8].values
test3=reader.iloc[a:,4:8].values
data3=np.array(data3)
test3=np.array(test3)
###
reader=pd.read_excel('9ppm甲醇.xlsx')
a=len(reader.index.values)-100
data4=reader.iloc[:a,4:8].values
test4=reader.iloc[a:,4:8].values
data4=np.array(data4)
test4=np.array(test4)
###
reader=pd.read_excel('10ppm甲醇.xlsx')
a=len(reader.index.values)-100
data5=reader.iloc[:a,4:8].values
test5=reader.iloc[a:,4:8].values
data5=np.array(data5)
test5=np.array(test5)
###
reader=pd.read_excel('12ppm甲醇.xlsx')
a=len(reader.index.values)-100
data6=reader.iloc[:a,4:8].values
test6=reader.iloc[a:,4:8].values
data6=np.array(data6)
test6=np.array(test6)
###
reader=pd.read_excel('15ppm甲醇.xlsx')
a=len(reader.index.values)-100
data7=reader.iloc[:a,4:8].values
test7=reader.iloc[a:,4:8].values
data7=np.array(data7)
test7=np.array(test7)
###
reader=pd.read_excel('18ppm甲醇.xlsx')
a=len(reader.index.values)-100
data8=reader.iloc[:a,4:8].values
test8=reader.iloc[a:,4:8].values
data8=np.array(data8)
test8=np.array(test8)
###
reader=pd.read_excel('20ppm甲醇.xlsx')
a=len(reader.index.values)-100
data9=reader.iloc[:a,4:8].values
test9=reader.iloc[a:,4:8].values
data9=np.array(data9)
test9=np.array(test9)
###
reader=pd.read_excel('25ppm甲醇.xlsx')
a=len(reader.index.values)-100
data10=reader.iloc[:a,4:8].values
test10=reader.iloc[a:,4:8].values
data10=np.array(data10)
test10=np.array(test10)

f_data=np.vstack((data1,data2,data3,data4,data5,data6,data7,data8,data9,data10)).astype('float32')

#print(f_data)
#print(f_data.shape)
a=f_data.max(axis = 0)
f_data=f_data/a
#f_data_mean=f_data.mean(0)
#f_data=f_data-f_data_mean
print(f_data)


f_test_data=np.vstack((test1,test2,test3,test4,test5,test6,test7,test8,test9,test10)).astype('float32')

#print(f_test_data.shape)
f_test_data=f_test_data/a

print(f_test_data)



def reader_creator(train_data):
    def reader():

        for d in train_data:
            yield d[:-1], d[-1:]
    return reader
print(reader_creator(train_data=f_data))

train_reader = fluid.io.batch(
    fluid.io.shuffle(
        reader_creator(f_data), buf_size=500),
        batch_size=BATCH_SIZE)
#print(train_reader)
test_reader = fluid.io.batch(
    fluid.io.shuffle(
        reader_creator(f_test_data), buf_size=500),
        batch_size=BATCH_SIZE)



train_program=fluid.Program()
startup_program=fluid.Program()

Rs_Ra_T_H = fluid.data(name='Rs_Ra_T_H', shape=[None,3], dtype='float32')

C = fluid.data(name='C', shape=[None,1], dtype='float32')
    #定义全连接层1
hidden1 = fluid.layers.fc(input=[Rs_Ra_T_H], size=3, act='softmax')
    #定义全连接层2
hidden2 = fluid.layers.fc(input=hidden1,size=7,act='tanh')
    #预测输出
prediction=fluid.layers.fc(input=hidden2,size=1)
    #设定loss
loss=fluid.layers.square_error_cost(input=prediction,label=C)
    #设定平均误差,作为迭代目标
avg_loss=fluid.layers.mean(loss)

test_program=train_program.clone(for_test=True)

sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
sgd_optimizer.minimize(avg_loss)


use_cuda = True  #False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place)

num_epochs = 100

def train_test(executor, program, reader, feeder, fetch_list):
    accumulated = 1 * [0]
    count = 0
    for data_test in reader():
        outs = executor.run(program=program,
                            feed=feeder.feed(data_test),
                            fetch_list=fetch_list)
        accumulated = [x_c[0] + x_c[1][0] for x_c in zip(accumulated, outs)] # 累加测试过程中的损失值
        count += 1 # 累加测试集中的样本数量
    return [x_d / count for x_d in accumulated] # 计算平均损失


#%matplotlib inline
params_dirname = "fit_a_line.inference.model"
feeder = fluid.DataFeeder(place=place, feed_list=[Rs_Ra_T_H, C])
exe.run(startup_program)
train_prompt = "train cost"
test_prompt = "test cost"
from paddle.utils.plot import Ploter
plot_prompt = Ploter(train_prompt, test_prompt)
step = 0

exe_test = fluid.Executor(place)


for pass_id in range(num_epochs):
    for data_train in train_reader():
        avg_loss_value, = exe.run(train_program,
                                  feed=feeder.feed(data_train),
                                  fetch_list=[avg_loss])
        if step % 10 == 0: # 每10个批次记录并输出一下训练损失
            plot_prompt.append(train_prompt, step, avg_loss_value[0])
            plot_prompt.plot()
            print("%s, Step %d, Cost %f" %
	                  (train_prompt, step, avg_loss_value[0]))
        if step % 100 == 0:  # 每100批次记录并输出一下测试损失
            test_metics = train_test(executor=exe_test,
                                     program=test_program,
                                     reader=test_reader,
                                     fetch_list=[avg_loss.name],
                                     feeder=feeder)
            plot_prompt.append(test_prompt, step, test_metics[0])
            plot_prompt.plot()
            print("%s, Step %d, Cost %f" %
	                  (test_prompt, step, test_metics[0]))
            if test_metics[0] < 10.0: # 如果准确率达到要求,则停止训练
                break

        step += 1

        if math.isnan(float(avg_loss_value[0])):
            sys.exit("got NaN loss, training failed.")

        #保存训练参数到之前给定的路径中
        if params_dirname is not None:
            fluid.io.save_inference_model(params_dirname, ['x'], [prediction], exe)

今天想搭建一个简单的神经网络拟合数据,已经可以将文件中的数据读取出来,但是在运行的时候提示avg_loss不是Variable,有大神解答一下吗?

报错信息如下

 

[[0.92637914 0.9777477 0.97822136 0.08 ]
[0.9189052 0.98228514 0.9693372 0.08 ]
[0.9239663 0.9796874 0.97582215 0.08 ]
...
[0.9459262 0.04433328 0.04597378 1. ]
[0.9466113 0.04414975 0.045841 1. ]
[0.94789386 0.04427883 0.04608302 1. ]]
[[0.95058274 0.07236755 0.07568762 0.08 ]
[0.9516528 0.07208883 0.07554381 0.08 ]
[0.95213014 0.07217994 0.07570527 0.08 ]
...
[0.9614707 0.04325511 0.04614776 1. ]
[0.96209127 0.04249408 0.04538712 1. ]
[0.9602698 0.04228874 0.0450182 1. ]]
.reader at 0x7f332537df80>
/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py:1093: UserWarning: There are no operators in the program to be executed. If you pass Program manually, please use fluid.program_guard to ensure the current Program is being used.
warnings.warn(error_info)
W0810 11:14:18.491413 8432 device_context.cc:252] Please NOTE: device: 0, CUDA Capability: 75, Driver API Version: 10.2, Runtime API Version: 10.0
W0810 11:14:18.493613 8432 device_context.cc:260] device: 0, cuDNN Version: 7.6.
/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py:1093: UserWarning: There are no operators in the program to be executed. If you pass Program manually, please use fluid.program_guard to ensure the current Program is being used.
warnings.warn(error_info)
/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py:613: UserWarning: The variable Rs_Ra_T_H is not found in program. It is not declared or is pruned.
% name)
/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py:613: UserWarning: The variable C is not found in program. It is not declared or is pruned.
% name)
/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py:1070: UserWarning: The following exception is not an EOF exception.
"The following exception is not an EOF exception.")
Traceback (most recent call last):
File "/home/guojushuai/PycharmProjects/Private_workplace/Ann.py", line 209, in
fetch_list=[avg_loss.name])
File "/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1071, in run
six.reraise(*sys.exc_info())
File "/home/guojushuai/anaconda3/lib/python3.7/site-packages/six.py", line 703, in reraise
raise value
File "/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1066, in run
return_merged=return_merged)
File "/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1154, in _run_impl
use_program_cache=use_program_cache)
File "/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1229, in _run_program
fetch_var_name)
paddle.fluid.core_avx.EnforceNotMet:

--------------------------------------------
C++ Call Stacks (More useful to developers):
--------------------------------------------
0 std::string paddle::platform::GetTraceBackString(std::string const&, char const*, int)
1 paddle::platform::EnforceNotMet::EnforceNotMet(std::string const&, char const*, int)
2 paddle::operators::FetchOp::RunImpl(paddle::framework::Scope const&, paddle::platform::Place const&) const
3 paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, paddle::platform::Place const&)
4 paddle::framework::Executor::RunPartialPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, long, long, bool, bool, bool)
5 paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool)
6 paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, std::vector > const&, bool, bool)

------------------------------------------
Python Call Stacks (More useful to users):
------------------------------------------
File "/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/framework.py", line 2610, in append_op
attrs=kwargs.get("attrs", None))
File "/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py", line 624, in _add_feed_fetch_ops
attrs={'col': i})
File "/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1224, in _run_program
fetch_var_name=fetch_var_name)
File "/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1154, in _run_impl
use_program_cache=use_program_cache)
File "/home/guojushuai/anaconda3/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1066, in run
return_merged=return_merged)
File "/home/guojushuai/PycharmProjects/Private_workplace/Ann.py", line 209, in
fetch_list=[avg_loss.name])

----------------------
Error Message Summary:
----------------------
NotFoundError: Input variable(mean_0.tmp_0) cannot be found in scope for operator 'Fetch'.Confirm that you have used the fetch `Variable` format instead of the string literal('mean_0.tmp_0') in `fetch_list` parameter when using `executor.run` method. In other words, the format of `executor.run(fetch_list=[fetch_var])`(fetch_var is a Variable) is recommended.
[Hint: fetch_var should not be null.] at (/paddle/paddle/fluid/operators/controlflow/fetch_op.cc:82)
[operator < fetch > error]

Process finished with exit code 1

0
收藏
回复
需求/bug反馈?一键提issue告诉我们
发现bug?如果您知道修复办法,欢迎提pr直接参与建设飞桨~
在@后输入用户全名并按空格结束,可艾特全站任一用户