# 同范围数据集验证

$f(x)=ax^2+b+noise,x\in[-1,1]$

$f(x)=ax^2+b,x\in[-1,1]$

# load_model.py

from mindspore import context
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
import mindspore.dataset as ds
from mindspore import nn, Tensor, Model
from mindspore.train.callback import Callback, LossMonitor
import numpy as np

class LinearNet(nn.Cell): # 与训练模型相同
def __init__(self):
super(LinearNet, self).__init__()
self.fc = nn.Dense(1,1,0.02,0.02)

def construct(self, x):
x = self.fc(x)
return x

net = LinearNet()
net.set_train(False)

net_loss = nn.loss.MSELoss()
model = Model(net, net_loss, metrics={"loss"})

def get_data(num, a=2.0, b=3.0): # 与训练模型相同格式
for _ in range(num):
x = np.random.uniform(-1.0, 1.0)
z = a * x ** 2 + b
yield np.array([x**2]).astype(np.float32), np.array([z]).astype(np.float32)

def create_dataset(num_data, batch_size=16, repeat_size=1):
input_data = ds.GeneratorDataset(list(get_data(num_data)), column_names=['x','z'])
input_data = input_data.batch(batch_size)
input_data = input_data.repeat(repeat_size)
return input_data

data_number = 160 # 测试集规模一般小于训练集
batch_number = 1
repeat_number = 1

ds_valid = create_dataset(data_number, batch_size=batch_number, repeat_size=repeat_number) # 生成测试集

acc = model.eval(ds_valid, dataset_sink_mode=False) # 模型验证
print (acc)


sudo docker run --rm -v /dev/shm:/dev/shm -v /home/dechin/projects/gitlab/dechin/src/mindspore/:/home/ --runtime=nvidia --privileged=true swr.cn-south-1.myhuaweicloud.com/mindspore/mindspore-gpu:1.2.0 /bin/bash -c "cd /home && python load_model.py"
[WARNING] DEBUG(1,python):2021-05-18-03:25:53.600.802 [mindspore/ccsrc/debug/debugger/debugger.cc:80] Debugger] Not enabling debugger. Debugger does not support CPU.
{'loss': 0.0029351555945297037}


$loss=\frac{\sum_n\left(f(x_i)-net(x_i)\right)^2}{n}$

# 扩大范围数据集验证

$f(x)=ax^2+b,x\in[-10,10]$

# load_model.py

from mindspore import context
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
import mindspore.dataset as ds
from mindspore import nn, Tensor, Model
from mindspore.train.callback import Callback, LossMonitor
import numpy as np

class LinearNet(nn.Cell):
def __init__(self):
super(LinearNet, self).__init__()
self.fc = nn.Dense(1,1,0.02,0.02)

def construct(self, x):
x = self.fc(x)
return x

net = LinearNet()
net.set_train(False)

net_loss = nn.loss.MSELoss()
model = Model(net, net_loss, metrics={"loss"})

def get_data(num, a=2.0, b=3.0):
for _ in range(num):
x = np.random.uniform(-10.0, 10.0)
z = a * x ** 2 + b
yield np.array([x**2]).astype(np.float32), np.array([z]).astype(np.float32)

def create_dataset(num_data, batch_size=16, repeat_size=1):
input_data = ds.GeneratorDataset(list(get_data(num_data)), column_names=['x','z'])
input_data = input_data.batch(batch_size)
input_data = input_data.repeat(repeat_size)
return input_data

data_number = 160
batch_number = 1
repeat_number = 1

ds_valid = create_dataset(data_number, batch_size=batch_number, repeat_size=repeat_number)

acc = model.eval(ds_valid, dataset_sink_mode=False)
print (acc)


sudo docker run --rm -v /dev/shm:/dev/shm -v /home/dechin/projects/gitlab/dechin/src/mindspore/:/home/ --runtime=nvidia --privileged=true swr.cn-south-1.myhuaweicloud.com/mindspore/mindspore-gpu:1.2.0 /bin/bash -c "cd /home && python load_model.py"
[WARNING] DEBUG(1,python):2021-05-18-03:26:29.528.892 [mindspore/ccsrc/debug/debugger/debugger.cc:80] Debugger] Not enabling debugger. Debugger does not support CPU.
{'loss': 52.90038294600496}


# 版权声明

“留一手”加剧内卷，“讲不清”浪费时间。
posted @ 2021-05-18 14:17  DECHIN  阅读(197)  评论(0编辑  收藏  举报